c# aes解密 java,C#实现的AES加密解密完整实例
本文實例講述了C#實現(xiàn)的AES加密解密。分享給大家供大家參考,具體如下:
/******************************************************************
* 創(chuàng)建人: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);
}
///
/// AES加密
///
/// 加密字符
/// 加密的密碼
/// 密鑰
///
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);
}
///
/// AES解密
///
///
///
///
///
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:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
密碼安全性在線檢測:
高強度密碼生成器:
MD5在線加密工具:
迅雷、快車、旋風(fēng)URL加密/解密工具:
在線散列/哈希算法加密工具:
希望本文所述對大家C#程序設(shè)計有所幫助。
總結(jié)
以上是生活随笔為你收集整理的c# aes解密 java,C#实现的AES加密解密完整实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再见,可视化!你好,Pandas!
- 下一篇: c# char unsigned_dll