转帖http://www.cnblogs.com/yn-jiang/p/3844151.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.ComponentModel;
namespace ConsoleApplication2
{
class Class1
{
//创建了密钥对
static RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
private string privateKey = RSA.ToXmlString(true);
private string publicKey = RSA.ToXmlString(false);
static byte[] AOutput;
public static string GetKeyFromContainer(string ContainerName, bool privatekey)
{
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
return rsa.ToXmlString(privatekey);
}
//加密
public static string RSAEncrypt(string publicKey, string content)
{
RSACryptoServiceProvider Rsa = new RSACryptoServiceProvider();
Rsa.FromXmlString(publicKey);
byte[] cipherbytes = Rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
AOutput = Rsa.Encrypt(cipherbytes, false);
return Convert.ToBase64String(AOutput);
}
//解密
public static string RSADecrypt(string privateKey, string content)
{
RSACryptoServiceProvider Rsa2 = new RSACryptoServiceProvider();
Rsa2.FromXmlString(privateKey);
byte[] cipherbytes = Rsa2.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
//获取hash值
private byte[] GetHash(string content)
{
try
{
//FileStream objFile = File.OpenRead(filePath);
byte[] data_Bytes = Encoding.ASCII.GetBytes(content);
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
byte[] Hashbyte = MD5.ComputeHash(data_Bytes);
//objFile.Close();
return Hashbyte;
}
catch
{
return null;
}
throw new NotImplementedException();
}
//获取文件hash
public static byte[] GetFileHash(string filePath)
{
try
{
FileStream objFile = File.OpenRead(filePath);
HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
byte[] Hashbyte = MD5.ComputeHash(objFile);
objFile.Close();
return Hashbyte;
}
catch
{
return null;
}
}
//创建数字签名
byte[] EncryptHash(string privateKey, byte[] hash_Bytes)
{
RSACryptoServiceProvider Rsa3 = new RSACryptoServiceProvider();
Rsa3.FromXmlString(privateKey);
RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(Rsa3);
RSAFormatter.SetHashAlgorithm("MD5");
//AOutput = Rsa3.SignData(data_Bytes, "SHA1");
return RSAFormatter.CreateSignature(hash_Bytes); ;
}
//验证数字签名
public bool DecryptHash(string publicKey, byte[] hash_byte, byte[] eSignature)
{
try
{
RSACryptoServiceProvider Rsa4 = new RSACryptoServiceProvider();
Rsa4.FromXmlString(publicKey);
//bool bVerify = Rsa4.VerifyData(strData, "SHA1", AOutput);
RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(Rsa4);
RSADeformatter.SetHashAlgorithm("MD5");
bool bVerify = RSADeformatter.VerifySignature(hash_byte,eSignature);
if (bVerify)
{
return true;
}
return false;
}
catch (CryptographicException e)
{
return false;
}
}
// 将Byte[]转换成十六进制字符串
public static string ConvertBytesToString(byte[] bytes)
{
string bytestring = string.Empty;
if (bytes != null && bytes.Length > 0)
{
for (int i = 0; i < bytes.Length; i++)
{
bytestring += bytes[i].ToString("X") + " ";
}
}
return bytestring;
}
static void Main(string[] args)
{
//打开文件,或者是获取网页数据
Console.WriteLine("接收方收到电子文件。");
string strMsg = "dadfdfgdgdgagdgadg"; //test data
Console.WriteLine(strMsg);
Class1 cls = new Class1();
//对电子文件进行哈希
Console.WriteLine("哈希值:");
&nb