■ DESCryptoServiceProvider 클래스를 사용해 DES 복호화하는 방법을 보여준다.
▶ DESCryptoServiceProvider 클래스 : DES 복호화 하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
using System; string plain = "안녕하세요?"; // [DESCryptoServiceProvider 클래스 : DES 암호화 하기] 참조 string encrypted = DESEncrypt(plain, "01234567", new byte[] { 38, 55, 206, 48, 28, 64, 20, 16 }); Console.WriteLine(plain ); Console.WriteLine(encrypted); string decrypted = DESDecrypt(encrypted, "01234567", new byte[] { 38, 55, 206, 48, 28, 64, 20, 16 }); Console.WriteLine(decrypted); |
▶ DESCryptoServiceProvider 클래스 : DES 복호화 하기 (C#)
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 |
using System; using System.IO; using System.Security.Cryptography; using System.Text; #region DES 복호화 하기 - DESDecrypt(encrypted, key, initialValueByteArray) /// <summary> /// DES 복호화 하기 /// </summary> /// <param name="encrypted">암호화 문자열</param> /// <param name="key">키 문자열</param> /// <param name="initialValueByteArray">초기값 바이트 배열</param> /// <returns>평문 문자열</returns> public static string DESDecrypt(string encrypted, string key, byte[] initialValueByteArray) { try { byte[] keyByteArray = Encoding.UTF8.GetBytes(key); DESCryptoServiceProvider desCryptoServiceProvider = new DESCryptoServiceProvider(); Byte[] ecryptedByteArray = Convert.FromBase64String(encrypted); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream ( memoryStream, desCryptoServiceProvider.CreateDecryptor(keyByteArray, initialValueByteArray), CryptoStreamMode.Write ); cryptoStream.Write(ecryptedByteArray, 0, ecryptedByteArray.Length); cryptoStream.FlushFinalBlock(); return Encoding.UTF8.GetString(memoryStream.ToArray()); } catch(Exception) { return string.Empty; } } #endregion |
※ DES 알고리즘은 64비트 키를 사용하기 때문에 키 문자열은 8 자리, IV 바이트 배열은 8 바이트로 설정한다.