public static byte[] AES_Encrypt128(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] key = new byte[16];
if(passwordBytes.Length>16)
{
Array.Copy(passwordBytes, 0, key, 0, 16);
}
else
{
key = passwordBytes;
}
byte[] encryptedBytes = null;
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 128;
AES.BlockSize = 128;
AES.Padding = PaddingMode.None;//如果不是None, 会自动添加一个Block
AES.Mode = CipherMode.ECB;
AES.Key = key;
using (CryptoStream cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
网友评论