AES加密算法C#实现
本人在使用C#开发winform的实际项目中,遇到一个和第三方平台进行通信的。平台要求使用AES进行加密算法,当C#端,暂且称为客户端,java端称为服务器端。当客户端发送数据给java端的时候,要使用AES算法对通信数据进行加密,java端使用AES算法进行解密,java端回复客户端同样使用AES算法进行加密,客户端(C#端)收到数据后,使用AES算法进行解密。
AES算法有两种,一种只有一个key即可加密解谜,一种使用一个key和一个IV进行数据加密。
C#无向量的AES加密算法
//需要引入的命名空间
using System.Security.Cryptography;
/// <summary>
/// AES加密(无向量)
/// </summary>
/// <param name="plainBytes">被加密的明文</param>
/// <param name="key">密钥</param>
/// <returns>密文</returns>
public static string AESEncrypt(String Data, String Key)
{
MemoryStream mStream = new MemoryStream();
RijndaelManaged aes = new RijndaelManaged();
byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
//aes.Key = _key;
aes.Key = bKey;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
try
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
该方法可以对明文进行直接加密,只需要传入需要加密的明文字符串,和key。
C#实现无向量解密
/// <summary>
/// AES解密(无向量)
/// </summary>
/// <param name="encryptedBytes">被加密的明文</param>
/// <param name="key">密钥</param>
/// <returns>明文</returns>
public static string AESDecrypt(String Data, String Key)
{
Byte[] encryptedBytes = Convert.FromBase64String(Data);
Byte[] bKey = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
MemoryStream mStream = new MemoryStream(encryptedBytes);
//mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
//mStream.Seek( 0, SeekOrigin.Begin );
RijndaelManaged aes = new RijndaelManaged();
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.PKCS7;
aes.KeySize = 128;
aes.Key = bKey;
//aes.IV = _iV;
CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
try
{
byte[] tmp = new byte[encryptedBytes.Length + 32];
int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
byte[] ret = new byte[len];
Array.Copy(tmp, 0, ret, 0, len);
return Encoding.UTF8.GetString(ret);
}
finally
{
cryptoStream.Close();
mStream.Close();
aes.Clear();
}
}
C#实现AES算法加密,解密还可以使用有向量的方式
有向量的方式实现C#AES算法加密
/// <summary>
/// AES加密
/// </summary>
/// <param name="Data">被加密的明文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>密文</returns>
public static String AESEncrypt(String Data, String Key, String Vector)
{
Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
//Byte[] bKey = new Byte[32];
Byte[] bKey = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
//Array.Copy(Encoding.UTF8.GetBytes(Key.PadLeft(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
//Array.Copy(Encoding.UTF8.GetBytes(Vector.PadLeft(bVector.Length)), bVector, bVector.Length);
Byte[] Cryptograph = null; // 加密后的密文
Rijndael Aes = Rijndael.Create();
Aes.Mode = CipherMode.CBC;
try
{
// 开辟一块内存流
using (MemoryStream Memory = new MemoryStream())
{
// 把内存流对象包装成加密流对象
using (CryptoStream Encryptor = new CryptoStream(Memory,
Aes.CreateEncryptor(bKey, bVector),
CryptoStreamMode.Write))
{
// 明文数据写入加密流
Encryptor.Write(plainBytes, 0, plainBytes.Length);
Encryptor.FlushFinalBlock();
Cryptograph = Memory.ToArray();
}
}
}
catch
{
Cryptograph = null;
}
return Convert.ToBase64String(Cryptograph);
}
C#实现有向量AES解密
/// <summary>
/// AES解密
/// </summary>
/// <param name="Data">被解密的密文</param>
/// <param name="Key">密钥</param>
/// <param name="Vector">向量</param>
/// <returns>明文</returns>
public static String AESDecrypt(String Data, String Key, String Vector)
{
Byte[] encryptedBytes = Convert.FromBase64String(Data);
//Byte[] bKey = new Byte[32];
Byte[] bKey = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
Byte[] bVector = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
Byte[] original = null; // 解密后的明文
Rijndael Aes = Rijndael.Create();
try
{
// 开辟一块内存流,存储密文
using (MemoryStream Memory = new MemoryStream(encryptedBytes))
{
// 把内存流对象包装成加密流对象
using (CryptoStream Decryptor = new CryptoStream(Memory,
Aes.CreateDecryptor(bKey, bVector),
CryptoStreamMode.Read))
{
// 明文存储区
using (MemoryStream originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}
original = originalMemory.ToArray();
}
}
}
}
catch
{
original = null;
}
return Encoding.UTF8.GetString(original);
}
以上方法,参考网络中的文章
https://www.cnblogs.com/liqipeng/archive/2013/03/23/4576174.html
亲测可用。
很赞哦! (0)