C#使用DES和AES实现加密解密功能示例
本文实例讲述了C#使用DES和AES实现加密解密功能。分享给大家供大家参考,具体如下:
usingSystem;
usingSystem.Text;
usingSystem.Security.Cryptography;
usingSystem.IO;
namespaceMyCryptography
{
///
///DES加密解密
///
publicclassDES
{
///
///获取密钥
///
privatestaticstringKey
{
get{return@"P@+#wG+Z";}
}
///
///获取向量
///
privatestaticstringIV
{
get{return@"L%n67}G\Mk@k%:~Y";}
}
///
///DES加密
///
///明文字符串
///密文
publicstaticstringDESEncrypt(stringplainStr)
{
byte[]bKey=Encoding.UTF8.GetBytes(Key);
byte[]bIV=Encoding.UTF8.GetBytes(IV);
byte[]byteArray=Encoding.UTF8.GetBytes(plainStr);
stringencrypt=null;
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
try
{
using(MemoryStreammStream=newMemoryStream())
{
using(CryptoStreamcStream=newCryptoStream(mStream,des.CreateEncryptor(bKey,bIV),CryptoStreamMode.Write))
{
cStream.Write(byteArray,0,byteArray.Length);
cStream.FlushFinalBlock();
encrypt=Convert.ToBase64String(mStream.ToArray());
}
}
}
catch{}
des.Clear();
returnencrypt;
}
///
///DES解密
///
///密文字符串
///明文
publicstaticstringDESDecrypt(stringencryptStr)
{
byte[]bKey=Encoding.UTF8.GetBytes(Key);
byte[]bIV=Encoding.UTF8.GetBytes(IV);
byte[]byteArray=Convert.FromBase64String(encryptStr);
stringdecrypt=null;
DESCryptoServiceProviderdes=newDESCryptoServiceProvider();
try
{
using(MemoryStreammStream=newMemoryStream())
{
using(CryptoStreamcStream=newCryptoStream(mStream,des.CreateDecryptor(bKey,bIV),CryptoStreamMode.Write))
{
cStream.Write(byteArray,0,byteArray.Length);
cStream.FlushFinalBlock();
decrypt=Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
catch{}
des.Clear();
returndecrypt;
}
}
///
///AES加密解密
///
publicclassAES
{
///
///获取密钥
///
privatestaticstringKey
{
get{return@")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M";}
}
///
///获取向量
///
privatestaticstringIV
{
get{return@"L+\~f4,Ir)b$=pkf";}
}
///
///AES加密
///
///明文字符串
///密文
publicstaticstringAESEncrypt(stringplainStr)
{
byte[]bKey=Encoding.UTF8.GetBytes(Key);
byte[]bIV=Encoding.UTF8.GetBytes(IV);
byte[]byteArray=Encoding.UTF8.GetBytes(plainStr);
stringencrypt=null;
Rijndaelaes=Rijndael.Create();
try
{
using(MemoryStreammStream=newMemoryStream())
{
using(CryptoStreamcStream=newCryptoStream(mStream,aes.CreateEncryptor(bKey,bIV),CryptoStreamMode.Write))
{
cStream.Write(byteArray,0,byteArray.Length);
cStream.FlushFinalBlock();
encrypt=Convert.ToBase64String(mStream.ToArray());
}
}
}
catch{}
aes.Clear();
returnencrypt;
}
///
///AES加密
///
///明文字符串
///加密失败时是否返回null,false返回String.Empty
///密文
publicstaticstringAESEncrypt(stringplainStr,boolreturnNull)
{
stringencrypt=AESEncrypt(plainStr);
returnreturnNull?encrypt:(encrypt==null?String.Empty:encrypt);
}
///
///AES解密
///
///密文字符串
///明文
publicstaticstringAESDecrypt(stringencryptStr)
{
byte[]bKey=Encoding.UTF8.GetBytes(Key);
byte[]bIV=Encoding.UTF8.GetBytes(IV);
byte[]byteArray=Convert.FromBase64String(encryptStr);
stringdecrypt=null;
Rijndaelaes=Rijndael.Create();
try
{
using(MemoryStreammStream=newMemoryStream())
{
using(CryptoStreamcStream=newCryptoStream(mStream,aes.CreateDecryptor(bKey,bIV),CryptoStreamMode.Write))
{
cStream.Write(byteArray,0,byteArray.Length);
cStream.FlushFinalBlock();
decrypt=Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
catch{}
aes.Clear();
returndecrypt;
}
///
///AES解密
///
///密文字符串
///解密失败时是否返回null,false返回String.Empty
///明文
publicstaticstringAESDecrypt(stringencryptStr,boolreturnNull)
{
stringdecrypt=AESDecrypt(encryptStr);
returnreturnNull?decrypt:(decrypt==null?String.Empty:decrypt);
}
}
}
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在线加密工具:
http://tools.jb51.net/password/CreateMD5Password
在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多关于C#相关内容还可查看本站专题:《C#加密与解密算法与技巧总结》、《C#窗体操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。