对于上上篇博客中我讲的一个故事,本文引用: https://www.cnblogs.com/ButterflyEffect/p/9851403.html

故事中提到的关于加密会出现,私钥加密,公钥解密的情况,这种情况我们将它称为【私钥加签,公钥验签】

说句好理解的话:

既然是加密,那肯定是不希望别人知道我的消息,所以只有我才能解密,所以可得出【公钥负责加密,私钥负责解密】

同理,既然是签名,那肯定是不希望有人冒充我发消息,只有我才能发布这个签名,所以可得出【私钥负责加签,公钥负责验签】

由以上得出关于安全性的问题,再举个好例子:

现在有A(私钥A、公钥A),B(私钥B、公钥B) ,A向B发送消息,用私钥A加签、用公钥B加密,发送给B,B用私钥B解密,然后用公钥A验签。

这样的话就能保证是A发的消息,并且只有B自己才能解密。这样是非常安全的!!!

话不多少,下面直接撸代码,以下代码是实现【私钥加签公钥验签】的功能,公钥加密私钥解密的功能在我其它博客有讲过...

 #region 私钥加密,公钥解密
/// <summary>
/// 私钥加密
/// </summary>
/// <param name="privateKey">RSA私钥 base64格式</param>
/// <param name="contentData">待加密的数据</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static string EncryptWithPrivateKey(string privateKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding")
{
return Convert.ToBase64String(EncryptWithPrivateKey(Convert.FromBase64String(privateKey), contentData, algorithm));
}
/// <summary>
/// 私钥加密
/// </summary>
/// <param name="privateKey">RSA私钥</param>
/// <param name="contentData">待加密的数据</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static byte[] EncryptWithPrivateKey(byte[] privateKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding")
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKey);
return Transform(privateKeyParam, contentData, algorithm, true);
}
/// <summary>
/// 公钥解密
/// </summary>
/// <param name="publicKey">RSA公钥 base64格式</param>
/// <param name="content">待解密数据 base64格式</param>
/// <param name="encoding">解密出来的数据编码格式,默认UTF-8</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static string DecryptWithPublicKey(string publicKey, string content, string encoding = "UTF-8", string algorithm = "RSA/ECB/PKCS1Padding")
{
return Encoding.GetEncoding("GB2312").GetString(DecryptWithPublicKey(Convert.FromBase64String(publicKey), Convert.FromBase64String(content), algorithm));
}
/// <summary>
/// 公钥解密
/// </summary>
/// <param name="publicKey">RSA公钥</param>
/// <param name="contentData">待解密数据</param>
/// <param name="algorithm">加密算法</param>
/// <returns></returns>
public static byte[] DecryptWithPublicKey(byte[] publicKey, byte[] contentData, string algorithm = "RSA/ECB/PKCS1Padding")
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKey);
return Transform(publicKeyParam, contentData, algorithm, false);
}
#endregion
private static byte[] Transform(AsymmetricKeyParameter key, byte[] contentData, string algorithm, bool forEncryption)
{
var c = CipherUtilities.GetCipher(algorithm);
c.Init(forEncryption, new ParametersWithRandom(key));
return c.DoFinal(contentData);
}

顺带提下,这里的第三方RSA引用包可以在【程序包管理器控制台】输入命令提示

PM> Install-Package BouncyCastle

完成包的安装

static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string xmlPrivateKey = rsa.ToXmlString(true);//XML密钥
string xmlPublicKey = rsa.ToXmlString(false);//XML公钥 string str = "测试数据";
byte[] b = System.Text.Encoding.Default.GetBytes(str);//字符串转字节数组,byte[] string EncryptedData = EncryptWithPrivateKey(B(xmlPrivateKey), b);//返回加密后的base64格式数据
Console.WriteLine("私钥加密结果:" + EncryptedData);
string DecipheringData = DecryptWithPublicKey(A(xmlPublicKey), EncryptedData);//返回解密后的明文数据
Console.WriteLine("公钥解密结果:" + DecipheringData); Console.ReadLine();
} //XML格式转base64格式,公钥
public static string A(string xml)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml);
var p = rsa.ExportParameters(false);
RsaKeyParameters key = new RsaKeyParameters(false, new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent)); SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(key);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
string publicKey = Convert.ToBase64String(serializedPublicBytes);
return publicKey;
} //XML格式转base64格式,私钥
public static string B(string xml)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xml);
var p = rsa.ExportParameters(true);
var key = new RsaPrivateCrtKeyParameters(
new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent), new BigInteger(1, p.D),
new BigInteger(1, p.P), new BigInteger(1, p.Q), new BigInteger(1, p.DP), new BigInteger(1, p.DQ),
new BigInteger(1, p.InverseQ)); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(key);
byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
string privateKey = Convert.ToBase64String(serializedPrivateBytes);
return privateKey;
}

最后加密测试可以在这个网站去测试 http://tool.chacuo.net/cryptrsapubkey

【绝迹篇】RSA加密算法(私钥加签公钥验签)的更多相关文章

  1. java RSA实现私钥签名、公钥验签、私钥加密数据、公钥解密数据

    通过OpenSSL生成公私钥文件(如果没有OpenSSL工具建议下载Cmder工具自带OpenSSL指令) 1.生成RSA密钥的方法 genrsa -out private-rsa.key 2048 ...

  2. RSA公钥、私钥、签名和验签

    1 RSA加密算法介绍 RSA又叫非对称加密算法,这类加密算法有一对秘钥,其中一个用来加密一个用来解密.这一对秘钥中你可以选择一个作为私钥(自己保存),另一个作为公钥(对外公开).用私钥加密的内容只能 ...

  3. SHA256withRSA证书签名,私钥签名/公钥验签

    证书签名 package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundE ...

  4. Java & PHP RSA 互通密钥、签名、验签、加密、解密

    RSA加密算法是一种非对称加密算法.在公开密钥加密和电子商业中RSA被广泛使用.RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Le ...

  5. RSA公钥验签

    1.业务场景,公司做理财业务,但是可能有第三方合作.与第三方合作获得更多客户流量.别人可以在第三方进行购买理财产品.那么怎么保证交易信息的安全性那,我们这里给出rsa加密实现原理. 2.工具类rsa: ...

  6. Openssl生成RSA公私钥以及将公钥转换成C#支持的格式

    Openssl生成RSA公私钥以及将公钥转换成C#支持的格式 1.RSA算法介绍 RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密.RSA ...

  7. Java Http接口加签、验签操作方法

    1.业务背景 最近接触了一些电商业务,发现在处理电商业务接口时,比如淘宝.支付类接口,接口双方为了确保数据参数在传输过程中未经过篡改,都需要对接口数据进行加签,然后在接口服务器端对接口参数进行验签,确 ...

  8. Delphi支付宝支付【支持SHA1WithRSA(RSA)和SHA256WithRSA(RSA2)签名与验签】

    作者QQ:(648437169) 点击下载➨Delphi支付宝支付             支付宝支付api文档 [Delphi支付宝支付]支持条码支付.扫码支付.交易查询.交易退款.退款查询.交易撤 ...

  9. c#RSA 私钥加签公钥解签

    /// RSA签名 /// </summary> /// <param name="data">待签名数据</param> /// <pa ...

随机推荐

  1. 使用 typeof 来检测对象是否undefined

    需求 判断是否为undefined 解决 使用 typeof 来检测对象是否已定义: if (typeof Obj !== "undefined" && Obj ! ...

  2. Tomcat添加用户

    在conf目录下tomcat-users.xml文件里添加如下代码: <role rolename="manager-gui"/> <user username= ...

  3. tmux使用技巧

    1.tmux 进入tmux 2.在tmux中 按ctrl+b 表示要进行tmux操作了. 3. c -> create a session 4. ","  -> ren ...

  4. BZOJ2882: 工艺(后缀数组)

    题意 题目链接 Sol 直接把序列复制一遍 后缀数组即可 在前\(N\)个位置中取\(rak\)最小的输出 #include<bits/stdc++.h> using namespace ...

  5. css3动画基础详解(@keyframes和animation)

    我们经常会看到CSS3能制作出很炫酷的动画效果,但是自己却只能做一些简单的.原因是对CSS3动画只知其一,不知其二.最近正好有做动画的项目,于是花时间将css3动画做了一个探究之旅,记录在册. 动画是 ...

  6. Python-约束和异常处理

    今天我们来说一说类的规范以及程序出现错误后我们要怎么进行处理 一.类的约束 首先,你要清楚,约束是对类的约束,比如,现在你是一个项目经理,然后呢,你给手下的人分活,张三你处理一下普通用户登录,李四你处 ...

  7. 【转】OkHttp使用进阶 译自OkHttp Github官方教程

    作者:GavinCT 出处:http://www.cnblogs.com/ct2011/ 英文版原版地址 Recipes · square/okhttp Wiki 同步get 下载一个文件,打印他的响 ...

  8. js中的this--执行上下文

    条件:函数调用的时候  才有执行上下文 this 不同情况的调用,this也不同 1)当函数直接打点调用,此时的this 是window 2)事件触发函数,此时的this是触发这个事件的对象 3)当对 ...

  9. iOS线程和进程的区别和联系

    线程和进程的区别主要在于它们是不同的操作系统资源管理方式.进程有独立的地址空间,一个进程崩溃后,在保护模式的影响下不会对其他进程产生影响,而线程只是一个进程中的不同执行路径.线程有自己的堆栈和局部变量 ...

  10. RocketMQ读书笔记1——简述

    [消息队列的功能介绍] 分布式消息队列可以提供应用解耦.流量削峰.消息分发.保证最终一致性.方便动态扩容等功能. [MQ使用场景1——应用解耦] 复杂的系统如电商系统,会存在多个子系统,如订单系统.库 ...