■ BASE64 URL을 인코딩/디코딩하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Text; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> private static void Main(string[] argumentArray) { // JWT PAYLOAD string source = "eyJzdWIiOiJJRDAwMDEiLCJiaXJ0aGRhdGUiOiIxOTkwLTAxLTAxIiwiZW1haWwiOiJ0ZXN0QGRhdW0ubmV0IiwiZ2VuZGVyIjoi" + "TWFsZSIsIkNvbXBhbnlHcm91cCI6IuyYgeyXhTHqt7jro7kiLCJDb21wYW55RGVwYXJ0bWVudCI6IuyYgeyXhTHtjIAiLCJDb21w" + "YW55VGl0bGUiOiLrjIDrpqwiLCJFbXBsb3llZUlEIjoiRU1QMDAwMSIsIkVtcGxveWVlTmFtZSI6Iu2Zjeq4uOuPmSIsIm5iZiI6" + "MTYwNDMzMDM2OSwiZXhwIjoxNjA0MzMwMzY5LCJpc3MiOiJodHRwczovL2xvY2FsaG9zdDo0NDMwMC8iLCJhdWQiOiJodHRwczov" + "L2xvY2FsaG9zdDo0NDMwMC8ifQ"; byte[] targetByteArrat = Base64URLEncodingHelper.Decode(source); string target = Encoding.UTF8.GetString(targetByteArrat); Console.WriteLine(target); } #endregion } } |
▶ Base64URLEncodingHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
using System; namespace TestProject { /// <summary> /// BASE64 URL 인코딩 헬퍼 /// </summary> public static class Base64URLEncodingHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 인코딩하기 - Encode(sourceByteArray) /// <summary> /// 인코딩하기 /// </summary> /// <param name="sourceByteArray">소스 바이트 배열</param> /// <returns>BASE64 문자열</returns> public static string Encode(byte[] sourceByteArray) { if(sourceByteArray == null) { throw new ArgumentNullException("sourceByteArray"); } return Convert.ToBase64String(sourceByteArray).TrimEnd('=').Replace('+', '-').Replace('/', '_'); } #endregion #region 디코딩하기 - Decode(source) /// <summary> /// 디코딩하기 /// </summary> /// <param name="source">BASE64 문자열</param> /// <returns>바이트 배열</returns> public static byte[] Decode(string source) { if(source == null) { throw new ArgumentNullException("source"); } return Convert.FromBase64String(SetBase64Padding(source.Replace('-', '+').Replace('_', '/'))); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region BASE64 패딩 설정하기 - SetBase64Padding(source) /// <summary> /// BASE64 패딩 설정하기 /// </summary> /// <param name="source">소스 문자열</param> /// <returns>BASE64 패딩 적용 문자열</returns> private static string SetBase64Padding(string source) { int padding = 3 - ((source.Length + 3) % 4); if(padding == 0) { return source; } return source + new string('=', padding); } #endregion } } |