1、c#net rsa证书签名加密方aesdes3desrar压缩安全策略关系using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using Syste
2、m.Security.Cryptography;using System.Security.Cryptography.X509Certificates;using System.Collections.Generic;using System.Text;using System.IO;using System.IO.Compression;namespace asap/ / asap应用类,包含签名/验签,压缩/解压缩,加密/解密,字符类型转换/ public classasapApp#region 参数类型转换/ / 获取字节值,将字符型转换成字节型【strData:原字符;TypeId编码
3、类型1:Base64编码, 2:UTF8编码, 3:ASCII编码】/ public static byte CodingToByte(string strData, int TypeId)trybyte byteData;switch (TypeId)case 1:byteData = Convert.FromBase64String(strData);break;case 2:byteData = System.Text.Encoding.UTF8.GetBytes(strData);break;default:System.Text.ASCIIEncoding ByteConverter
4、 = new ASCIIEncoding();byteData = ByteConverter.GetBytes(strData);break;return byteData;catchbyte bnull=;return bnull;/ / 获取字符值, 将字节型转换成字符型【byteData:原字节;TypeId编码类型1:Base64编码, 2:UTF8编码, 3:ASCII编码】/ public static string CodingToString(byte byteData, int TypeId)trystring stringData;switch (TypeId)case
5、1:stringData = System.Convert.ToBase64String(byteData);break;case 2:stringData = System.Text.Encoding.UTF8.GetString(byteData);break;default:System.Text.ASCIIEncoding ByteConverter = new ASCIIEncoding();stringData = ByteConverter.GetString(byteData);break;return stringData;catchstring bnull =;return
6、 bnull;#endregion#region 随机key:64位、128位、192位/ / 获取随机key 【BitType:位数传入1:64位/8个字符;传入2:128位/16个字符;传入3:192位/24个字符;】/ public static byte randomKey(int BitType)trystring typeName = ;int typeSize = 0;if (BitType = 1)typeName = DES;typeSize = 64;if (BitType = 2)typeName = TripleDES;typeSize = 128;if (BitTyp
7、e = 3)typeName = 3DES;typeSize = 192;SymmetricAlgorithm symmProvider = SymmetricAlgorithm.Create(typeName);symmProvider.KeySize = typeSize;byte bkey = symmProvider.Key;return bkey;catchbyte bnull = ;return bnull;#endregion#region GZip压缩/解压缩字符串/ / GZip压缩函数(支持版本:2.0)/ public static string CompressGZip
8、(byte strSource)tryif (strSource = null)throw new System.ArgumentException(字符串为空!);/byte buffer = System.Text.Encoding.UTF8.GetBytes(strSource);byte buffer = strSource;System.IO.MemoryStream ms = new System.IO.MemoryStream();System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStr
9、eam(ms, System.IO.Compression.CompressionMode.Compress, true);stream.Write(buffer, 0, buffer.Length);stream.Close();byte buffer1 = ms.ToArray();ms.Close();return Convert.ToBase64String(buffer1, 0, buffer1.Length); /将压缩后的byte转换为Base64Stringcatchstring bnull = ;return bnull;/ / Gzip解压缩函数(支持版本:2.0)【参数s
10、trSource:原文;codeType:指定编码类型,值1:Base64,值2:UTF8】/ public static string DecompressGZip(string strSource,int codeType)tryif (strSource = null)throw new System.ArgumentException(字符串不能为空!);byte buffer = Convert.FromBase64String(strSource);System.IO.MemoryStream ms = new System.IO.MemoryStream();ms.Write(b
11、uffer, 0, buffer.Length);ms.Position = 0;System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);stream.Flush();int nSize = 6000 * 1024 + 256; /字符串不超过6000Kbyte decompressBuffer = new bytenSize;int nSizeIncept = stream.Read(
12、decompressBuffer, 0, nSize);stream.Close();string backStr = ;if (codeType = 1)backStr = System.Convert.ToBase64String(decompressBuffer, 0, nSizeIncept);if (codeType = 2)backStr = System.Text.Encoding.UTF8.GetString(decompressBuffer, 0, nSizeIncept);ms.Close();return backStr;/转换为普通的字符串catchstring bnu
13、ll = ;return bnull;#endregion#region 签名算法及摘要#region MD5签名/ / MD5签名,然后返回string类型签名数据(1:要签名的参数,2:编码方式)。/ public static string MD5Signature(string dataStr, string codeType)trySystem.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();byte t = md5.ComputeHash(Syst
14、em.Text.Encoding.GetEncoding(codeType).GetBytes(dataStr);System.Text.StringBuilder sb = new System.Text.StringBuilder(32);for (int i = 0; i t.Length; i+)sb.Append(ti.ToString(x).PadLeft(2, 0);return sb.ToString();catchstring bnull = ;return bnull;#endregion#region MD5/SHA1摘要/ / MD5摘要,然后返回byte类型摘要数据。
15、/ public static byte MD5summary(string strSource)trybyte bSource = System.Text.Encoding.UTF8.GetBytes(strSource);MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();byte result = md5.ComputeHash(bSource);/摘要值/string backStr = Convert.ToBase64String(result);return result;catchbyte bnull = ;
16、return bnull;/ / SHA1摘要,然后返回byte类型摘要数据。/ public static byte SHA1summary(string strSource)trybyte bSource = System.Text.Encoding.UTF8.GetBytes(strSource);SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();byte HashData = sha.ComputeHash(bSource);/string backStr = Convert.ToBase64String(H
17、ashData);return HashData;catchbyte bnull = ;return bnull;#endregion#region 引用证书非对称签名/验签RSA/ / 引用证书非对称签名/验签RSA-私钥签名【OriginalString:原文(有中文用utf-8编码的字节);prikey_path:证书路径;CertificatePW:证书密码;SignType:签名摘要类型(1:MD5,2:SHA1)】/ public static byte CerRSASignature(byte OriginalString, string prikey_path, string
18、CertificatePW, int SignType)tryX509Certificate2 x509_Cer1 = new X509Certificate2(prikey_path, CertificatePW);RSACryptoServiceProvider rsapri = (RSACryptoServiceProvider)x509_Cer1.PrivateKey;RSAPKCS1SignatureFormatter f = new RSAPKCS1SignatureFormatter(rsapri);byte result;switch (SignType)case 1:f.Se
19、tHashAlgorithm(MD5);/摘要算法MD5MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();result = md5.ComputeHash(OriginalString);/摘要值break;default:f.SetHashAlgorithm(SHA1);/摘要算法SHA1SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();result = sha.ComputeHash(OriginalString);/摘要值break;by
20、te SignData = f.CreateSignature(result);return SignData;catchbyte bnull = ;return bnull;/ / 引用证书非对称签名/验签RSA-公钥验签【OriginalString:原文(有中文用utf-8编码的字节);SignatureString:签名字符;pubkey_path:证书路径;CertificatePW:证书密码;SignType:签名摘要类型(1:MD5,2:SHA1)】/ public static bool CerRSAVerifySignature(byte OriginalString, by
21、te SignatureString, string pubkey_path, string CertificatePW, int SignType)tryX509Certificate2 x509_Cer1 = new X509Certificate2(pubkey_path, CertificatePW);RSACryptoServiceProvider rsapub = (RSACryptoServiceProvider)x509_Cer1.PublicKey.Key;rsapub.ImportCspBlob(rsapub.ExportCspBlob(false);RSAPKCS1Sig
22、natureDeformatter f = new RSAPKCS1SignatureDeformatter(rsapub);byte HashData;switch (SignType)case 1:f.SetHashAlgorithm(MD5);/摘要算法MD5MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();HashData = md5.ComputeHash(OriginalString);break;default:f.SetHashAlgorithm(SHA1);/摘要算法SHA1SHA1CryptoServ
23、iceProvider sha = new SHA1CryptoServiceProvider();HashData = sha.ComputeHash(OriginalString);break;if (f.VerifySignature(HashData, SignatureString)return true;elsereturn false;catchreturn false ;#endregion#region 引用证书非对称加密/解密RSA/ / 引用证书非对称加密/解密RSA-公钥加密获取密文【DataToEncrypt:原文(有中文用utf-8编码的字节);pubkey_pat
24、h:证书路径;CertificatePW:证书密码】/ public static byte CerRSAEncrypt(byte DataToEncrypt, string pubkey_path, string CertificatePW)tryX509Certificate2 x509_Cer1 = new X509Certificate2(pubkey_path, CertificatePW);RSACryptoServiceProvider rsapub = (RSACryptoServiceProvider)x509_Cer1.PublicKey.Key;byte bytes_Cy
25、pher_Text = rsapub.Encrypt(DataToEncrypt, false);return bytes_Cypher_Text;catchbyte bnull = ;return bnull;/ / 引用证书非对称加密/解密RSA-私钥解密获取原文【DataToDecrypt:密文;prikey_path:证书路径;CertificatePW:证书密码】【原文有中文返回字节用utf-8编码转换成字符】/ public static byte CerRSADecrypt(byte DataToDecrypt, string prikey_path, string Certif
26、icatePW)tryX509Certificate2 x509_Cer2 = new X509Certificate2(prikey_path, CertificatePW);RSACryptoServiceProvider rsapri = (RSACryptoServiceProvider)x509_Cer2.PrivateKey;byte bytes_Plain_Text = rsapri.Decrypt(DataToDecrypt, false);return bytes_Plain_Text;catchbyte bnull = ;return bnull;#endregion#re
27、gion Des 64Bit 对称加密/解密算法/ / Des 64Bit 算法加密字串【DataToEncrypt:原文;ekey:随机密码,8个字符】【原文有中文传入的原文字符用utf-8编码转换成字节】/ public static byte DesEncrypt(byte DataToEncrypt, byte ekey)trySymmetricAlgorithm mCSP = new DESCryptoServiceProvider();ICryptoTransform ct;MemoryStream ms;CryptoStream cs;/向量指定的是:byte eiv =0,0,
28、0,0,0,0,0,0;byte eiv = CodingToByte(AAAAAAAAAAA=, 1);ct = mCSP.CreateEncryptor(ekey, eiv);ms = new MemoryStream();cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);cs.Write(DataToEncrypt, 0, DataToEncrypt.Length);cs.FlushFinalBlock();cs.Close();return ms.ToArray();catchbyte bnull = ;return bnull;/ / Des 64Bit 算法解密字串【DataToDecrypt:密文;ekey:随机密码,8个字符】【原文有中文返回字节用utf-8编码转换成字符】/ public static byte DesDecrypt(byte DataToDecrypt, byte
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1