C#自定义RSA加密解密及RSA签名和验证类实例
作者:feige 发布时间:2023-08-08 17:48:11
标签:C#,RSA,类
本文实例讲述了C#自定义RSA加密解密及RSA签名和验证类。分享给大家供大家参考。具体分析如下:
这个C#类自定义RSA加密解密及RSA签名和验证,包含了RSA加密、解密及签名所需的相关函数,带有详细的注释说明。
using System;
using System.Text;
using System.Security.Cryptography;
namespace DotNet.Utilities
{
/// <summary>
/// RSA加密解密及RSA签名和验证
/// </summary>
public class RSACryption
{
public RSACryption()
{
}
#region RSA 加密解密
#region RSA 的密钥产生
/// <summary>
/// RSA 的密钥产生 产生私钥 和公钥
/// </summary>
/// <param name="xmlKeys"></param>
/// <param name="xmlPublicKey"></param>
public void RSAKey(out string xmlKeys,out string xmlPublicKey)
{
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
xmlKeys=rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
#endregion
#region RSA的加密函数
//##############################################################################
//RSA 方式加密
//说明KEY必须是XML的行式,返回的是字符串
//在有一点需要说明!!该加密方式有 长度 限制的!!
//##############################################################################
//RSA的加密函数 string
public string RSAEncrypt(string xmlPublicKey,string m_strEncryptString )
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(m_strEncryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
//RSA的加密函数 byte[]
public string RSAEncrypt(string xmlPublicKey,byte[] EncryptString )
{
byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result=Convert.ToBase64String(CypherTextBArray);
return Result;
}
#endregion
#region RSA的解密函数
//RSA的解密函数 string
public string RSADecrypt(string xmlPrivateKey, string m_strDecryptString )
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray =Convert.FromBase64String(m_strDecryptString);
DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
//RSA的解密函数 byte
public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString )
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray=rsa.Decrypt(DecryptString, false);
Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
#endregion
#endregion
#region RSA数字签名
#region 获取Hash描述表
//获取Hash描述表 ,sharejs.com
public bool GetHash(string m_strSource, ref byte[] HashData)
{
//从字符串中取得Hash描述
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
HashData = MD5.ComputeHash(Buffer);
return true;
}
//获取Hash描述表
public bool GetHash(string m_strSource, ref string strHashData)
{
//从字符串中取得Hash描述
byte[] Buffer;
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(m_strSource);
HashData = MD5.ComputeHash(Buffer);
strHashData = Convert.ToBase64String(HashData);
return true;
}
//获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
{
//从文件中取得Hash描述
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
return true;
}
//获取Hash描述表
public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
{
//从文件中取得Hash描述
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
strHashData = Convert.ToBase64String(HashData);
return true;
}
#endregion
#region RSA签名
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
return true;
}
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, ref string m_strEncryptedSignatureData)
{
byte[] EncryptedSignatureData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
return true;
}
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref byte[] EncryptedSignatureData)
{
byte[] HashbyteSignature;
HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
return true;
}
//RSA签名
public bool SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, ref string m_strEncryptedSignatureData)
{
byte[] HashbyteSignature;
byte[] EncryptedSignatureData;
HashbyteSignature = Convert.FromBase64String(m_strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
return true;
}
#endregion
#region RSA 签名验证
public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
{
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
{
byte[] DeformatterData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData =Convert.FromBase64String(p_strDeformatterData);
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
{
byte[] DeformatterData;
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(p_strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(p_strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData =Convert.FromBase64String(p_strDeformatterData);
if(RSADeformatter.VerifySignature(HashbyteDeformatter,DeformatterData))
{
return true;
}
else
{
return false;
}
}
#endregion
#endregion
}
}
希望本文所述对大家的C#程序设计有所帮助。
0
投稿
猜你喜欢
- 本文实例讲述了Java泛型的使用限制。分享给大家供大家参考,具体如下:一 什么情况下不能使用泛型1 不能使用泛型的形参创建对象。T o=ne
- 本文实例为大家分享了SpringBoot获取yml和properties配置文件的具体代码,供大家参考,具体内容如下(一)yml配置文件:p
- 前言记得前段时间的文章么?redis使用位图法记录在线用户的状态,还是需要自己实现一个IM在线用户状态的记录,今天来讲讲另一方案,布隆过滤器
- 本文实例讲述了C#操作session的类。分享给大家供大家参考。具体分析如下:这个C#类对session操作进行了再次封装,可以大大简化se
- 本文实例为大家分享了Intent实现页面跳转的两种的方法,供大家参考,具体内容如下下图中两个不同的方法就是两种页面之间跳转的情况1).跳转不
- Android开发中有时需要在应用中或进程间传递对象,下面详细介绍Intent使用Bundle传递对象的方法。被传递的对象需要先实现序列化,
- package com.test;import org.htmlparser.Node;import org.htmlparser.Node
- Java中提供了大数字(超过16位有效位)的操作类,即 java.math.BinInteger 类和 java.math.BigDecim
- 哈喽大家好啊,我是Hydra。Spring作为项目中不可缺少的底层框架,提供的最基础的功能就是bean的管理了。bean的注入相信大家都比较
- PS:公司的业务中有个超级大的作业就是把OFFICE文档转成PDF,我猜之前没程序猿们,公司那些人应该是一个个手动转。强烈为猿们感叹,帮你们
- 前言在windows平台下实现高性能网络服务器,iocp(完成端口)是唯一选择。编写网络服务器面临的问题有:1 快速接收客户端的连接。2 快
- C# goto 语句用于直接在一个程序中转到程序中的标签指定的位置,标签实际上由标识符加上冒号构成。语法形式如下。goto Labell;
- 序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程。对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过
- 对大部分系统来说都需要权限管理来决定不同用户可以看到哪些内容,那么如何在Spring MVC中实现权限验证呢?当然我们可以继续使用servl
- 实例如下:private bool creatExcel(string xlsfile) { &nb
- 本文实例为大家分享了Unity实现俄罗斯方块第2部分,供大家参考,具体内容如下代码部分1. 实现物体自由降落(在有关于物体的脚本中编写)1)
- 本文实例为大家分享了android recyclerview模拟聊天界面的具体代码,供大家参考,具体内容如下效果图:实现代码:package
- 这个小游戏是我和我姐们儿的JAVA课程设计,也是我做的第一个JAVA项目,适合初学者,希望能帮到那些被JAVA课设所困扰的孩纸们~~~一、该
- 前言最近在优化自己之前基于Spring AOP的统一响应体的实现方案。什么是统一响应体呢?在目前的前后端分离架构下,后端主要是一个RESTf
- 如下所示:import org.apache.commons.lang.StringUtils; public class Test {