.net 对称加密
2024-08-31 07:46:17
后台
public class CryptoHelper
{
// 对称加密算法提供器
private ICryptoTransform encryptor;//加密器对象
private ICryptoTransform decryptor;//解密器对象
private const int BufferSize = 1024;
public CryptoHelper(String algorithmName, String key)
{
SymmetricAlgorithm provider = SymmetricAlgorithm.Create(algorithmName);
provider.Key = Encoding.UTF8.GetBytes(key);//加密密钥
provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//偏移量
encryptor = provider.CreateEncryptor();
decryptor = provider.CreateDecryptor();
}
/// <summary>
/// TripleDES 算法 给密钥加密
/// </summary>
/// <param name="key"></param>
public CryptoHelper(string key) : this("TripleDES", key) { }
/// <summary>
/// 加密算法
/// </summary>
/// <param name="clearText"></param>
/// <returns></returns>
public String Encrypt(String clearText)
{
//创建明文流
byte[] clearBuffer = Encoding.UTF8.GetBytes(clearText);
MemoryStream clearStream = new MemoryStream(clearBuffer);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(encryptedStream, encryptor, CryptoStreamMode.Write);
//将明文流写入到buffer中
//将buffer中的数据写入到cryptoStream 中
int bytesRead = 0;
byte[] buffer = new byte[BufferSize];
//不能用循环 会执行两次
//do
//{
bytesRead = clearStream.Read(buffer, 0, BufferSize);
cryptoStream.Write(buffer, 0, BufferSize);
//} while (bytesRead > 0);
cryptoStream.FlushFinalBlock();
//获取加密后的文本
buffer = encryptedStream.ToArray();
string encryptedText = Convert.ToBase64String(buffer);
return encryptedText;
}
/// <summary>
/// 解密算法
/// </summary>
/// <param name="encryptedText"></param>
/// <returns></returns>
public String Decrypt(String encryptedText)
{
byte[] encryptedBuffer = Convert.FromBase64String(encryptedText);
Stream encryptedStream = new MemoryStream(encryptedBuffer);
MemoryStream clearStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read);
int bytesRead = 0;
byte[] buffer = new byte[BufferSize];
//不能用循环 会执行两次
//do
//{
bytesRead = cryptoStream.Read(buffer, 0, BufferSize);
clearStream.Write(buffer, 0, bytesRead);
//} while (bytesRead > 0);
buffer = clearStream.GetBuffer();
String clearText = Encoding.UTF8.GetString(buffer, 0, (int)clearStream.Length);
return clearText;
}
public static String Encrypt(string clearText, string Key)
{
CryptoHelper helper = new CryptoHelper(Key);
return helper.Encrypt(clearText);
}
public static String Decrypt(String encryptedText, String Key)
{
CryptoHelper helper = new CryptoHelper(Key);
return helper.Decrypt(encryptedText);
}
}
前台
String key = "abcdefghijklmno2";//17位数
String clearText = "欢迎光临!www.ritztours.com";
CryptoHelper helper = new CryptoHelper(key);
String encryptedText = helper.Encrypt(clearText);
String DecryptText = helper.Decrypt(encryptedText);
最新文章
- Linux根文件系统分析之init和busybox
- CentOS 6.3 中安装VirtualBOX增强工具失败:Building the main Guest Additions module[FAILED]
- EJB之Timer
- (转)JS产生随机数的几个用法!
- CSS实现垂直居中
- 【C#】C# 实现发送手机短信
- Eclipse groovy in action
- 将Oracle JDBC驱动库安装到本地仓库
- NCache实现Oracle数据与分布式缓存数据同步的3个步骤
- [HNOI2014]道路堵塞
- Text-CNN-文本分类-keras
- 使用svn创建分支!
- 剑指offer例题分享--7
- ie.360,qq浏览器这种ie内核浏览器默认阻止弹窗
- python3用BeautifulSoup用re.compile来匹配需要抓取的href地址
- Jmeter响应中中文乱码怎么解决
- 【 js 基础 】【读书笔记】Javascript “继承”
- office每次打开都要重新配置
- Checked Exception &; Unchecked Exception
- 三、Django之请求与响应-Part 1
热门文章
- (转)基于MVC4+EasyUI的Web开发框架经验总结(9)--在Datagrid里面实现外键字段的转义操作
- logger日志
- <;meta http-equiv=";refresh"; content=";3";>; 什么意思?
- day005 流程控制 (if / for / while)
- BPM结束任务
- PAT_A1128#N Queens Puzzle
- 多叉树结构的数据,parent表示法转成children表示法
- win10、win7 使用centos配置网络,可以让Xshell进行连接,虚拟机进行上网;
- docker安装部署
- SBC37x交叉编译平台QT+OPENCV