码迷,mamicode.com
首页 > 其他好文 > 详细

非对称加密、解密、验证辅助类 RSASecurityHelper

时间:2015-07-22 20:27:06      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

 

/// <summary>

/// 非对称加密生成的私钥和公钥

/// </summary>

/// <param name="privateKey">私钥</param>

/// <param name="publicKey">公钥</param>

public static void GenerateRSAKey(out string privateKey, out string publicKey)

 

#region 非对称数据加密(公钥加密)

 

/// <summary>

/// 非对称加密字符串数据,返回加密后的数据

/// </summary>

/// <param name="publicKey">公钥</param>

/// <param name="originalString">待加密的字符串</param>

/// <returns>加密后的数据</returns>

public static string RSAEncrypt(string publicKey, string originalString)

 

/// <summary>

/// 非对称加密字节数组,返回加密后的数据

/// </summary>

/// <param name="publicKey">公钥</param>

/// <param name="originalBytes">待加密的字节数组</param>

/// <returns>返回加密后的数据</returns>

public static string RSAEncrypt(string publicKey, byte[] originalBytes)

 

#endregion

 

#region 非对称解密(私钥解密)

 

/// <summary>

/// 非对称解密字符串,返回解密后的数据

/// </summary>

/// <param name="privateKey">私钥</param>

/// <param name="encryptedString">待解密数据</param>

/// <returns>返回解密后的数据</returns>

public static string RSADecrypt(string privateKey, string encryptedString)

 

/// <summary>

/// 非对称解密字节数组,返回解密后的数据

/// </summary>

/// <param name="privateKey">私钥</param>

/// <param name="encryptedBytes">待解密数据</param>

/// <returns></returns>

public static string RSADecrypt(string privateKey, byte[] encryptedBytes)

 

#endregion

 

#region 非对称加密签名、验证

 

/// <summary>

/// 使用非对称加密签名数据

/// </summary>

/// <param name="privateKey">私钥</param>

/// <param name="originalString">待加密的字符串</param>

/// <returns>加密后的数据</returns>

public static string RSAEncrypSignature(string privateKey, string originalString)

 

/// <summary>

/// 对私钥加密签名的字符串,使用公钥对其进行验证

/// </summary>

/// <param name="originalString">未加密的文本,如机器码</param>

/// <param name="encrytedString">加密后的文本,如注册序列号</param>

/// <returns>如果验证成功返回True,否则为False</returns>

public static bool Validate(string originalString, string encrytedString)

 

/// <summary>

/// 对私钥加密的字符串,使用公钥对其进行验证

/// </summary>

/// <param name="originalString">未加密的文本,如机器码</param>

/// <param name="encrytedString">加密后的文本,如注册序列号</param>

/// <param name="publicKey">非对称加密的公钥</param>

/// <returns>如果验证成功返回True,否则为False</returns>

public static bool Validate(string originalString, string encrytedString, string publicKey)

 

 

 

 

2

//生成加解密私钥、公钥

string publicKey = "";

string privateKey = "";

RSASecurityHelper.GenerateRSAKey(out privateKey, out publicKey);

 

string originalString = "testdata";

string encryptedString = RSASecurityHelper.RSAEncrypt(publicKey, originalString);

string originalString2 = RSASecurityHelper.RSADecrypt(privateKey, encryptedString);

if (originalString == originalString2)

{

MessageUtil.ShowTips("解密完全正确");

}

else

{

MessageUtil.ShowWarning("解密失败");

}

 

string regcode = RSASecurityHelper.RSAEncrypSignature(privateKey, originalString);

bool validated = RSASecurityHelper.Validate(originalString, regcode, publicKey);
MessageUtil.ShowTips( validated ? "验证成功" : "验证失败");

 

 

3

/// <summary>

/// 每次程序运行时候,检查用户是否注册

/// </summary>

/// <returns>如果用户已经注册, 那么返回True, 否则为False</returns>

public bool CheckRegister()

{

// 先获取用户的注册码进行比较

string serialNumber = string.Empty; //注册码

RegistryKey reg = Registry.CurrentUser.OpenSubKey(UIConstants.SoftwareRegistryKey, true);

if (null != reg)

{

serialNumber = (string)reg.GetValue("SerialNumber");

Portal.gc.bRegisted = Portal.gc.Register(serialNumber);

}

 

return Portal.gc.bRegisted;

}

 

/// <summary>

/// 调用非对称加密方式对序列号进行验证

/// </summary>

/// <param name="serialNumber">正确的序列号</param>

/// <returns>如果成功返回True,否则为False</returns>

public bool Register(String serialNumber)

{

string hardNumber = HardwareInfoHelper.GetCPUId();

return RSASecurityHelper.Validate(hardNumber, serialNumber);

}

 

public string GetHardNumber()

{

return HardwareInfoHelper.GetCPUId();
}

 

非对称加密、解密、验证辅助类 RSASecurityHelper

标签:

原文地址:http://www.cnblogs.com/watchfluture/p/4668248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!