Beispiel #1
0
        public static string aesDecryptStringFromBytes(byte[] cipherText, string privateKeyStr)
        {
            string plaintext = null;

            using (Aes aesAlg = AES_class.GetAes(privateKeyStr))
            {
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            return(plaintext);
        }
Beispiel #2
0
 public static byte[] aesEncryptStringToBytes(string plainText, string privateKeyStr)
 {
     byte[] encrypted;
     using (Aes aesAlg = AES_class.GetAes(privateKeyStr))// Aes.Create())
     {
         ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
         using (MemoryStream msEncrypt = new MemoryStream())
         {
             using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
             {
                 using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                 {
                     swEncrypt.Write(plainText);
                 }
                 encrypted = msEncrypt.ToArray();
             }
         }
     }
     return(encrypted);
 }