ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# AES 256 암호화와 복호화 코드
    닷넷/C# 2017. 9. 14. 14:23
    반응형
            #region 암호화 & 복호화
            private string AESEncrypt256(string _text)
            {
                string Password = "jasmintime"// 인증키
                RijndaelManaged RijndaelCipher = new RijndaelManaged();
                byte[] PlainText = Encoding.Unicode.GetBytes(_text);
                byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
                PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
                ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
                MemoryStream memoryStream = new MemoryStream();
     
                CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
                cryptoStream.Write(PlainText, 0, PlainText.Length);
                cryptoStream.FlushFinalBlock();
     
                byte[] CipherBytes = memoryStream.ToArray();
                memoryStream.Close();
                cryptoStream.Close();
     
                return Convert.ToBase64String(CipherBytes);
            }
     
            private string AESDecrypte256(string _text)
            {
                string Password = "jasmintime"// 인증키
                RijndaelManaged RijndaelCipher = new RijndaelManaged();
     
                byte[] EncryptedData = Convert.FromBase64String(_text);
                byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
                PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
                ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
                MemoryStream memoryStream = new MemoryStream(EncryptedData);
                CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
     
                byte[] PlainText = new byte[EncryptedData.Length];
                int DecrypedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
                memoryStream.Close();
                cryptoStream.Close();
     
                return Encoding.Unicode.GetString(PlainText, 0, DecrypedCount);
            }
            #endregion
    cs


    반응형

    댓글

Designed by Tistory.