C#_完整的RSA操作类
生活随笔
收集整理的這篇文章主要介紹了
C#_完整的RSA操作类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.IO;namespace JXRSALibrary
{/// <summary>/// RSA操作類/// </summary>public class RSALibrary{/// <summary>/// 生成公私鑰/// </summary>/// <param name="PrivateKeyPath"></param>/// <param name="PublicKeyPath"></param>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;}}/// <summary>/// 對(duì)原始數(shù)據(jù)進(jìn)行MD5加密/// </summary>/// <param name="m_strSource">待加密數(shù)據(jù)</param>/// <returns>返回機(jī)密后的數(shù)據(jù)</returns>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);}/// <summary>/// RSA加密/// </summary>/// <param name="xmlPublicKey">公鑰</param>/// <param name="m_strEncryptString">MD5加密后的數(shù)據(jù)</param>/// <returns>RSA公鑰加密后的數(shù)據(jù)</returns>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;}/// <summary>/// RSA解密/// </summary>/// <param name="xmlPrivateKey">私鑰</param>/// <param name="m_strDecryptString">待解密的數(shù)據(jù)</param>/// <returns>解密后的結(jié)果</returns>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;}/// <summary>/// 對(duì)MD5加密后的密文進(jìn)行簽名/// </summary>/// <param name="p_strKeyPrivate">私鑰</param>/// <param name="m_strHashbyteSignature">MD5加密后的密文</param>/// <returns></returns>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);}/// <summary>/// 簽名驗(yàn)證/// </summary>/// <param name="p_strKeyPublic">公鑰</param>/// <param name="p_strHashbyteDeformatter">待驗(yàn)證的用戶名</param>/// <param name="p_strDeformatterData">注冊(cè)碼</param>/// <returns></returns>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;}}/// <summary>/// 獲取硬盤ID/// </summary>/// <returns>硬盤ID</returns>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;}/// <summary>/// 獲取CpuID/// </summary>/// <returns>CpuID</returns>private string GetCpuID(){string CpuInfo = "";ManagementClass cimobject = newManagementClass("Win32_Processor");ManagementObjectCollection moc = cimobject.GetInstances();foreach (ManagementObject mo in moc){CpuInfo = mo.Properties["ProcessorId"].Value.ToString();}return CpuInfo;}/// <summary>/// 讀注冊(cè)表中指定鍵的值/// </summary>/// <param name="key">鍵名</param>/// <returns>返回鍵值</returns>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;//可能沒有此注冊(cè)項(xiàng);}}/// <summary>/// 創(chuàng)建注冊(cè)表中指定的鍵和值/// </summary>/// <param name="key">鍵名</param>/// <param name="value">鍵值</param>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;}}/// <summary>/// 初始化注冊(cè)表,程序運(yùn)行時(shí)調(diào)用,在調(diào)用之前更新公鑰xml/// </summary>/// <param name="path">公鑰路徑</param>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);}}/// <summary>/// 獲取一個(gè)與指定值不同的隨機(jī)整數(shù)/// </summary>/// <param name="temp">指定值</param>/// <returns>隨機(jī)整數(shù)</returns>private int GetRandomNum(int temp){Random ra = new Random();while (true){int r = ra.Next(1, 100000);if (r == temp)continue;elsereturn r;}}/// <summary>/// 取得干擾字符/// </summary>/// <returns></returns>public string GetRegisterRandom(){return this.ReadReg("RegisterRandom");}/// <summary>/// 重置干擾字符/// </summary>public void SetRegisterRandom(){string r = this.ReadReg("RegisterRandom");WriteReg("RegisterRandom", GetRandomNum(int.Parse(r)).ToString());}/// <summary>/// 將公鑰寫入注冊(cè)表/// </summary>/// <param name="publickey">公鑰</param>public void WriteCodeToReg(string publickey){WriteReg("RegisterPublicKey", publickey);}/// <summary>/// 創(chuàng)建公鑰文件/// </summary>/// <param name="path"></param>/// <param name="publickey"></param>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;}}/// <summary>/// 創(chuàng)建私鑰文件/// </summary>/// <param name="path"></param>/// <param name="privatekey"></param>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;}}/// <summary>/// 讀取公鑰/// </summary>/// <param name="path"></param>/// <returns></returns>public string ReadPublicKey(string path){StreamReader reader = new StreamReader(path);string publickey = reader.ReadToEnd();reader.Close();return publickey;}/// <summary>/// 讀取私鑰/// </summary>/// <param name="path"></param>/// <returns></returns>public string ReadPrivateKey(string path){StreamReader reader = new StreamReader(path);string privatekey = reader.ReadToEnd();reader.Close();return privatekey;}}
}
轉(zhuǎn)載于:https://www.cnblogs.com/lijialong/archive/2010/07/12/RSA_D.html
總結(jié)
以上是生活随笔為你收集整理的C#_完整的RSA操作类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php中static和self的区别
- 下一篇: 指针数组 与 数组指针 的分析