AES加密算法C#实现

本人在使用C#开发winform的实际项目中,遇到一个和第三方平台进行通信的。平台要求使用AES进行加密算法,当C#端,暂且称为客户端,java端称为服务器端。当客户端发送数据给java端的时候,要使用AES算法对通信数据进行加密,java端使用AES算法进行解密,java端回复客户端同样使用AES算法进行加密,客户端(C#端)收到数据后,使用AES算法进行解密。

AES算法有两种,一种只有一个key即可加密解谜,一种使用一个key和一个IV进行数据加密。

C#无向量的AES加密算法

  1. //需要引入的命名空间
  2. using System.Security.Cryptography;
  3. /// <summary>
  4. /// AES加密(无向量)
  5. /// </summary>
  6. /// <param name="plainBytes">被加密的明文</param>
  7. /// <param name="key">密钥</param>
  8. /// <returns>密文</returns>
  9. public static string AESEncrypt(String Data, String Key)
  10. {
  11. MemoryStream mStream = new MemoryStream();
  12. RijndaelManaged aes = new RijndaelManaged();
  13. byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
  14. Byte[] bKey = new Byte[32];
  15. Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  16. aes.Mode = CipherMode.ECB;
  17. aes.Padding = PaddingMode.PKCS7;
  18. aes.KeySize = 128;
  19. //aes.Key = _key;
  20. aes.Key = bKey;
  21. //aes.IV = _iV;
  22. CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
  23. try
  24. {
  25. cryptoStream.Write(plainBytes, 0, plainBytes.Length);
  26. cryptoStream.FlushFinalBlock();
  27. return Convert.ToBase64String(mStream.ToArray());
  28. }
  29. finally
  30. {
  31. cryptoStream.Close();
  32. mStream.Close();
  33. aes.Clear();
  34. }
  35. }

该方法可以对明文进行直接加密,只需要传入需要加密的明文字符串,和key。

C#实现无向量解密

  1. /// <summary>
  2. /// AES解密(无向量)
  3. /// </summary>
  4. /// <param name="encryptedBytes">被加密的明文</param>
  5. /// <param name="key">密钥</param>
  6. /// <returns>明文</returns>
  7. public static string AESDecrypt(String Data, String Key)
  8. {
  9. Byte[] encryptedBytes = Convert.FromBase64String(Data);
  10. Byte[] bKey = new Byte[32];
  11. Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  12. MemoryStream mStream = new MemoryStream(encryptedBytes);
  13. //mStream.Write( encryptedBytes, 0, encryptedBytes.Length );
  14. //mStream.Seek( 0, SeekOrigin.Begin );
  15. RijndaelManaged aes = new RijndaelManaged();
  16. aes.Mode = CipherMode.ECB;
  17. aes.Padding = PaddingMode.PKCS7;
  18. aes.KeySize = 128;
  19. aes.Key = bKey;
  20. //aes.IV = _iV;
  21. CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
  22. try
  23. {
  24. byte[] tmp = new byte[encryptedBytes.Length + 32];
  25. int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
  26. byte[] ret = new byte[len];
  27. Array.Copy(tmp, 0, ret, 0, len);
  28. return Encoding.UTF8.GetString(ret);
  29. }
  30. finally
  31. {
  32. cryptoStream.Close();
  33. mStream.Close();
  34. aes.Clear();
  35. }
  36. }

C#实现AES算法加密,解密还可以使用有向量的方式

有向量的方式实现C#AES算法加密

  1. /// <summary>
  2. /// AES加密
  3. /// </summary>
  4. /// <param name="Data">被加密的明文</param>
  5. /// <param name="Key">密钥</param>
  6. /// <param name="Vector">向量</param>
  7. /// <returns>密文</returns>
  8. public static String AESEncrypt(String Data, String Key, String Vector)
  9. {
  10. Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);
  11. //Byte[] bKey = new Byte[32];
  12. Byte[] bKey = new Byte[16];
  13. Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  14. //Array.Copy(Encoding.UTF8.GetBytes(Key.PadLeft(bKey.Length)), bKey, bKey.Length);
  15. Byte[] bVector = new Byte[16];
  16. Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
  17. //Array.Copy(Encoding.UTF8.GetBytes(Vector.PadLeft(bVector.Length)), bVector, bVector.Length);
  18. Byte[] Cryptograph = null; // 加密后的密文
  19. Rijndael Aes = Rijndael.Create();
  20. Aes.Mode = CipherMode.CBC;
  21. try
  22. {
  23. // 开辟一块内存流
  24. using (MemoryStream Memory = new MemoryStream())
  25. {
  26. // 把内存流对象包装成加密流对象
  27. using (CryptoStream Encryptor = new CryptoStream(Memory,
  28. Aes.CreateEncryptor(bKey, bVector),
  29. CryptoStreamMode.Write))
  30. {
  31. // 明文数据写入加密流
  32. Encryptor.Write(plainBytes, 0, plainBytes.Length);
  33. Encryptor.FlushFinalBlock();
  34. Cryptograph = Memory.ToArray();
  35. }
  36. }
  37. }
  38. catch
  39. {
  40. Cryptograph = null;
  41. }
  42. return Convert.ToBase64String(Cryptograph);
  43. }

C#实现有向量AES解密

  1. /// <summary>
  2. /// AES解密
  3. /// </summary>
  4. /// <param name="Data">被解密的密文</param>
  5. /// <param name="Key">密钥</param>
  6. /// <param name="Vector">向量</param>
  7. /// <returns>明文</returns>
  8. public static String AESDecrypt(String Data, String Key, String Vector)
  9. {
  10. Byte[] encryptedBytes = Convert.FromBase64String(Data);
  11. //Byte[] bKey = new Byte[32];
  12. Byte[] bKey = new Byte[16];
  13. Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
  14. Byte[] bVector = new Byte[16];
  15. Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
  16. Byte[] original = null; // 解密后的明文
  17. Rijndael Aes = Rijndael.Create();
  18. try
  19. {
  20. // 开辟一块内存流,存储密文
  21. using (MemoryStream Memory = new MemoryStream(encryptedBytes))
  22. {
  23. // 把内存流对象包装成加密流对象
  24. using (CryptoStream Decryptor = new CryptoStream(Memory,
  25. Aes.CreateDecryptor(bKey, bVector),
  26. CryptoStreamMode.Read))
  27. {
  28. // 明文存储区
  29. using (MemoryStream originalMemory = new MemoryStream())
  30. {
  31. Byte[] Buffer = new Byte[1024];
  32. Int32 readBytes = 0;
  33. while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)
  34. {
  35. originalMemory.Write(Buffer, 0, readBytes);
  36. }
  37. original = originalMemory.ToArray();
  38. }
  39. }
  40. }
  41. }
  42. catch
  43. {
  44. original = null;
  45. }
  46. return Encoding.UTF8.GetString(original);
  47. }

以上方法,参考网络中的文章
https://www.cnblogs.com/liqipeng/archive/2013/03/23/4576174.html
亲测可用。

2024-01-11 23:25:39  user 阅读(78) 评论(0) 标签:AES,解密算法,Java算法加密,C#解密,C#加密,Java解密 分类:C#