C# 数字证书 RSA加密解密 加签验签
KeyValuePair<string, string> keyPair = Encrypter.CreateRSAKey();
string privateKey = keyPair.Value;
string publicKey = keyPair.Key;
string content = "cc"; string Signed = Encrypter.HashAndSignString(content, privateKey);
Console.WriteLine("数字签名:{0}", Signed); bool verify = Encrypter.VerifySigned(content, Signed, publicKey);
Console.WriteLine("签名验证结果:{0}", verify); Console.ReadKey(); X509Certificate2 x509 = new X509Certificate2(@"F:/CA.cer");
Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject);
Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer);
Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version);
Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore);
Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter);
Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint);
Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber);
Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName);
Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(true));
Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length);
Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(true));
Console.WriteLine("{0}Certificate to XML String: {1}{0}", Environment.NewLine, RSAPublicKeyDotNet2Java(x509.PublicKey.Key.ToXmlString(false))); X509Store store = new X509Store();
store.Open(OpenFlags.MaxAllowed);
store.Add(x509);
store.Close(); //Console.ReadKey(); UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("");
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(x509.PublicKey.Key.ToXmlString(false));
byte[] encryptedData = RSA.Encrypt(dataToEncrypt, false);
var ff = Convert.ToBase64String(encryptedData);
} UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes(""); var RSAalg = cc; //使用SHA1进行摘要算法,生成签名
byte[] encryptedData = RSAalg.SignData(dataToEncrypt, new SHA1CryptoServiceProvider());
var bb = Convert.ToBase64String(encryptedData); byte[] dataToVerifyBytes = ByteConverter.GetBytes("");
byte[] signedDataBytes = Convert.FromBase64String(bb);
var a = RSAalg.VerifyData(dataToVerifyBytes, new SHA1CryptoServiceProvider(), signedDataBytes);
Console.WriteLine(a);
Console.ReadKey(); X509Certificate2 x509 = new X509Certificate2(@"F:213978863940714.pfx", "");
var gg = x509.PrivateKey.ToXmlString(true); var h = Encrypter.GetPrivateKey();
var hh = h.ToXmlString(true); X509Certificate2 cert = new X509Certificate2(@"F:213978863940714.pfx", "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); Console.WriteLine(RSAPrivateKeyDotNet2Java(cert.PrivateKey.ToXmlString(true)));
Console.WriteLine("----------------------------------------");
Console.WriteLine(RSAPublicKeyDotNet2Java(cert.PrivateKey.ToXmlString(false))); var privateKey = "<RSAKeyValue><Modulus>x95HqPU/Q7sC0BHfjMvO7lM1LLhOnS4zeVh3xIMWZZ9a5ERx9waV34CEavwMlwvvYy16biVRL2DJA2jCoHI1fd9HYaoE2ZAD0FemLF9sq3/9RO6IeZYkpFqKcQWOsI5VN7wqkzFUEtRTAYHTXSDsTZPYPrgPVrbgCNKr9bgOOG8=</Modulus><Exponent>AQAB</Exponent><P>5InK4JxHxhTCY7eKUfLAUhRBwpqFe++1neTOHVVrMMG0G//CMkEMYFt4gIBBsKxBWlB6TyPrZ0LK7YUlXELlzQ==</P><Q>3+KNdxQyZpOlZhvUyni7H1MQjQru+ffTvlL1M6rk3pI6hUjWkH1GMOQvX/Xx+/Kf4Fe1KTQch5qb9GYCD+kbKw==</Q><DP>xFA5HcghP14FvXKkNtC3s8oC0w+5KkU3VXJ+O2Rst20tMf/46QJHh14LnRaPVxwg51vKNME+LW2Ks410ElTE4Q==</DP><DQ>Zq+3qNVXpJq1sxayy8cCNITZw4cvQvF7agEMvAz2+mrhcn6NAyqiRgxy+jWJLsECuVghHGvtZfjw7PDYo0mMjw==</DQ><InverseQ>3Y+xaV7Kvu1ywe8+1vW76rsXvp0D/MzIXaP5ZNPyRJo1QhCSEbZi7DLlTH60WNKixctkktLyNIMdIKZYY9oacQ==</InverseQ><D>WbNsC+tNonM7FvD+mK0bySB0/AYX2jlTBsHqtrpygddcLph9YXWGLBH83BsU93F21dciXG7JGe9hJ/OLbgDz+if/aBaYIVtxOeJ02oSY8t2I9KPUlYdbhVLg/m7Le4lkpU+4XfHlqa0w8QgXzLQSz27Tv/RDq8reS5nGno+2Dlk=</D></RSAKeyValue>";
var publicKey = cert.PrivateKey.ToXmlString(false); ////var t = Encrypter.EncryptByRSA("123456",publicKey);
////var s = Encrypter.DecryptByRSA(t,privateKey); var d = Encrypter.HashAndSignString("972a17e69f4b824c6c7792b37262861874a67a77a9762639679647ab666a11e3", privateKey);
var h = Encrypter.VerifySigned("7a2e9897f27bec76157ddf53febf7479740193a8cb50db07ce2d2b9105b66b8b", d, publicKey);
Console.WriteLine(h);
Console.ReadKey(); } public static string RSAPrivateKeyDotNet2Java(string privateKey)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(privateKey);
BigInteger m = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[].InnerText));
BigInteger exp = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[].InnerText));
BigInteger d = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[].InnerText));
BigInteger p = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[].InnerText));
BigInteger q = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[].InnerText));
BigInteger dp = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[].InnerText));
BigInteger dq = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[].InnerText));
BigInteger qinv = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[].InnerText));
RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
return Convert.ToBase64String(serializedPrivateBytes);
} public static string RSAPublicKeyDotNet2Java(string publicKey)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(publicKey);
BigInteger m = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[].InnerText));
BigInteger p = new BigInteger(, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[].InnerText));
RsaKeyParameters pub = new RsaKeyParameters(false, m, p); SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
return Convert.ToBase64String(serializedPublicBytes);
} /// <summary>
/// RSA私钥格式转换,java->.net
/// </summary>
/// <param name="privateKey">java生成的RSA私钥</param>
/// <returns></returns>
public static string RSAPrivateKeyJava2DotNet(string privateKey)
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
} public static string RSAPublicKeyJava2DotNet(string publicKey)
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
} }
C# 数字证书 RSA加密解密 加签验签的更多相关文章
- 微信小程序(17)-- RSA加密 解密 加签 验签
RSA加密 解密 加签 验签 /** * 注:区分RSA私钥的类型,有pkcs1和pkcs8.pkcs8格式的私钥主要用于Java中 pkcs1格式: -----BEGIN RSA PRIVATE K ...
- RSA密钥生成、加密解密、签名验签
RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...
- PHP 生成公钥私钥,加密解密,签名验签
test_encry.php <?php //创建私钥,公钥 //create_key(); //要加密内容 $str = "test_str"; //加密 $encrypt ...
- RSA加密解密及RSA加签验签
RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解 ...
- RSA加密解密与加签验签
RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年7月首次在美国公布 ...
- RSA 加密 解密 公钥 私钥 签名 加签 验签
http://blog.csdn.net/21aspnet/article/details/7249401# http://www.ruanyifeng.com/blog/2013/06/rsa_al ...
- RSA加密解密及RSA签名和验证及证书
RSA加密解密及RSA签名和验证及证书 公钥是给别人的 发送密文使用公钥加密 验证签名使用公钥验证 私钥是自己保留的 接受密文使用私钥解密 发送签名使用私钥签名 上述过程逆转是不行的,比如使用私钥加密 ...
- Java实现RSA密钥对并在加解密、加签验签中应用的实例
一.项目结构 二.代码具体实现 1.密钥对生成的两种方式:一种生成公钥私文件,一种生成公钥私串 KeyPairGenUtil.java package com.wangjinxiang.genkey. ...
- Python rsa公私钥生成 rsa公钥加解密(分段加解密)-私钥加签验签实战
一般现在的SAAS服务提供现在的sdk或api对接服务都涉及到一个身份验证和数据加密的问题.一般现在普遍的做法就是配置使用非对称加密的方式来解决这个问题,你持有SAAS公司的公钥,SAAS公司持有你的 ...
随机推荐
- K:有限状态自动机
有限状态自动机是一种特殊的状态机.它表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型.有限状态自动机分为两种,一种是 确定有限状态自动机(DFA) ,一种是 非确定有限状态自动机(NF ...
- js中list 和 map还有string的部分操作
1.创建list或者数组 var list = []; list中添加元素:list.push("hello"); 如果没有先定义为数组类型不能使用 push方法 判断list ...
- 对抗网络GAN的应用实例
https://sigmoidal.io/beginners-review-of-gan-architectures/ 嗨,大家好!像许多追随AI进展的人一样,我无法忽略生成建模的最新进展,尤其是 ...
- 那些年vue踩过的坑
1.前言 学习Vue前端框架已经一个月了,作为一个web刚入门的菜鸟,在学习的过程中,网上有些技术博客往往没有什么可以借鉴的地方,在这里 我特意将我从开始一直到登录的过程记录下来.希望看到我的文章的朋 ...
- java实现Kafka生产者示例
使用java实现Kafka的生产者 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 3 ...
- 深入理解net core中的依赖注入、Singleton、Scoped、Transient(三)
相关文章: 深入理解net core中的依赖注入.Singleton.Scoped.Transient(一) 深入理解net core中的依赖注入.Singleton.Scoped.Transient ...
- SQLServer Temp tables 数据疑问
1. 现象 使用Cacti监控,有关于临时表的一个图形 可以看到正在使用的临时表Active Temp Tables的数量非常大,并且在非工作时间,也维持在400个左右.感觉非常奇怪,所以追查下! 2 ...
- machine learning model(algorithm model) .vs. statistical model
https://www.analyticsvidhya.com/blog/2015/07/difference-machine-learning-statistical-modeling/ http: ...
- 一些简单的SQL Server服务器监控
1:磁盘活动的一些监控 指标 吞吐量:IOPS,存储子系统每秒能提供多少次I/O 吞吐量,MB/S,I/O子系统每秒能提供多少MB 延时:每个I/O请求占用多少时间 队列长度:队列中有多少IO请求在等 ...
- python_循环(迭代)
#for 和 while #for用来迭代处理,什么叫迭代?你就当没看到这个词,for是把一堆玩意做一个一个加工用的,比如吃一袋花生,得一个一个剥吧,就是这意思 a = 'abcde' #每个字母当做 ...