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公司持有你的 ...
随机推荐
- Zookeeper学习文章目录1
目录:参考文章如下 1.ZooKeeper学习第一期---Zookeeper简单介绍 2. ZooKeeper学习第二期--ZooKeeper安装配置 3. ZooKeeper学习第三期---Zook ...
- RequestAnimationFrame更好的实现Javascript动画
一直以来,JavaScript的动画都是通过定时器和间隔来实现的.虽然使用CSS transitions 和 animations使Web开发实现动画更加方便,但多年来以JavaScript为基础来实 ...
- bootstrap学习笔记(表单)
1.基础表单 :对于基础表单,Bootstrap并未对其做太多的定制性效果设计,仅仅对表单内的fieldset.legend.label标签进行了定制. fieldset { min-width: 0 ...
- 关于nginx下drupal7无法$_GET获取参数的问题
参考:nginx 官方drupal 配置 - Drupal | NGINX 之前配置好了LNMP下的drupal7(7.59版本),简洁路径也配好了.但是在使用过程出现问题. 配置的nginx.con ...
- 解决input为number类型时maxlength无效的问题
使用input数字number类型的时候maxlength无效,假设需要控制输入数量为18,可以用以下方式: 无效: <input type="text" maxlengt ...
- C#基础 继承和实例化
有代码如下,问输出的是多少: class Program { static void Main(string[] args) { B b = new B(); Console.ReadKey(); } ...
- Prometheus Node_exporter 之 Basic Net / Disk Info
1. Network Traffic Basic 每个接口的基本网络信息 type: GraphUnit: bytesrecv {{device}} 各个网络接口的下载量 recv lo: 本地环回接 ...
- leveldb源码分析--插入删除流程
由于网络上对leveldb的分析文章都比较丰富,一些基础概念和模型都介绍得比较多,所以本人就不再对这些概念以专门的篇幅进行介绍,本文主要以代码流程注释的方式. 首先我们从db的插入和删除开始以对整个体 ...
- scrapy实战--爬取报刊名称及地址
目标:爬取全国报刊名称及地址 链接:http://news.xinhuanet.com/zgjx/2007-09/13/content_6714741.htm 目的:练习scrapy爬取数据 学习过s ...
- extjs 可视化开发工具
不清楚Ext可视化的可以看一下http://www.screencast.com/users/JackSlocum这里的三个视频哈. 安装和汉化的步骤 第一步下载:Ext需要支持AIR的支持 下载并安 ...