You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

151 lines
5.3 KiB

//-----------------------------------------------------------------------
// <copyright file="AES.cs" company="Origtek">
// AES belongs to Copyright (c) Origtek. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace OT.COM.Encryption
{
using System;
using System.IO;
using System.Security.Cryptography;
/// <summary>
/// <seealso cref="https://msdn.microsoft.com/zh-tw/library/system.security.cryptography.aes(v=vs.110).aspx"/>
/// AES Encrypt Decrypt
/// </summary>
public partial class AES : AEncryption
{
/// <summary>
/// Main encrypt function
/// </summary>
/// <param name="plainText">
/// Plain text to be encrypt
/// </param>
/// <param name="byaKey">
/// Encrypt key
/// </param>
/// <param name="byaIV">
/// Encrypt initial vector
/// </param>
/// <returns>
/// Encrypt result byte
/// </returns>
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;
}
/// <summary>
/// Main decrypt function
/// </summary>
/// <param name="cipherText">
/// Plain text to be decrypt
/// </param>
/// <param name="byaKey">
/// decrypt key
/// </param>
/// <param name="byaIV">
/// decrypt initial vector
/// </param>
/// <returns>
/// decrypt result byte
/// </returns>
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;
}
}
}