Code:
  1. 加密:
  2. static String Encrypt(String pwd) {
  3. DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//des进行加密
  4. PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key
  5. byte[] key = db.GetBytes(8);
  6. MemoryStream ms = new MemoryStream();//存储加密后的数据
  7. CryptoStream cs = new CryptoStream(ms,desc.CreateEncryptor(key, key),CryptoStreamMode.Write);
  8. byte[] data = Encoding.Unicode.GetBytes(pwd);//取到密码的字节流
  9. cs.Write(data, 0, data.Length);//进行加密
  10. cs.FlushFinalBlock();
  11. byte[] res = ms.ToArray();//取加密后的数据
  12. return Encoding.Unicode.GetString(res);//转换到字符串返回
  13. }
  14. 解密:
  15. static String Decrypt(String pwd, String data) {
  16. DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
  17. PasswordDeriveBytes db = new PasswordDeriveBytes(pwd, null);//产生key
  18. byte[] key = db.GetBytes(8);
  19. MemoryStream ms = new MemoryStream();//存储解密后的数据
  20. CryptoStream cs = new CryptoStream(ms,desc.CreateDecryptor(key, key),CryptoStreamMode.Write);
  21. byte[] databytes = Encoding.Unicode.GetBytes(data);//取到加密后的数据的字节流
  22. cs.Write(databytes, 0, databytes.Length);//解密数据
  23. cs.FlushFinalBlock();
  24. byte[] res = ms.ToArray();
  25. return Encoding.Unicode.GetString(res);//返回解密后的数据
  26. }
  27. 1、方法一 (不可逆加密)
  28. public string EncryptPassword(string PasswordString,string PasswordFormat )
  29. {
  30. string encryptPassword = null;
  31. if (PasswordFormat="SHA1"){
  32. encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString
  33. ,"SHA1");
  34. }
  35. elseif (PasswordFormat="MD5")
  36. { encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordString
  37. ,"MD5");
  38. }
  39. return encryptPassword ;
  40. }
  41. 2、方法二 (可逆加密)
  42. public interface IBindesh
  43. {
  44. string encode(string str);
  45. string decode(string str);
  46. }
  47. public class EncryptionDecryption : IBindesh
  48. {
  49. public string encode(string str)
  50. {
  51. string htext = "";
  52. for ( int i = 0; i < str.Length; i++)
  53. {
  54. htext = htext + (char) (str[i] + 10 - 1 * 2);
  55. }
  56. return htext;
  57. }
  58. public string decode(string str)
  59. {
  60. string dtext = "";
  61. for ( int i=0; i < str.Length; i++)
  62. {
  63. dtext = dtext + (char) (str[i] - 10 + 1*2);
  64. }
  65. return dtext;
  66. }
  67. 3、方法三 (可逆加密)
  68. const string KEY_64 = "VavicApp";//注意了,是8个字符,64位
  69. const string IV_64 = "VavicApp";
  70. public string Encode(string data)
  71. {
  72. byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
  73. byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
  74. DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  75. int i = cryptoProvider.KeySize;
  76. MemoryStream ms = new MemoryStream();
  77. CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey,
  78. byIV), CryptoStreamMode.Write);
  79. StreamWriter sw = new StreamWriter(cst);
  80. sw.Write(data);
  81. sw.Flush();
  82. cst.FlushFinalBlock();
  83. sw.Flush();
  84. return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
  85. }
  86. public string Decode(string data)
  87. {
  88. byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
  89. byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
  90. byte[] byEnc;
  91. try
  92. {
  93. byEnc = Convert.FromBase64String(data);
  94. }
  95. catch
  96. {
  97. return null;
  98. }
  99. DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
  100. MemoryStream ms = new MemoryStream(byEnc);
  101. CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey,
  102. byIV), CryptoStreamMode.Read);
  103. StreamReader sr = new StreamReader(cst);
  104. return sr.ReadToEnd();
  105. }
  106. 4、MD5不可逆加密
  107. (32位加密)
  108. public string GetMD5(string s, string _input_charset)
  109. {
  110. /**//**//**//// <summary>
  111. /// 与ASP兼容的MD5加密算法
  112. /// </summary>
  113. MD5 md5 = new MD5CryptoServiceProvider();
  114. byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
  115. StringBuilder sb = new StringBuilder(32);
  116. for (int i = 0; i < t.Length; i++)
  117. {
  118. sb.Append(t[i].ToString("x").PadLeft(2, '0'));
  119. }
  120. return sb.ToString();
  121. }
  122. (16位加密)
  123. public static string GetMd5Str(string ConvertString)
  124. {
  125. MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
  126. string t2 =
  127. BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
  128. t2 = t2.Replace("-", "");
  129. return t2;
  130. }
  131. 5、加解文本文件
  132. //加密文件
  133. private static void EncryptData(String inName, String outName, byte[] desKey, byte[]
  134. desIV)
  135. {
  136. //Create the file streams to handle the input and output files.
  137. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  138. FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  139. fout.SetLength(0);
  140. //Create variables to help with read and write.
  141. byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
  142. long rdlen = 0;              //This is the total number of bytes written.
  143. long totlen = fin.Length;    //This is the total length of the input file.
  144. int len;                     //This is the number of bytes to be written at a time.
  145. DES des = new DESCryptoServiceProvider();
  146. CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV),
  147. CryptoStreamMode.Write);
  148. //Read from the input file, then encrypt and write to the output file.
  149. while (rdlen < totlen)
  150. {
  151. len = fin.Read(bin, 0, 100);
  152. encStream.Write(bin, 0, len);
  153. rdlen = rdlen + len;
  154. }
  155. encStream.Close();
  156. fout.Close();
  157. fin.Close();
  158. }
  159. //解密文件
  160. private static void DecryptData(String inName, String outName, byte[] desKey, byte[]
  161. desIV)
  162. {
  163. //Create the file streams to handle the input and output files.
  164. FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
  165. FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
  166. fout.SetLength(0);
  167. //Create variables to help with read and write.
  168. byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
  169. long rdlen = 0;              //This is the total number of bytes written.
  170. long totlen = fin.Length;    //This is the total length of the input file.
  171. int len;                     //This is the number of bytes to be written at a time.
  172. DES des = new DESCryptoServiceProvider();
  173. CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV),
  174. CryptoStreamMode.Write);
  175. //Read from the input file, then encrypt and write to the output file.
  176. while (rdlen < totlen)
  177. {
  178. len = fin.Read(bin, 0, 100);
  179. encStream.Write(bin, 0, len);
  180. rdlen = rdlen + len;
  181. }
  182. encStream.Close();
  183. fout.Close();
  184. fin.Close();
  185. }
  186. 6、
  187. using System;
  188. using System.Collections.Generic;
  189. using System.Text;
  190. using System.Security.Cryptography;
  191. using System.IO;
  192. namespace Component
  193. {
  194. public class Security
  195. {
  196. public Security()
  197. {
  198. }
  199. //默认密钥向量
  200. private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
  201. /**//**//**//**//**//**//**//// <summary>
  202. /// DES加密字符串
  203. /// </summary>
  204. /// <param name="encryptString">待加密的字符串</param>
  205. /// <param name="encryptKey">加密密钥,要求为8位</param>
  206. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  207. public static string EncryptDES(string encryptString, string encryptKey)
  208. {
  209. try
  210. {
  211. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  212. byte[] rgbIV = Keys;
  213. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  214. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  215. MemoryStream mStream = new MemoryStream();
  216. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey,
  217. rgbIV), CryptoStreamMode.Write);
  218. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  219. cStream.FlushFinalBlock();
  220. return Convert.ToBase64String(mStream.ToArray());
  221. }
  222. catch
  223. {
  224. return encryptString;
  225. }
  226. }
  227. /**//**//**//**//**//**//**//// <summary>
  228. /// DES解密字符串
  229. /// </summary>
  230. /// <param name="decryptString">待解密的字符串</param>
  231. /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
  232. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  233. public static string DecryptDES(string decryptString, string decryptKey)
  234. {
  235. try
  236. {
  237. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
  238. byte[] rgbIV = Keys;
  239. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  240. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  241. MemoryStream mStream = new MemoryStream();
  242. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,
  243. rgbIV), CryptoStreamMode.Write);
  244. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  245. cStream.FlushFinalBlock();
  246. return Encoding.UTF8.GetString(mStream.ToArray());
  247. }
  248. catch
  249. {
  250. return decryptString;
  251. }
  252. }
  253. }
  254. }

[转]C#加密解密源码相关推荐

  1. php字符串加密解密源码,PHP中加密解密字符串函数源代码

    PHP中加密解密字符串函数源代码: /** *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ / ...

  2. DES 算法加密/解密源码

    //复制粘贴即可使用 class Program{static void Main(string[] args){string value = null;string jeiguo = null;wh ...

  3. java aes cfb 256_aes加密解密源码,包括aes128、aes192、aes256位,以及cbc、cfb、ecb、ofb、pcbc模式...

    AES加解密总共有以下这些 算法/模式/填充                 字节加密后数据长度       不满16字节加密后长度 AES/CBC/NoPadding                 ...

  4. C# Md5与AES加密解密源码记录

    1.AES 加密32个x是解密密钥 AES_k 是公钥.EncryptByAES方法和De方法都需要用到 public static string EncryptByAES(string input, ...

  5. 企业级程序苏林加密系统源码/PHP加密程序源码

    本资源可免费获取,请至尾部读阅! 企业级程序苏林加密系统源码,一款PHP加密程序源码,支持sg11加密.xend加密.goto加密.Leave加密.enphp加密.NoName加密. 可以发展用户,可 ...

  6. WoShop跨境电商国际支付Paypal支付商城全开源无加密商城源码

    WoShop跨境电商国际支付Paypal支付商城全开源无加密商城源码 现在的企业都喜欢直接用源码进行开发,特别是在跨境电商版块这一行,很多企业都是直接购买跨境电商系统源码再进行一个二次开发,从而给自己 ...

  7. WoShop跨境电商USDT支付语言插件全开源无加密商城源码

    WoShop跨境电商USDT支付语言插件全开源无加密商城源码 基于现场直播+购物模式,用户可以"边看边买"现场直播商城平台,全终端支持,统一管理后台,传播更强,管理更方便,支持私有 ...

  8. WoShop分销积分直播短视频商城全开源无加密商城源码

    WoShop分销积分直播短视频商城全开源无加密商城源码 随着分销积分直播短视频商城的市场走向兴盛,不止直播电商系统的使用越来越广泛,寻求分销积分直播短视频商城源码的人也越来越多.但源码市场混乱,价格. ...

  9. 卡b卡社区在线下单系统无加密全套源码

    介绍: 卡b卡社区在线下单系统无加密全套源码安装说明 把本程序上传到服务器或者主机空间 然后打开域名/install 配置数据库 数据库配置完成后打开域名即可! 后台地址:/admin 账号:2281 ...

  10. WoShop多商户直播短视频APP小程序商城全开源无加密商城源码

    WoShop多商户直播短视频APP小程序商城全开源无加密商城源码 基于现场直播+购物模式,用户可以"边看边买"现场直播商城平台,全终端支持,统一管理后台,传播更强,管理更方便,支持 ...

最新文章

  1. python asyncio回调函数_最近用 Python 的 asyncio,有好多不懂。。
  2. shell脚本中$#、$0、$@等特殊变量的含义
  3. python鼠标事件 详解_Python selenium键盘鼠标事件实现过程详解
  4. jsp超链接到java文件,jsp页面超链接传中文终极解决办法
  5. 用线程实现动态改变图标
  6. ubuntu镜像添加jdk_docker基础镜像ubuntu添加jdk1.8
  7. UI登陆页面素材|让设计师在竞争中脱颖而出
  8. 打开浏览器不是主页_浏览器首页被篡改!教你几个快速解决的方法
  9. Spring5的几个新特性
  10. 入门系列之基于MATLAB的滚动轴承内外圈复合线性剥落故障动力学建模
  11. 【c语言】计算圆周率的近似值
  12. 龙芯3A3000上实现BLFS的轻量级桌面LXDE
  13. localhost和127.0.0.1的区别
  14. 微软edge浏览器安装包下载地址-Microsoft edge download
  15. 2.2 电阻的串联和并联
  16. 芯片组:北桥芯片和南桥芯片
  17. 基于MATLAB的声信号的采集与分析,基于Matlab的声音信号采集与分析处理
  18. 软考系统集成项目管理工程师视频教程(计算机系统集成部分)-乔俊峰-专题视频课程...
  19. Win CE 添加微软自带拼音输入法
  20. 右键拖动调出BandZip的快捷菜单

热门文章

  1. IS-IS详解(二)——IS-IS邻居建立
  2. 中国HBase技术社区第十届meetup--HBase生态实践 (杭州站)...
  3. 技术解读Rainbond ServiceMesh微服务架构_开源PaaS Rainbond
  4. 颠覆大数据分析之结论
  5. socket编程:简单的TCP客户端
  6. AMD Fusion 开发者峰会透漏的信息
  7. Linux下python执行Killed
  8. div 中的i标签如何点击事件_前端优化:语义标签进化史
  9. 开关造成的毛刺_解决交易中的毛刺问题,你可以这样做
  10. docker容器打包成镜像