using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace ApplyTest
{
public class RSAEncryption
{
///
/// 生成公私钥
///
///
///
public void RSAKey(string PrivateKeyPath, string PublicKeyPath)
{
try
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
this.CreatePrivateKeyXML(PrivateKeyPath, provider.ToXmlString(true));
this.CreatePublicKeyXML(PublicKeyPath, provider.ToXmlString(false));
}
catch (Exception exception)
{
throw exception;
}
}
///
/// 对原始数据进行MD5加密
///
/// 待加密数据
/// 返回机密后的数据
public string GetHash(string m_strSource)
{
HashAlgorithm algorithm = HashAlgorithm.Create("MD5");
byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
byte[] inArray = algorithm.ComputeHash(bytes);
return Convert.ToBase64String(inArray);
}
///
/// RSA加密
///
/// 公钥
/// MD5加密后的数据
/// RSA公钥加密后的数据
public string RSAEncrypt(string xmlPublicKey, string m_strEncryptString)
{
string str2;
try
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPublicKey);
byte[] bytes = new UnicodeEncoding().GetBytes(m_strEncryptString);
str2 = Convert.ToBase64String(provider.Encrypt(bytes, false));
}
catch (Exception exception)
{
throw exception;
}
return str2;
}
///
/// RSA解密
///
/// 私钥
/// 待解密的数据
/// 解密后的结果
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString)
{
string str2;
try
{
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPrivateKey);
byte[] rgb = Convert.FromBase64String(m_strDecryptString);
byte[] buffer2 = provider.Decrypt(rgb, false);
str2 = new UnicodeEncoding().GetString(buffer2);
}
catch (Exception exception)
{
throw exception;
}
return str2;
}
///
/// 对MD5加密后的密文进行签名
///
/// 私钥
/// MD5加密后的密文
///
public string SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature)
{
byte[] rgbHash = Convert.FromBase64String(m_strHashbyteSignature);
RSACryptoServiceProvider key = new RSACryptoServiceProvider();
key.FromXmlString(p_strKeyPrivate);
RSAPKCS1SignatureFormatter formatter = new RSAPKCS1SignatureFormatter(key);
formatter.SetHashAlgorithm("MD5");
byte[] inArray = formatter.CreateSignature(rgbHash);
return Convert.ToBase64String(inArray);
}
///
/// 签名验证
///
/// 公钥
/// 待验证的用户名
/// 注册码
///
public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
{
try
{
byte[] rgbHash = Convert.FromBase64String(p_strHashbyteDeformatter);
RSACryptoServiceProvider key = new RSACryptoServiceProvider();
key.FromXmlString(p_strKeyPublic);
RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
deformatter.SetHashAlgorithm("MD5");
byte[] rgbSignature = Convert.FromBase64String(p_strDeformatterData);
if (deformatter.VerifySignature(rgbHash, rgbSignature))
{
return true;
}
return false;
}
catch
{
return false;
}
}
///
/// 获取硬盘ID
///
/// 硬盘ID
public string GetHardID()
{
string HDInfo = "";
ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo in moc1)
{
HDInfo = (string)mo.Properties["Model"].Value;
}
return HDInfo;
}
///
/// 读注册表中指定键的值
///
/// 键名
/// 返回键值
private string ReadReg(string key)
{
string temp = "";
try
{
RegistryKey myKey = Registry.LocalMachine;
RegistryKey subKey = myKey.OpenSubKey(@"SOFTWARE/JX/Register");
temp = subKey.GetValue(key).ToString();
subKey.Close();
myKey.Close();
return temp;
}
catch (Exception)
{
throw;//可能没有此注册项;
}
}
///
/// 创建注册表中指定的键和值
///
/// 键名
/// 键值
private void WriteReg(string key, string value)
{
try
{
RegistryKey rootKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/JX/Register");
rootKey.SetValue(key, value);
rootKey.Close();
}
catch (Exception)
{
throw;
}
}
///
/// 创建公钥文件
///
///
///
public void CreatePublicKeyXML(string path, string publickey)
{
try
{
FileStream publickeyxml = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(publickeyxml);
sw.WriteLine(publickey);
sw.Close();
publickeyxml.Close();
}
catch
{
throw;
}
}
///
/// 创建私钥文件
///
///
///
public void CreatePrivateKeyXML(string path, string privatekey)
{
try
{
FileStream privatekeyxml = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(privatekeyxml);
sw.WriteLine(privatekey);
sw.Close();
privatekeyxml.Close();
}
catch
{
throw;
}
}
///
/// 读取公钥
///
///
///
public string ReadPublicKey(string path)
{
StreamReader reader = new StreamReader(path);
string publickey = reader.ReadToEnd();
reader.Close();
return publickey;
}
///
/// 读取私钥
///
///
///
public string ReadPrivateKey(string path)
{
StreamReader reader = new StreamReader(path);
string privatekey = reader.ReadToEnd();
reader.Close();
return privatekey;
}
///
/// 初始化注册表,程序运行时调用,在调用之前更新公钥xml
///
/// 公钥路径
public void InitialReg(string path)
{
Registry.LocalMachine.CreateSubKey(@"SOFTWARE/JX/Register");
Random ra = new Random();
string publickey = this.ReadPublicKey(path);
if (Registry.LocalMachine.OpenSubKey(@"SOFTWARE/JX/Register").ValueCount <= 0)
{
this.WriteReg("RegisterRandom", ra.Next(1, 100000).ToString());
this.WriteReg("RegisterPublicKey", publickey);
}
else
{
this.WriteReg("RegisterPublicKey", publickey);
}
}
}
}