:MD5 不可逆加密
2:Des对称可逆加密
3:RSA非对称可逆加密
4:数字证书 SSL

                  Anker_张(博客园)http://www.cnblogs.com/AnkerZhang/

1:MD5 不可逆加密

using System.IO;
using System.Security.Cryptography;
using System.Text; namespace EncryptDemo
{
/// <summary>
/// 不可逆加密
/// 1 防止被篡改
/// 2 防止明文存储
/// 3 防止抵赖,数字签名
/// </summary>
public class MD5Encrypt
{
#region MD5
/// <summary>
/// MD5加密,和动网上的16/32位MD5加密结果相同,
/// 使用的UTF8编码
/// </summary>
/// <param name="strSource">待加密的字符串</param>
/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
/// <returns>加密后的字串</returns>
public static string Encrypt(string source, int length = )//默认参数
{
HashAlgorithm provider = CryptoConfig.CreateFromName("MD5") as HashAlgorithm;
if (string.IsNullOrEmpty(source)) return string.Empty; byte[] bytes = Encoding.UTF8.GetBytes(source);// Encoding.ASCII.GetBytes(source);
byte[] hashValue = provider.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
switch (length)
{
case ://16位密文是32位密文的9到24位字符
for (int i = ; i < ; i++)
sb.Append(hashValue[i].ToString("x2"));
break;
case :
for (int i = ; i < ; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
default:
for (int i = ; i < hashValue.Length; i++)
{
sb.Append(hashValue[i].ToString("x2"));
}
break;
}
return sb.ToString();
}
/// <summary>
/// MD5加密,和动网上的16/32位MD5加密结果相同,
/// 使用的UTF8编码
/// 为文件加密方法
/// </summary>
/// <param name="strSource">待加密的文件路径</param>
/// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param>
/// <returns>加密后的字串</returns>
public static string EncryptFile(string pathFile, int length = )//默认参数
{
using (FileStream fsRead = new FileStream(pathFile, FileMode.Open))
{
int fsLen = (int)fsRead.Length;
byte[] heByte = new byte[fsLen];
int r = fsRead.Read(heByte, , heByte.Length);
string myStr = System.Text.Encoding.UTF8.GetString(heByte);
return Encrypt(myStr, length);
}
}
#endregion MD5
}
}

2:Des对称可逆加密

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text; namespace EncryptDemo
{
/// <summary>
/// DES AES Blowfish
///  对称加密算法的优点是速度快,
///  缺点是密钥管理不方便,要求共享密钥。
/// 可逆对称加密 密钥长度8
/// </summary>
public class DesEncrypt
{
//8位长度
private static string KEY = "Anker_张1";
private static byte[] rgbKey = ASCIIEncoding.ASCII.GetBytes(KEY.Substring(, ));
private static byte[] rgbIV = ASCIIEncoding.ASCII.GetBytes(KEY.Insert(, "Z").Substring(, ));
/// <summary>
/// DES 加密
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static string Encrypt(string strValue)
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
using (MemoryStream memStream = new MemoryStream())
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
StreamWriter sWriter = new StreamWriter(crypStream);
sWriter.Write(strValue);
sWriter.Flush();
crypStream.FlushFinalBlock();
memStream.Flush();
return Convert.ToBase64String(memStream.GetBuffer(), , (int)memStream.Length);
}
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="EncValue"></param>
/// <returns></returns>
public static string Decrypt(string EncValue)
{
DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
byte[] buffer = Convert.FromBase64String(EncValue); using (MemoryStream memStream = new MemoryStream())
{
CryptoStream crypStream = new CryptoStream(memStream, dsp.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
crypStream.Write(buffer, , buffer.Length);
crypStream.FlushFinalBlock();
return ASCIIEncoding.UTF8.GetString(memStream.ToArray());
}
}
}
}

3:RSA非对称可逆加密

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text; namespace EncryptDemo
{
/// <summary>
/// RSA ECC
/// 可逆非对称加密
/// 非对称加密算法的优点是密钥管理很方便,缺点是速度慢。
/// </summary>
public class RsaEncrypt
{
/// <summary>
/// publicKey:加密,privateKey解密
/// </summary>
/// <returns></returns>
public static KeyValuePair<string, string> GetKeyPair()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
string publicKey = RSA.ToXmlString(false);
string privateKey = RSA.ToXmlString(true);
return new KeyValuePair<string, string>(publicKey, privateKey);
}
/// <summary>
/// 加密
/// </summary>
/// <param name="content"></param>
/// <param name="publicKey">返还公钥</param>
/// <param name="privateKey">返回密钥</param>
/// <returns>加密后结果</returns>
public static void GetKey(out string publicKey, out string privateKey)
{
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
publicKey = rsaProvider.ToXmlString(false);
privateKey = rsaProvider.ToXmlString(true);
}
/// <summary>
/// 加密:内容+公钥
/// </summary>
/// <param name="content"></param>
/// <param name="publicKey"></param>
/// <returns></returns>
public static string Encrypt(string content, string publicKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] DataToEncrypt = ByteConverter.GetBytes(content);
byte[] resultBytes = rsa.Encrypt(DataToEncrypt, false);
return Convert.ToBase64String(resultBytes);
}
/// <summary>
/// 解密 内容+私钥
/// </summary>
/// <param name="content"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
public static string Decrypt(string content, string privateKey)
{
byte[] dataToDecrypt = Convert.FromBase64String(content);
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(privateKey);
byte[] resultBytes = RSA.Decrypt(dataToDecrypt, false);
UnicodeEncoding ByteConverter = new UnicodeEncoding();
return ByteConverter.GetString(resultBytes);
}
}
}

RSA非对称可逆加密原理

 4:数字证书 SSL

.Net中的加密解密:http://www.cnblogs.com/JimmyZhang/archive/2008/10/02/Cryptograph.html

MD5算法原理:http://blog.csdn.net/forgotaboutgirl/article/details/7258109

详情Des加密: http://www.iplaysoft.com/encrypt-arithmetic.html

详情RSA加密:http://www.iplaysoft.com/encrypt-arithmetic.html

SSL 与 数字证书 的基本概念和工作原理:http://blog.csdn.net/jhonguy/article/details/7577729

最新文章

  1. json中$ref对象重复引用问题
  2. [BZOJ2768][JLOI2010]冠军调查(最小割)
  3. dedecms后台批量替换文章中的关键词
  4. 09 高效的PL/SQL程序设计
  5. 针对SharePointFarm场时安装部署OWA的步骤
  6. JDK的帮助文档
  7. java-base64
  8. Asp.Net MVC4 + Oracle + EasyUI + Bootstrap
  9. 修改searchBar的返回按钮的显示文字
  10. extjs 倒计时
  11. Oracle 数据库禁止全表访问的时候direct path read /////
  12. SignalR学习笔记(三)Self-Host
  13. c++中求数组长度
  14. python之三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数
  15. Shell命令-文件及内容处理之wc,tr
  16. TCP UDP 数据包过大导致分片情况
  17. 8 张脑图入门 JavaScript - 基础面试不倒
  18. maven 3.5.2 修改java_home
  19. Java JNDI 学习
  20. Expert C Programming 阅读笔记(~CH1)

热门文章

  1. notHere 对框架解决方案的框架预期处理
  2. BZOJ1815: [Shoi2006]color 有色图
  3. 一起来学linux:压缩与解压缩
  4. 剑指Offer:链表中环的入口节点【23】
  5. 剑指Offer:删除链表的节点【18】
  6. ZOJ - 3861 Valid Pattern Lock 【全排列】
  7. Zookeeper四字命令
  8. (5)表单Action后台验证
  9. ES索引瘦身 禁用_source后需要设置field store才能获取数据 否则无法显示搜索结果
  10. ES BM25 TF-IDF相似度算法设置——