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公司持有你的 ...
随机推荐
- POJ 1284 Primitive Roots 数论原根。
Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2479 Accepted: 1385 D ...
- HwUI,CMS管理系统模板,漂亮,简单,兼容好
HwUI兼容目前所有浏览器,IE6+,Opera,Firefox,Chorme,Safari,由于IE6基本已废弃,所以也没有测试IE6的兼容,但做了部分IE6兼容调整.HwUI操作简单,路由导航不需 ...
- 在vue-cli中使用layer中的layData日期组件
有朋友问我怎么在vue-cli项目中使用layui中的layData组件,有时间从网上查了下写下篇文章. 1.首先去layData官网把文件包下载下来,解压出来的laydate文件夹整个放在vue-c ...
- CSS之浮动布局(float,浮动原理,清除/闭合浮动方法)
css之浮动布局 本人博客:查看文章 1.什么是浮动:在我们布局的时用到的一种技术,能够方便我们进行布局,通过让元素浮动,我们可以使元素在水平上左右移动,再通过margin属性调整位置 2.浮动的 ...
- JS 面试题 实践一
1.完成 extname 函数,它会接受一个文件名作为参数,你需要返回它的扩展名. 例如,输入 emoji.png,返回 .png. const extname = (filename) => ...
- CSS属性display的浅略探讨
display 的属性值有:none|inline|block|inline-block|list-item|run-in|table|inline-table|table-row-group|tab ...
- ArcGIS10.3+Oracle12C+ArcGIS Server10.3安装布署(之二)
1.创建PDB 输入 dbca 命令 2.安装完成后,连接PDBSDE的容器数据库 3.环境变量 从Oracle的官方网站下载 instantclient-basic-nt-12.1.0.2.0. ...
- Arcengine编辑代码
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...
- ios虚拟机安装 (三)
安装mac系统出现欢迎使用的界面-->一直继续(个人设定) 点击左上角-->Dock-->启动放大 Safari 浏览器
- android设计的布局在阿拉伯语下界面错乱的解决方法
(1)正在AndroidManifest.xml声明文件的application元素中,增加” android:supportsRtl=true” (2)建] androidの设计的布局在阿拉伯语下界 ...