日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中使用DES和AES加密解密

發(fā)布時(shí)間:2023/12/10 C# 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中使用DES和AES加密解密 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼using?System;
using?System.Text;
using?System.Security.Cryptography;
using?System.IO;

namespace?MyCryptography
{
????
///?<summary>
????
///?DES加密解密
????
///?</summary>
????public?class?DES
????{
????????
///?<summary>
????????
///?獲取密鑰
????????
///?</summary>
????????private?static?string?Key
????????{
????????????
get?{?return?@"P@+#wG+Z";?}
????????}

????????
///?<summary>
????????
///?獲取向量
????????
///?</summary>
????????private?static?string?IV
????????{
????????????
get?{?return?@"L%n67}G\Mk@k%:~Y";?}
????????}

????????
///?<summary>
????????
///?DES加密
????????
///?</summary>
????????
///?<param?name="plainStr">明文字符串</param>
????????
///?<returns>密文</returns>
????????public?static?string?DESEncrypt(string?plainStr)
????????{
????????????
byte[]?bKey?=?Encoding.UTF8.GetBytes(Key);
????????????
byte[]?bIV?=?Encoding.UTF8.GetBytes(IV);
????????????
byte[]?byteArray?=?Encoding.UTF8.GetBytes(plainStr);

????????????
string?encrypt?=?null;
????????????DESCryptoServiceProvider?des?
=?new?DESCryptoServiceProvider();
????????????
try
????????????{
????????????????
using?(MemoryStream?mStream?=?new?MemoryStream())
????????????????{
????????????????????
using?(CryptoStream?cStream?=?new?CryptoStream(mStream,?des.CreateEncryptor(bKey,?bIV),?CryptoStreamMode.Write))
????????????????????{
????????????????????????cStream.Write(byteArray,?
0,?byteArray.Length);
????????????????????????cStream.FlushFinalBlock();
????????????????????????encrypt?
=?Convert.ToBase64String(mStream.ToArray());
????????????????????}
????????????????}
????????????}
????????????
catch?{?}
????????????des.Clear();

????????????
return?encrypt;
????????}

????????
///?<summary>
????????
///?DES解密
????????
///?</summary>
????????
///?<param?name="encryptStr">密文字符串</param>
????????
///?<returns>明文</returns>
????????public?static?string?DESDecrypt(string?encryptStr)
????????{
????????????
byte[]?bKey?=?Encoding.UTF8.GetBytes(Key);
????????????
byte[]?bIV?=?Encoding.UTF8.GetBytes(IV);
????????????
byte[]?byteArray?=?Convert.FromBase64String(encryptStr);

????????????
string?decrypt?=?null;
????????????DESCryptoServiceProvider?des?
=?new?DESCryptoServiceProvider();
????????????
try
????????????{
????????????????
using?(MemoryStream?mStream?=?new?MemoryStream())
????????????????{
????????????????????
using?(CryptoStream?cStream?=?new?CryptoStream(mStream,?des.CreateDecryptor(bKey,?bIV),?CryptoStreamMode.Write))
????????????????????{
????????????????????????cStream.Write(byteArray,?
0,?byteArray.Length);
????????????????????????cStream.FlushFinalBlock();
????????????????????????decrypt?
=?Encoding.UTF8.GetString(mStream.ToArray());
????????????????????}
????????????????}
????????????}
????????????
catch?{?}
????????????des.Clear();

????????????
return?decrypt;
????????}
????}

????
///?<summary>
????
///?AES加密解密
????
///?</summary>
????public?class?AES
????{
????????
///?<summary>
????????
///?獲取密鑰
????????
///?</summary>
????????private?static?string?Key
????????{
????????????
get?{?return?@")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M";?}
????????}

????????
///?<summary>
????????
///?獲取向量
????????
///?</summary>
????????private?static?string?IV
????????{
????????????
get?{?return?@"L+\~f4,Ir)b$=pkf";?}
????????}

????????
///?<summary>
????????
///?AES加密
????????
///?</summary>
????????
///?<param?name="plainStr">明文字符串</param>
????????
///?<returns>密文</returns>
????????public?static?string?AESEncrypt(string?plainStr)
????????{
????????????
byte[]?bKey?=?Encoding.UTF8.GetBytes(Key);
????????????
byte[]?bIV?=?Encoding.UTF8.GetBytes(IV);
????????????
byte[]?byteArray?=?Encoding.UTF8.GetBytes(plainStr);

????????????
string?encrypt?=?null;
????????????Rijndael?aes?
=?Rijndael.Create();
????????????
try
????????????{
????????????????
using?(MemoryStream?mStream?=?new?MemoryStream())
????????????????{
????????????????????
using?(CryptoStream?cStream?=?new?CryptoStream(mStream,?aes.CreateEncryptor(bKey,?bIV),?CryptoStreamMode.Write))
????????????????????{
????????????????????????cStream.Write(byteArray,?
0,?byteArray.Length);
????????????????????????cStream.FlushFinalBlock();
????????????????????????encrypt?
=?Convert.ToBase64String(mStream.ToArray());
????????????????????}
????????????????}
????????????}
????????????
catch?{?}
????????????aes.Clear();

????????????
return?encrypt;
????????}

????????
///?<summary>
????????
///?AES加密
????????
///?</summary>
????????
///?<param?name="plainStr">明文字符串</param>
????????
///?<param?name="returnNull">加密失敗時(shí)是否返回?null,false?返回?String.Empty</param>
????????
///?<returns>密文</returns>
????????public?static?string?AESEncrypt(string?plainStr,?bool?returnNull)
????????{
????????????
string?encrypt?=?AESEncrypt(plainStr);
????????????
return?returnNull???encrypt?:?(encrypt?==?null???String.Empty?:?encrypt);
????????}

????????
///?<summary>
????????
///?AES解密
????????
///?</summary>
????????
///?<param?name="encryptStr">密文字符串</param>
????????
///?<returns>明文</returns>
????????public?static?string?AESDecrypt(string?encryptStr)
????????{
????????????
byte[]?bKey?=?Encoding.UTF8.GetBytes(Key);
????????????
byte[]?bIV?=?Encoding.UTF8.GetBytes(IV);
????????????
byte[]?byteArray?=?Convert.FromBase64String(encryptStr);

????????????
string?decrypt?=?null;
????????????Rijndael?aes?
=?Rijndael.Create();
????????????
try
????????????{
????????????????
using?(MemoryStream?mStream?=?new?MemoryStream())
????????????????{
????????????????????
using?(CryptoStream?cStream?=?new?CryptoStream(mStream,?aes.CreateDecryptor(bKey,?bIV),?CryptoStreamMode.Write))
????????????????????{
????????????????????????cStream.Write(byteArray,?
0,?byteArray.Length);
????????????????????????cStream.FlushFinalBlock();
????????????????????????decrypt?
=?Encoding.UTF8.GetString(mStream.ToArray());
????????????????????}
????????????????}
????????????}
????????????
catch?{?}
????????????aes.Clear();

????????????
return?decrypt;
????????}

????????
///?<summary>
????????
///?AES解密
????????
///?</summary>
????????
///?<param?name="encryptStr">密文字符串</param>
????????
///?<param?name="returnNull">解密失敗時(shí)是否返回?null,false?返回?String.Empty</param>
????????
///?<returns>明文</returns>
????????public?static?string?AESDecrypt(string?encryptStr,?bool?returnNull)
????????{
????????????
string?decrypt?=?AESDecrypt(encryptStr);
????????????
return?returnNull???decrypt?:?(decrypt?==?null???String.Empty?:?decrypt);
????????}
????}
}

?

轉(zhuǎn)載于:https://www.cnblogs.com/kevin-top/archive/2010/07/01/1769351.html

總結(jié)

以上是生活随笔為你收集整理的C#中使用DES和AES加密解密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。