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. The current state of generics in Delphi( 转载)

    The current state of generics in Delphi   To avoid duplication of generated code, the compiler build ...

  2. Linux学习---条件预处理的应用

    预处理的使用: ⑴包含头文件 #include ⑵宏定义 #define    替换,不进行语法检查 ①常量宏定义:#define 宏名 (宏体) (加括号为防止不进行语法检查而出现的错误) eg:# ...

  3. android 学习网站

    菜鸟教程 http://www.runoob.com/android/android-tutorial.html Android基础入门教程  http://www.runoob.com/w3cnot ...

  4. elasticsearch之hello(spring data整合)

    1.书写pom.xml文件 <dependencies> <dependency> <groupId>org.springframework.data</gr ...

  5. vscode调试angular

    之前在Asp.net MVC + Angular1 的项目中,要调试前台代码都是用浏览器的开发者工具,使用正常,也没有感觉太大的不方便. 后来接触Angular2项目,因为它是要经过编译的,所以在浏览 ...

  6. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)

    近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只考虑dotnet到dotnet的情形下,我们可以选择remoting.WCF(http).WCF(tcp).WCF(RESTful). ...

  7. Android 富文本编辑器实现方案

    本人实现富文本编辑器的时候,总结了如下两种方案: 1. 纯 EditText 实现方案 2. 使用ScrollView作为最外层的父容器来控制展示效果 示例demo地址为:https://github ...

  8. Docker - 国内镜像的配置及使用

    Docker国内镜像 DaoCloud - Docker加速器 阿里云 - 开发者平台 微镜像 - 希云cSphere 网易蜂巢 阿里云的Docker加速器 阿里云 - 开发者平台:https://d ...

  9. js 学习记录(一)

    前情提要: 本记录参照  <<javascript 入门导论>>  这本书学习 

  10. jquery选择器和属性

    jQuery的选择器 <html lang="en"> <head> <meta charset="UTF-8"> < ...