<pre name="code" class="csharp"><pre name="code" class="csharp"> private string Encrypt_AES(string sourceString, string AESKey)//加密 sourceString: 要加密的字符串; AESKey使用的密钥 16位
{
Byte[] keyArray;
Byte[] toEncryptArray;
keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(AESKey);
toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(sourceString);
System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
private string Decrypt_AES(string sourceString, string AESKey)//解密
{
Byte[] keyArray;
Byte[] toEncryptArray;
keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(AESKey);
toEncryptArray = Convert.FromBase64String(sourceString);
System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
rDel.Key = keyArray;
rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
}
AES Encrypt And Decrypt AES加密解密
最新推荐文章于 2025-06-11 08:01:35 发布