private static RijndaelManaged CreateKey(DeriveBytes deriveBytes) { var result = new RijndaelManaged {Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7}; result.Key = deriveBytes.GetBytes(result.KeySize / 8); result.IV = deriveBytes.GetBytes(result.BlockSize / 8); return result; }
/// <summary> /// Decrypts a byte array /// </summary> /// <param name="Data">Data to be decrypted</param> /// <param name="Key">Password to decrypt with</param> /// <param name="Algorithm">Algorithm to use for decryption</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <returns>A decrypted byte array</returns> public byte[] Decrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(InitialVector)) throw new ArgumentNullException(nameof(InitialVector)); if (Data == null) return null; using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm)) { byte[] PlainTextBytes = new byte[0]; if (SymmetricKey != null) { SymmetricKey.Mode = CipherMode.CBC; using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream(Data)) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)) { PlainTextBytes = CryptoStream.ReadAllBinary(); } } } SymmetricKey.Clear(); } return PlainTextBytes; } }
/// <summary> /// Encrypts a byte array /// </summary> /// <param name="Data">Data to be encrypted</param> /// <param name="Key">Password to encrypt with</param> /// <param name="InitialVector">Needs to be 16 ASCII characters long</param> /// <param name="KeySize"> /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES) /// </param> /// <param name="Algorithm">Algorithm to use</param> /// <returns>The encrypted byte array</returns> public byte[] Encrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256) { if (string.IsNullOrEmpty(InitialVector)) throw new ArgumentNullException(nameof(InitialVector)); if (Data == null) return null; using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm)) { byte[] CipherTextBytes = new byte[0]; if (SymmetricKey != null) { SymmetricKey.Mode = CipherMode.CBC; using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray())) { using (MemoryStream MemStream = new MemoryStream()) { using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)) { CryptoStream.Write(Data, 0, Data.Length); CryptoStream.FlushFinalBlock(); CipherTextBytes = MemStream.ToArray(); } } } SymmetricKey.Clear(); } return CipherTextBytes; } }
private static RijndaelManaged GetAesAlgorithm(DeriveBytes bytes) { RijndaelManaged aes = new RijndaelManaged(); aes.Mode = CipherMode.CBC; aes.Key = bytes.GetBytes(aes.KeySize / 8); aes.IV = bytes.GetBytes(aes.BlockSize / 8); return aes; }