//-----------------------------------------------------------------------
//
// AES belongs to Copyright (c) Origtek. All rights reserved.
//
//-----------------------------------------------------------------------
namespace OT.COM.Encryption
{
using System;
using System.IO;
using System.Security.Cryptography;
///
///
/// AES Encrypt Decrypt
///
public partial class AES : AEncryption
{
///
/// Main encrypt function
///
///
/// Plain text to be encrypt
///
///
/// Encrypt key
///
///
/// Encrypt initial vector
///
///
/// Encrypt result byte
///
public override byte[] EncryptStringToByte(string plainText, byte[] byaKey, byte[] byaIV)
{
//// Check arguments.
if (plainText == null || plainText.Length <= 0)
{
throw new ArgumentNullException("plainText");
}
if (byaKey == null || ((byaKey.Length != 16) && (byaKey.Length != 24) && (byaKey.Length != 32)))
{
int nLenth = byaKey == null ? 0 : byaKey.Length;
throw new ArgumentException($"Key == null || ((Key.Length != 16) && (Key.Length != 24) && (Key.Length != 32)) length={nLenth}");
}
if (byaIV == null || (byaIV.Length != 16))
{
int nLenth = byaIV == null ? 0 : byaIV.Length;
throw new ArgumentException($"IV == null || (IV.Length != 16) length={nLenth}");
}
byte[] encrypted;
//// Create an Aes object
//// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = byaKey;
aesAlg.IV = byaIV;
// Create a decrytor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new())
{
using (CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new(csEncrypt))
{
//// Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
//// Return the encrypted bytes from the memory stream.
return encrypted;
}
///
/// Main decrypt function
///
///
/// Plain text to be decrypt
///
///
/// decrypt key
///
///
/// decrypt initial vector
///
///
/// decrypt result byte
///
public override string DecryptByteToString(byte[] cipherText, byte[] byaKey, byte[] byaIV)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
{
throw new ArgumentNullException("cipherText");
}
if (byaKey == null || ((byaKey.Length != 16) && (byaKey.Length != 24) && (byaKey.Length != 32)))
{
throw new ArgumentException("Key == null || ((Key.Length != 16) && (Key.Length != 24) && (Key.Length != 32))");
}
if (byaIV == null || (byaIV.Length != 16))
{
throw new ArgumentException("IV == null || (IV.Length != 16)");
}
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = byaKey;
aesAlg.IV = byaIV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new(cipherText))
{
using (CryptoStream csDecrypt = new(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
}