C#完成的AES加密解密完全實例。本站提示廣大學習愛好者:(C#完成的AES加密解密完全實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的AES加密解密完全實例正文
本文實例講述了C#完成的AES加密解密。分享給年夜家供年夜家參考,詳細以下:
/******************************************************************
* 創立人:HTL
* 解釋:C# AES加密解密
*******************************************************************/
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public class Test
{
public static void Main()
{
//暗碼
string password="1234567890123456";
//加密初始化向量
string iv=" ";
string message=AESEncrypt("abcdefghigklmnopqrstuvwxyz0123456789",password,iv);
Console.WriteLine(message);
message=AESDecrypt("8Z3dZzqn05FmiuBLowExK0CAbs4TY2GorC2dDPVlsn/tP+VuJGePqIMv1uSaVErr",password,iv);
Console.WriteLine(message);
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="text">加密字符</param>
/// <param name="password">加密的暗碼</param>
/// <param name="iv">密鑰</param>
/// <returns></returns>
public static string AESEncrypt(string text, string password, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = new byte[16];
ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
byte[] plainText = Encoding.UTF8.GetBytes(text);
byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherBytes);
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="text"></param>
/// <param name="password"></param>
/// <param name="iv"></param>
/// <returns></returns>
public static string AESDecrypt(string text, string password, string iv)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.CBC;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = ivBytes;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
}
PS:關於加密解密感興致的同伙還可以參考本站在線對象:
暗碼平安性在線檢測:
tools.jb51.net/password/my_password_safe
高強度暗碼生成器:
tools.jb51.net/password/CreateStrongPassword
MD5在線加密對象:
tools.jb51.net/password/CreateMD5Password
迅雷、慢車、旋風URL加密/解密對象:
tools.jb51.net/password/urlrethunder
在線散列/哈希算法加密對象:
tools.jb51.net/password/hash_encrypt
願望本文所述對年夜家C#法式設計有所贊助。