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加密解密 加签验签的更多相关文章

  1. 微信小程序(17)-- RSA加密 解密 加签 验签

    RSA加密 解密 加签 验签 /** * 注:区分RSA私钥的类型,有pkcs1和pkcs8.pkcs8格式的私钥主要用于Java中 pkcs1格式: -----BEGIN RSA PRIVATE K ...

  2. RSA密钥生成、加密解密、签名验签

    RSA 非对称加密公钥加密,私钥解密 私钥签名,公钥验签 下面是生成随机密钥对: //随机生成密钥对 KeyPairGenerator keyPairGen = null; try { keyPair ...

  3. PHP 生成公钥私钥,加密解密,签名验签

    test_encry.php <?php //创建私钥,公钥 //create_key(); //要加密内容 $str = "test_str"; //加密 $encrypt ...

  4. RSA加密解密及RSA加签验签

    RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解 ...

  5. RSA加密解密与加签验签

    RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.1987年7月首次在美国公布 ...

  6. RSA 加密 解密 公钥 私钥 签名 加签 验签

    http://blog.csdn.net/21aspnet/article/details/7249401# http://www.ruanyifeng.com/blog/2013/06/rsa_al ...

  7. RSA加密解密及RSA签名和验证及证书

    RSA加密解密及RSA签名和验证及证书 公钥是给别人的 发送密文使用公钥加密 验证签名使用公钥验证 私钥是自己保留的 接受密文使用私钥解密 发送签名使用私钥签名 上述过程逆转是不行的,比如使用私钥加密 ...

  8. Java实现RSA密钥对并在加解密、加签验签中应用的实例

    一.项目结构 二.代码具体实现 1.密钥对生成的两种方式:一种生成公钥私文件,一种生成公钥私串 KeyPairGenUtil.java package com.wangjinxiang.genkey. ...

  9. Python rsa公私钥生成 rsa公钥加解密(分段加解密)-私钥加签验签实战

    一般现在的SAAS服务提供现在的sdk或api对接服务都涉及到一个身份验证和数据加密的问题.一般现在普遍的做法就是配置使用非对称加密的方式来解决这个问题,你持有SAAS公司的公钥,SAAS公司持有你的 ...

随机推荐

  1. Hibernate(二)持久化对象的状态

    简介 以前学习Hibernate的笔记,整理一下便发出来了,防止弄丢.有错误的话麻烦各位留言评论,感激不尽. 持久化类 Hibernate完成了从面向对象模型表示的对象至关系模型表示的数据结构的映射, ...

  2. html打造动画【系列2】- 可爱的蛙蛙表情

    先感受一下全部表情包: 在开始之前先安利一个知识点:Flex弹性布局 我们一般做水平三列布局都是用的float方法,将每一块浮动显示在同一行.这种方法会导致元素没有原来的高度属性,要用清除浮动来解决空 ...

  3. 关于java.lang.NoClassDefFoundError: org/apache/commons/collections/FastHashMap的错误解决办法

    在JavaEE开发中,在把配置文件中的数据或用户表单提交上来的数据,封装在相应JavaBean的对象的对应属性中时:在实际开发中,使用第三方法工具包BeanUtils(commons-beanutil ...

  4. 行内元素和块级元素的具体区别是什么?inline-block是什么?(面试题目)

    一,行内元素与块级元素的区别: 1.行内元素与块级元素直观上的区别二.行内元素与块级元素的三个区别 行内元素会在一条直线上排列(默认宽度只与内容有关),都是同一行的,水平方向排列. 块级元素各占据一行 ...

  5. Object equals 方法

    package com.mydemo.controller; public class TestEquals { public static void main(String[] args) { Do ...

  6. 把梳子卖给和尚 引起的CRM

    招聘故事  N个人去参加一招聘,主考官出了一道实践题目:把梳子卖给和尚.众多应聘者认为这是开玩笑,最后只剩下甲.乙.丙三个人.   主持人交代:以10日为限,向我报告销售情况.  十 天一到.   主 ...

  7. js获取元素显示隐藏的当前状态

    js获取元素显示隐藏的当前状态 // CSS var display = $("."+cls).css("display"); if(display == &q ...

  8. asp.net 對象圖解

  9. 使用AKLocationManager定位

    使用AKLocationManager定位 https://github.com/ideaismobile/AKLocationManager 以下是使用情况: 是不是很简单呢,我们可以将它的步骤进一 ...

  10. linux下jira搭建&破解(转自:https://www.cnblogs.com/zpw-1/p/9553358.html)

    写在前面 网络类似文章不少,但是同样的路,别人走可能一马平川,自己走可能磕磕绊绊.记录一下自己搭建过程的一路踩坑历程[目前还记得的]. 一.环境准备 1,jira7.3的运行是依赖java环境的,也就 ...