반응형
Base64Encode
public static string Base64Encode(string data) { try { byte[] encData_byte = new byte[data.Length]; encData_byte = System.Text.Encoding.UTF8.GetBytes(data); string encodedData = Convert.ToBase64String(encData_byte); return encodedData; } catch (Exception e) { throw new Exception("Error in Base64Encode: " + e.Message); } }
Base64Decode
public static string Base64Decode(string data) { try { System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); System.Text.Decoder utf8Decode = encoder.GetDecoder(); byte[] todecode_byte = Convert.FromBase64String(data); int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length); char[] decoded_char = new char[charCount]; utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0); string result = new String(decoded_char); return result; } catch (Exception e) { throw new Exception("Error in Base64Decode: " + e.Message); } }
Test Code
string text = "Hello Ben !!!"; string encodedText = Base64Encode(text); string decodedText = Base64Decode(encodedText); Console.WriteLine($"encodedText : {encodedText}"); Console.WriteLine($"decodedText : {decodedText}");
Test Result
encodedText : SGVsbG8gQmVuICEhIQ== decodedText : Hello Ben !!!
반응형
'[====== Development ======] > C#' 카테고리의 다른 글
| WPF Window Event 발생 순서 (0) | 2021.06.10 |
|---|---|
| Registry에 등록된 ActiveX의 파일 경로 가져오기 (0) | 2021.06.07 |
| WeakReference Class (0) | 2021.05.20 |
| File 변경 감시 (0) | 2021.05.12 |
| zip file Helper (1) | 2021.04.20 |
