C#对字符串的简单加密解密过程C#的SQL数据库登陆密码的加密解密

C#对字符串的简单加密解密过程C#的SQL数据库登陆密码的加密解密加密还原直接复制代码使用即可

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Security.Cryptography; using System.IO;namespace PDAPrint{class ClassSecurityString{public static string encryptKey = ""; //定义密钥public static string Encrypt(string str){DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类对象byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定义字节数组,用来存储密钥byte[] data = Encoding.Unicode.GetBytes(str);//定义字节数组,用来存储要加密的字符串MemoryStream MStream = new MemoryStream(); //实例化内存流对象//使用内存流实例化加密流对象CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);CStream.Write(data, 0, data.Length); //向加密流中写入数据CStream.FlushFinalBlock();//释放加密流return Convert.ToBase64String(MStream.ToArray());//返回加密后的字符串}/// <summary>/// 解密字符串/// </summary>/// <param name="str">要解密的字符串</param>/// <returns>解密后的字符串</returns>public static string Decrypt(string str){DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类对象byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定义字节数组,用来存储密钥byte[] data = Convert.FromBase64String(str);//定义字节数组,用来存储要解密的字符串MemoryStream MStream = new MemoryStream(); //实例化内存流对象//使用内存流实例化解密流对象CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);CStream.Write(data, 0, data.Length);//向解密流中写入数据CStream.FlushFinalBlock();//释放解密流return Encoding.Unicode.GetString(MStream.ToArray());//返回解密后的字符串}}}

private void button10_Click(object sender, EventArgs e) { string str = "123"; //记录输入的字符串 string strNew =ClassSecurityString.Encrypt(str); //加密字符串 string strOld = ClassSecurityString.Decrypt(strNew);

MessageBox.Show("【加密:" + strNew + @"】" + "【解密:" + strOld + @"】"); }

汉码盘点机原创,转载请注明出处。

汉码盘点机——专注于傻瓜式的仓库条码管理,国内仓库条码管理整体解决方案领导品牌。

(官网:)

版权声明:本文为博主原创文章,,未经博主允许不得转载。

赶快上路吧,不要有一天我们在对方的葬礼上说,要是当时去了就好了。

C#对字符串的简单加密解密过程C#的SQL数据库登陆密码的加密解密

相关文章:

你感兴趣的文章:

标签云: