[转]C#加密解密源码
- 加密:
- static String Encrypt(String pwd) {
- DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
- PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key
- byte[] key = db.GetBytes(8);
- MemoryStream ms = new MemoryStream();//存储加密后的数据
- CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);
- byte[] data = Encoding.Unicode.GetBytes(pwd);//取到密码的字节流
- cs.Write(data, 0, data.Length);//进行加密
- cs.FlushFinalBlock();
- byte[] res = ms.ToArray();//取加密后的数据
- return Encoding.Unicode.GetString(res);//转换到字符串返回
- }
- 解密:
- static String Decrypt(String pwd, String data) {
- DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
- PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key
- byte[] key = db.GetBytes(8);
- MemoryStream ms = new MemoryStream();//存储解密后的数据
- CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);
- byte[] databytes = Encoding.Unicode.GetBytes(data);//取到加密后的数据的字节流
- cs.Write(databytes, 0, databytes.Length);//解密数据
- cs.FlushFinalBlock();
- byte[] res = ms.ToArray();
- return Encoding.Unicode.GetString(res);//返回解密后的数据
- }
- 1、方法一 (不可逆加密)
- public string EncryptPassword(string PasswordString,string PasswordFormat )
- {
- string encryptPassword = null;
- if (PasswordFormat="SHA1"){
- encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString
- ,"SHA1");
- }
- elseif (PasswordFormat="MD5")
- { encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString
- ,"MD5");
- }
- return encryptPassword ;
- }
- 2、方法二 (可逆加密)
- public interface IBindesh
- {
- string encode(string str);
- string decode(string str);
- }
- public class EncryptionDecryption : IBindesh
- {
- public string encode(string str)
- {
- string htext = "";
- for ( int i = 0; i < str.Length; i++)
- {
- htext = htext + (char) (str[i] + 10 - 1 * 2);
- }
- return htext;
- }
- public string decode(string str)
- {
- string dtext = "";
- for ( int i=0; i < str.Length; i++)
- {
- dtext = dtext + (char) (str[i] - 10 + 1*2);
- }
- return dtext;
- }
- 3、方法三 (可逆加密)
- const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
- const string IV_64 = "VavicApp";
- public string Encode(string data)
- {
- byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
- byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
- DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
- int i = cryptoProvider.KeySize;
- MemoryStream ms = new MemoryStream();
- CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey,
- byIV), CryptoStreamMode.Write);
- StreamWriter sw = new StreamWriter(cst);
- sw.Write(data);
- sw.Flush();
- cst.FlushFinalBlock();
- sw.Flush();
- return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
- }
- public string Decode(string data)
- {
- byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
- byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
- byte[] byEnc;
- try
- {
- byEnc = Convert.FromBase64String(data);
- }
- catch
- {
- return null;
- }
- DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
- MemoryStream ms = new MemoryStream(byEnc);
- CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey,
- byIV), CryptoStreamMode.Read);
- StreamReader sr = new StreamReader(cst);
- return sr.ReadToEnd();
- }
- 4、MD5不可逆加密
- (32位加密)
- public string GetMD5(string s, string _input_charset)
- {
- /**//**//**//// <summary>
- /// 与ASP兼容的MD5加密算法
- /// </summary>
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
- StringBuilder sb = new StringBuilder(32);
- for (int i = 0; i < t.Length; i++)
- {
- sb.Append(t[i].ToString("x").PadLeft(2, '0'));
- }
- return sb.ToString();
- }
- (16位加密)
- public static string GetMd5Str(string ConvertString)
- {
- MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
- string t2 =
- BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
- t2 = t2.Replace("-", "");
- return t2;
- }
- 5、加解文本文件
- //加密文件
- private static void EncryptData(String inName, String outName, byte[] desKey, byte[]
- desIV)
- {
- //Create the file streams to handle the input and output files.
- FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength(0);
- //Create variables to help with read and write.
- byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
- long rdlen = 0; //This is the total number of bytes written.
- long totlen = fin.Length; //This is the total length of the input file.
- int len; //This is the number of bytes to be written at a time.
- DES des = new DESCryptoServiceProvider();
- CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV),
- CryptoStreamMode.Write);
- //Read from the input file, then encrypt and write to the output file.
- while (rdlen < totlen)
- {
- len = fin.Read(bin, 0, 100);
- encStream.Write(bin, 0, len);
- rdlen = rdlen + len;
- }
- encStream.Close();
- fout.Close();
- fin.Close();
- }
- //解密文件
- private static void DecryptData(String inName, String outName, byte[] desKey, byte[]
- desIV)
- {
- //Create the file streams to handle the input and output files.
- FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength(0);
- //Create variables to help with read and write.
- byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
- long rdlen = 0; //This is the total number of bytes written.
- long totlen = fin.Length; //This is the total length of the input file.
- int len; //This is the number of bytes to be written at a time.
- DES des = new DESCryptoServiceProvider();
- CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV),
- CryptoStreamMode.Write);
- //Read from the input file, then encrypt and write to the output file.
- while (rdlen < totlen)
- {
- len = fin.Read(bin, 0, 100);
- encStream.Write(bin, 0, len);
- rdlen = rdlen + len;
- }
- encStream.Close();
- fout.Close();
- fin.Close();
- }
- 6、
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Security.Cryptography;
- using System.IO;
- namespace Component
- {
- public class Security
- {
- public Security()
- {
- }
- //默认密钥向量
- private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
- /**//**//**//**//**//**//**//// <summary>
- /// DES加密字符串
- /// </summary>
- /// <param name="encryptString">待加密的字符串</param>
- /// <param name="encryptKey">加密密钥,要求为8位</param>
- /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
- public static string EncryptDES(string encryptString, string encryptKey)
- {
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
- byte[] rgbIV = Keys;
- byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
- DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
- MemoryStream mStream = new MemoryStream();
- CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey,
- rgbIV), CryptoStreamMode.Write);
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return Convert.ToBase64String(mStream.ToArray());
- }
- catch
- {
- return encryptString;
- }
- }
- /**//**//**//**//**//**//**//// <summary>
- /// DES解密字符串
- /// </summary>
- /// <param name="decryptString">待解密的字符串</param>
- /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
- /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
- public static string DecryptDES(string decryptString, string decryptKey)
- {
- try
- {
- byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
- byte[] rgbIV = Keys;
- byte[] inputByteArray = Convert.FromBase64String(decryptString);
- DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
- MemoryStream mStream = new MemoryStream();
- CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,
- rgbIV), CryptoStreamMode.Write);
- cStream.Write(inputByteArray, 0, inputByteArray.Length);
- cStream.FlushFinalBlock();
- return Encoding.UTF8.GetString(mStream.ToArray());
- }
- catch
- {
- return decryptString;
- }
- }
- }
- }
[转]C#加密解密源码相关推荐
- php字符串加密解密源码,PHP中加密解密字符串函数源代码
PHP中加密解密字符串函数源代码: /** *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ / ...
- DES 算法加密/解密源码
//复制粘贴即可使用 class Program{static void Main(string[] args){string value = null;string jeiguo = null;wh ...
- java aes cfb 256_aes加密解密源码,包括aes128、aes192、aes256位,以及cbc、cfb、ecb、ofb、pcbc模式...
AES加解密总共有以下这些 算法/模式/填充 字节加密后数据长度 不满16字节加密后长度 AES/CBC/NoPadding ...
- C# Md5与AES加密解密源码记录
1.AES 加密32个x是解密密钥 AES_k 是公钥.EncryptByAES方法和De方法都需要用到 public static string EncryptByAES(string input, ...
- 企业级程序苏林加密系统源码/PHP加密程序源码
本资源可免费获取,请至尾部读阅! 企业级程序苏林加密系统源码,一款PHP加密程序源码,支持sg11加密.xend加密.goto加密.Leave加密.enphp加密.NoName加密. 可以发展用户,可 ...
- WoShop跨境电商国际支付Paypal支付商城全开源无加密商城源码
WoShop跨境电商国际支付Paypal支付商城全开源无加密商城源码 现在的企业都喜欢直接用源码进行开发,特别是在跨境电商版块这一行,很多企业都是直接购买跨境电商系统源码再进行一个二次开发,从而给自己 ...
- WoShop跨境电商USDT支付语言插件全开源无加密商城源码
WoShop跨境电商USDT支付语言插件全开源无加密商城源码 基于现场直播+购物模式,用户可以"边看边买"现场直播商城平台,全终端支持,统一管理后台,传播更强,管理更方便,支持私有 ...
- WoShop分销积分直播短视频商城全开源无加密商城源码
WoShop分销积分直播短视频商城全开源无加密商城源码 随着分销积分直播短视频商城的市场走向兴盛,不止直播电商系统的使用越来越广泛,寻求分销积分直播短视频商城源码的人也越来越多.但源码市场混乱,价格. ...
- 卡b卡社区在线下单系统无加密全套源码
介绍: 卡b卡社区在线下单系统无加密全套源码安装说明 把本程序上传到服务器或者主机空间 然后打开域名/install 配置数据库 数据库配置完成后打开域名即可! 后台地址:/admin 账号:2281 ...
- WoShop多商户直播短视频APP小程序商城全开源无加密商城源码
WoShop多商户直播短视频APP小程序商城全开源无加密商城源码 基于现场直播+购物模式,用户可以"边看边买"现场直播商城平台,全终端支持,统一管理后台,传播更强,管理更方便,支持 ...
最新文章
- python asyncio回调函数_最近用 Python 的 asyncio,有好多不懂。。
- shell脚本中$#、$0、$@等特殊变量的含义
- python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解
- jsp超链接到java文件,jsp页面超链接传中文终极解决办法
- 用线程实现动态改变图标
- ubuntu镜像添加jdk_docker基础镜像ubuntu添加jdk1.8
- UI登陆页面素材|让设计师在竞争中脱颖而出
- 打开浏览器不是主页_浏览器首页被篡改!教你几个快速解决的方法
- Spring5的几个新特性
- 入门系列之基于MATLAB的滚动轴承内外圈复合线性剥落故障动力学建模
- 【c语言】计算圆周率的近似值
- 龙芯3A3000上实现BLFS的轻量级桌面LXDE
- localhost和127.0.0.1的区别
- 微软edge浏览器安装包下载地址-Microsoft edge download
- 2.2 电阻的串联和并联
- 芯片组:北桥芯片和南桥芯片
- 基于MATLAB的声信号的采集与分析,基于Matlab的声音信号采集与分析处理
- 软考系统集成项目管理工程师视频教程(计算机系统集成部分)-乔俊峰-专题视频课程...
- Win CE 添加微软自带拼音输入法
- 右键拖动调出BandZip的快捷菜单