1、MD5加密

 public static string EncryptString(string source)
{
string result;
if (source == string.Empty || source == null)
{
result = string.Empty;
}
else
{
Encoding utf = new UTF8Encoding();
byte[] bytes = utf.GetBytes(source);
MD5 md5 = new MD5CryptoServiceProvider();
result = Convert.ToBase64String(md5.ComputeHash(bytes));
}
return result;
}

2、 SHA_1加密

 public static string EncryptString(string source)
{
string result;
if (source == "" || source == string.Empty || source == null)
{
result = source;
}
else
{
Encoding utf = new UTF8Encoding();
byte[] bytes = utf.GetBytes(source);
SHA1 sha = new SHA1CryptoServiceProvider();
result = Convert.ToBase64String(sha.ComputeHash(bytes));
}
return result;
}

3、对称加密示例

 private static readonly string key = "sajdkfj";

         private static readonly byte[] iv = new byte[]
{
,
,
,
,
,
,
, }; public static string EncryptString(string source)
{
string result;
if (source == string.Empty || source == null)
{
result = source;
}
else
{
Encoding utf = new UTF8Encoding();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform encrpt = des.CreateEncryptor(utf.GetBytes(SymmetricEncryption.key), SymmetricEncryption.iv);
byte[] byteMessage = utf.GetBytes(source);
byte[] byteEncrpt = encrpt.TransformFinalBlock(byteMessage, , byteMessage.Length);
result = Convert.ToBase64String(byteEncrpt);
}
return result;
} public static string DecryptString(string source)
{
Encoding utf = new UTF8Encoding();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
ICryptoTransform decrpt = des.CreateDecryptor(utf.GetBytes(SymmetricEncryption.key), SymmetricEncryption.iv);
byte[] byteEncrypt = Convert.FromBase64String(source);
byte[] byteDecrypt = decrpt.TransformFinalBlock(byteEncrypt, , byteEncrypt.Length);
return utf.GetString(byteDecrypt);
}

4、非对称加密

 public const string PUBLIC_KEY = "sdfasdfjsldkjfljLKjlkjaslkdj";

         public void RSAKey(out string privateKeys, out string publicKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
privateKeys = rsa.ToXmlString(true);
publicKey = rsa.ToXmlString(false);
} public string RSAEncrypt(string publicKey, string strEncryptString)
{
byte[] plainTextBArray = new UnicodeEncoding().GetBytes(strEncryptString);
return this.RSAEncrypt(publicKey, plainTextBArray);
} public string RSAEncrypt(string publicKey, byte[] encryptString)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
int keySize = rsa.KeySize / ;
int bufferSize = keySize - ;
byte[] buffer = new byte[bufferSize];
MemoryStream msInput = new MemoryStream(encryptString);
MemoryStream msOutput = new MemoryStream();
for (int readLen = msInput.Read(buffer, , bufferSize); readLen > ; readLen = msInput.Read(buffer, , bufferSize))
{
byte[] dataToEnc = new byte[readLen];
Array.Copy(buffer, , dataToEnc, , readLen);
byte[] encData = rsa.Encrypt(dataToEnc, false);
msOutput.Write(encData, , encData.Length);
}
msInput.Close();
byte[] cypherTextBArray = msOutput.ToArray();
msOutput.Close();
rsa.Clear();
return Convert.ToBase64String(cypherTextBArray);
} public string RSADecrypt(string privateKey, string strDecryptString)
{
byte[] plainTextBArray = Convert.FromBase64String(strDecryptString);
return this.RSADecrypt(privateKey, plainTextBArray);
} public string RSADecrypt(string privateKey, byte[] decryptString)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
int keySize = rsa.KeySize / ;
byte[] buffer = new byte[keySize];
MemoryStream msInput = new MemoryStream(decryptString);
MemoryStream msOutput = new MemoryStream();
for (int readLen = msInput.Read(buffer, , keySize); readLen > ; readLen = msInput.Read(buffer, , keySize))
{
byte[] dataToDec = new byte[readLen];
Array.Copy(buffer, , dataToDec, , readLen);
byte[] decData = rsa.Decrypt(dataToDec, false);
msOutput.Write(decData, , decData.Length);
}
msInput.Close();
byte[] dypherTextBArray = msOutput.ToArray();
msOutput.Close();
rsa.Clear();
return new UnicodeEncoding().GetString(dypherTextBArray);
} public bool GetHash(string strSource, ref byte[] HashData)
{
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
byte[] Buffer = Encoding.GetEncoding("GB2312").GetBytes(strSource);
HashData = MD5.ComputeHash(Buffer);
return true;
} public string GetHash(string strSource)
{
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
byte[] Buffer = Encoding.GetEncoding("GB2312").GetBytes(strSource);
byte[] HashData = MD5.ComputeHash(Buffer);
return Convert.ToBase64String(HashData);
} public void SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, out byte[] EncryptedSignatureData)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("MD5");
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
} public void SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, out string m_strEncryptedSignatureData)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("MD5");
byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
} public void SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, out byte[] EncryptedSignatureData)
{
byte[] HashbyteSignature = Convert.FromBase64String(this.GetHash(m_strHashbyteSignature));
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("MD5");
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
} public void SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, out string m_strEncryptedSignatureData)
{
byte[] HashbyteSignature = Convert.FromBase64String(this.GetHash(m_strHashbyteSignature));
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
RSAFormatter.SetHashAlgorithm("MD5");
byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
} public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("MD5");
return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
} public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
{
byte[] HashbyteDeformatter = Convert.FromBase64String(this.GetHash(p_strHashbyteDeformatter));
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("MD5");
return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
} public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("MD5");
byte[] DeformatterData = Convert.FromBase64String(p_strDeformatterData);
return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
} public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
{
byte[] HashbyteDeformatter = Convert.FromBase64String(this.GetHash(p_strHashbyteDeformatter));
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
RSADeformatter.SetHashAlgorithm("MD5");
byte[] DeformatterData = Convert.FromBase64String(p_strDeformatterData);
return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
}

C# 常用的加密代码参考的更多相关文章

  1. Java常用的加密解密类(对称加密类)

    Java常用的加密解密类 原文转载至:http://blog.csdn.net/wyc_cs/article/details/8793198 原创 2013年04月12日 14:33:35 1704 ...

  2. 常用 Java 静态代码分析工具的分析与比较

    常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基 本概念及主要技术,随后分别介绍了现有 4 种主流 Java 静态代码分析工具 (Checkstyle,FindBu ...

  3. 【转载】常用 Java 静态代码分析工具的分析与比较

    摘自:http://www.oschina.net/question/129540_23043常用 Java 静态代码分析工具的分析与比较 简介: 本文首先介绍了静态代码分析的基本概念及主要技术,随后 ...

  4. Proguard语法及常用proguard.cfg代码段

    本文主要ProGuard常用语法.标准proguard.cfg文件内容.常用proguard.cfg代码段及proguard与log level结合解决debug模式日志问题. 1.ProGuard的 ...

  5. C#开发中常用的加密解密方法

    转载自:https://www.cnblogs.com/bj981/p/11203711.html C#开发中常用的加密解密方法 相信很多人在开发过程中经常会遇到需要对一些重要的信息进行加密处理,今天 ...

  6. Jquery 代码参考

    jquery 代码参考 jQuery(document).ready(function($){}); jQuery(window).on('load', function(){}); $('.vide ...

  7. 十五个常用的jquery代码段【转】

    好的文章顶一个 回到顶部按钮 通过使用 jQuery 中的 animate 和 scrollTop 方法,你无需插件便可创建一个简单地回到顶部动画: 1 // Back to top 2 $('a.t ...

  8. 十五个常用的jquery代码段

    十五个常用的jquery代码段 回到顶部按钮 通过使用 jQuery 中的 animate 和 scrollTop 方法,你无需插件便可创建一个简单地回到顶部动画: 1 // Back to top ...

  9. 关于win2008r2上配置iis,出现加密代码与联邦基础加密冲突的问题的解决

    在win2008r2上配置asp.net网站时,出现这个问题: This implementation is not part of the Windows Platform FIPS validat ...

随机推荐

  1. 手机端table表格bug

    table表格在手机端有一个小小的bug,就是td有一个右边线,解决办法可已给tr加一个背景色就行,或者table都行,完美解决

  2. SAS 输入与输出格式

    SAS 输入与输出格式 一.认识SAS中的数据格式 SAS 中的格式有: 数字型 字符型 日期型 1.其中数字型的格式有一下集中表示方式: 整型数值:321 浮点数值:321.123 带逗号的数值:1 ...

  3. ABP框架系列之三十四:(Multi-Tenancy-多租户)

    What Is Multi Tenancy? "Software Multitenancy refers to a software architecture in which a sing ...

  4. 做到让DBCP连接池不超时

    前些天部署了一个项目,但每次隔一段时间打开都会报如下所示的错误:  javax.servlet.ServletException: org.springframework.transaction.Ca ...

  5. python基本数据类型之集合

    python基本数据类型之集合 集合是一种容器,用来存放不同元素. 集合有3大特点: 集合的元素必须是不可变类型(字符串.数字.元组): 集合中的元素不能重复: 集合是无序的. 在集合中直接存入lis ...

  6. Python json和pickle模块

    用于序列化的两个模块 json,用于字符串 和 python数据类型间进行转换 pickle,用于python特有的类型 和 python的数据类型间进行转换 Json模块提供了四个功能:dumps. ...

  7. String str.trim()

    String.trim() 方法不仅仅是去除字符串两端的空格字符,它能去除25种字符: ('/t', '/n', '/v', '/f', '/r', ' ', '/x0085', '/x00a0', ...

  8. 深度学习框架caffe/CNTK/Tensorflow/Theano/Torch的对比

    在单GPU下,所有这些工具集都调用cuDNN,因此只要外层的计算或者内存分配差异不大其性能表现都差不多. Caffe: 1)主流工业级深度学习工具,具有出色的卷积神经网络实现.在计算机视觉领域Caff ...

  9. RabbitMQ与消息总线

    Windows环境安装RabbitMQ,https://www.cnblogs.com/xibei666/p/5931267.html 1.消息发送流程 using System; using Sys ...

  10. vue 自学笔记(七) 组件细节问题

    前情提要: 这里盘点一下,组件细节的问题 现在我们观察一些用框架开发的网页BiliBili.掘金,会发现很多部分都十分相似或者一模一样,我们甚至可以将其拆分归类.而事实上,页面的确是被一个个组件构成的 ...