C#实现加密的几种方法介绍
作者:農碼一生 时间:2022-12-12 22:38:00
1.ACSII码加密
//ACSII码加密
private static string ACSIIPWd(string rpwd)
{
string Ret;
byte[] array = System.Text.Encoding.ASCII.GetBytes(rpwd);
byte[] byteArray = new byte[array.Length];
for (int i = 0; i < array.Length; i++)
{
int asciicode = (int)(array[i]);
asciicode = asciicode + 1;
byteArray[i] = (byte)asciicode;
}
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
string strCharacter = asciiEncoding.GetString(byteArray);
Ret = strCharacter;
return Ret;
}
2.MD5加密
//MD5加密
private static string MD5PWd(string rpwd)
{
string Ret;
MD5 md5 = new MD5CryptoServiceProvider();
byte[] palindata = Encoding.Default.GetBytes(rpwd);//将要加密的字符串转换为字节数组
byte[] encryptdata = md5.ComputeHash(palindata);//将字符串加密后也转换为字符数组
Ret = Convert.ToBase64String(encryptdata);
return Ret;
}
3.RSA加密
//RSA加密
private static string RSAPWD1(string myKeyContainerName)
{
string ret = "";
CspParameters cp = new CspParameters();
cp.KeyContainerName = myKeyContainerName;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
ret = rsa.ToXmlString(true);
Console.WriteLine("Key is : \n" + rsa.ToXmlString(true));
return ret;
}
4.DES加密
//DES加密
private static string DESPWD(string ciphertext)
{
string desPWD = "";
byte[] buffer;
DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();//先创建 一个内存流
CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateEncryptor(), CryptoStreamMode.Write);//将内存流连接到加密转换流
StreamWriter sw = new StreamWriter(cryStream);
sw.WriteLine(ciphertext);//将要加密的字符串写入加密转换流
sw.Close();
cryStream.Close();
buffer = ms.ToArray();//将加密后的流转换为字节数组
desPWD = Convert.ToBase64String(buffer);//将加密后的字节数组转换为字符串
return desPWD;
}
来源:https://www.cnblogs.com/wml-it/p/12978564.html
标签:C#,加密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++ 让函数返回数组的方法
2022-12-04 06:49:33
C#中foreach语句使用break暂停遍历的方法
2022-10-12 20:14:11
C#遍历系统进程的方法
2021-09-24 15:35:29
Android Rsa数据加解密的介绍与使用示例
2023-06-24 04:51:38
![](https://img.aspxhome.com/file/2023/3/94973_0s.png)
Java Lambda List转Map代码实例
2022-05-24 20:15:42
Android中GridView插件的使用方法
2021-07-26 16:46:28
![](https://img.aspxhome.com/file/2023/6/138056_0s.png)
mybatis if标签判断不生效的解决方法
2023-11-28 12:30:49
Java 电话号码的组合示例详解
2023-07-30 22:27:53
![](https://img.aspxhome.com/file/2023/7/88207_0s.png)
Java IO之流的分类详解
2022-08-14 02:18:41
![](https://img.aspxhome.com/file/2023/1/82331_0s.png)
java通过Jsoup爬取网页过程详解
2021-12-20 03:24:10
C#表达式中的动态查询详解【译】
2021-07-29 05:19:42
![](https://img.aspxhome.com/file/2023/3/76423_0s.png)
C#运算符之与,或,异或及移位运算小结
2023-07-19 10:35:50
RecyclerView上拉加载封装代码
2023-05-08 21:02:05
![](https://img.aspxhome.com/file/2023/5/117145_0s.gif)
Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法
2023-04-17 11:57:25
IDEA 使用mybatis插件Free Mybatis plugin的步骤(推荐)
2022-12-05 02:01:03
![](https://img.aspxhome.com/file/2023/8/72328_0s.jpg)
Android持久化技术之SharedPreferences存储实例详解
2023-03-25 03:17:10
![](https://img.aspxhome.com/file/2023/2/138922_0s.jpg)
微信js sdk invalid signature签名错误问题的解决方法分析
2021-07-26 02:26:47
每天学Java!一分钟了解JRE与JDK
2021-06-13 11:52:41
![](https://img.aspxhome.com/file/2023/5/85895_0s.jpg)
springBoot详解集成Swagger流程
2023-02-26 22:23:33
![](https://img.aspxhome.com/file/2023/0/89640_0s.png)
Android studio4.1更新后出现的问题详解
2022-12-06 03:22:54
![](https://img.aspxhome.com/file/2023/4/137624_0s.png)