JAVA,NET RSA密钥格式转换

JAVA和NET RSA密钥格式相互转换(公钥,私钥)

不多说直接上代码,需要引用开源类库BouncyCastle.Crypto.dll

也可以在这里下载http://downloads.bouncycastle.org/csharp/bccrypto-net-1.7-bin.zip

以下为转化代码

 1 using System;
 2 using System.Xml;
 3 using Org.BouncyCastle.Asn1.Pkcs;
 4 using Org.BouncyCastle.Asn1.X509;
 5 using Org.BouncyCastle.Crypto.Parameters;
 6 using Org.BouncyCastle.Math;
 7 using Org.BouncyCastle.Pkcs;
 8 using Org.BouncyCastle.Security;
 9 using Org.BouncyCastle.X509;
10 /// <summary>
11 /// RSA密钥格式转换
12 /// </summary>
13 public class RSAKeyConvert
14 {
15     /// <summary>
16     /// RSA私钥格式转换,java->.net
17     /// </summary>
18     /// <param name="privateKey">java生成的RSA私钥</param>
19     /// <returns></returns>
20     public static string RSAPrivateKeyJava2DotNet(string privateKey)
21     {
22         RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));
23         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>",
24         Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
25         Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
26         Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
27         Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
28         Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
29         Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
30         Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
31          Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
32     }
33     /// <summary>
34     /// RSA私钥格式转换,.net->java
35     /// </summary>
36     /// <param name="privateKey">.net生成的私钥</param>
37     /// <returns></returns>
38     public static string RSAPrivateKeyDotNet2Java(string privateKey)
39     {
40         XmlDocument doc = new XmlDocument();
41         doc.LoadXml(privateKey);
42         BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
43         BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
44         BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
45         BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
46         BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
47         BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
48         BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
49         BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));
50         RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);
51         PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
52         byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
53         return Convert.ToBase64String(serializedPrivateBytes);
54     }
55     /// <summary>
56     /// RSA公钥格式转换,java->.net
57     /// </summary>
58     /// <param name="publicKey">java生成的公钥</param>
59     /// <returns></returns>
60     public static string RSAPublicKeyJava2DotNet(string publicKey)
61     {
62         RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
63         return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
64             Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
65             Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
66     }
67     /// <summary>
68     /// RSA公钥格式转换,.net->java
69     /// </summary>
70     /// <param name="publicKey">.net生成的公钥</param>
71     /// <returns></returns>
72     public static string RSAPublicKeyDotNet2Java(string publicKey)
73     {
74         XmlDocument doc = new XmlDocument(); doc.LoadXml(publicKey);
75         BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
76         BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
77         RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
78         SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
79         byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
80         return Convert.ToBase64String(serializedPublicBytes);
81     }
82 }
时间: 2024-11-08 04:33:50

JAVA,NET RSA密钥格式转换的相关文章

.NET与JAVA RSA密钥格式转换

一.该篇内容用于记录.net和Java之间,RSA公密钥的转换 using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Pkcs; using Org.BouncyCastle.Security; using Org.BouncyC

C#和JAVA的RSA密钥、公钥转换

C#的秘钥跟JAVA的密钥区别 RSA对于程序本身是没有区别的,其格式都是相同的.对于不同的程序来说,存储使用的语法(包装的类)会有所不同. RSA语法和语法标准有很多,大的类型大概分为ASN.1.PKCS.X.509. RSA语法介绍 ASN.1.PKCS是最初的,也是最主要的RSA公钥和私钥的语法标准,被RSA Lab所维护. ASN.1.PKCS#1都定义了公钥和私钥的类型--序列化的数字.为了下一个层次的抽象(适当的包装),现在一般使用的组合是:PKCS#8的私钥,X.509的公钥 .

Java之格林威治时间格式转换成北京时间格式

Java之格林威治时间格式转换成北京时间格式 package com.mtons.mblog; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class DateUtils { /** * 支持jdk1.6的写法<br/> * 解析2015-12-27T14:20:34+08:00格式类型的时

Java实现时间日期格式转换示例

package com.hanqi.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 日期转换器 * * @author asus * */ public class DateConvertor { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H

java中jqGrid时间戳格式转换

找到如下代码 if( !isNaN( date - 0 ) && String(format).toLowerCase() == "u") { //Unix timestamp timestamp = new Date( parseFloat(date)*1000 ); } 改为,即去掉*1000 if( !isNaN( date - 0 ) && String(format).toLowerCase() == "u") { //Un

RSA密钥之C#格式与Java格式转换

前言 最近由于项目需求,服务端由c#编写,客户端由java编写.通信数据使用RSA非对称加密.但是java和c#生成的密钥格式是不一样的,所以需要转换格式才可以正常使用.网上搜到使用java进行格式转换的代码(如:http://blog.csdn.net/road2010/article/details/40071881 ),本文将给出一种c#的实现方法. 密钥格式 java密钥格式如下: 私钥: MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBA

asp.net RSA密钥之C#格式与Java格式转换(PEM格式)

//商户私钥(Java格式),公钥已上传        private static string PrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDETH1ziFZ70KCIqTjZvzWs/MIjrcbFptR748voH8fXDsuG2XHU+ds4f8tbuqgp4d4pW7bgwtyJOOYTuRHxh2QsNKvVpg4EZmAWBWCzIHd4J2bWQYjUq+ZwF/Ht6eY/ysQ8rkfAGX

RSA密钥,JAVA与.NET之间转换

最近在做银联的一个接口,用到RSA签名,悲剧来了,.net用的RSA密钥格式和JAVA用的不一样 .net为XML格式 <RSAKeyValue><Modulus>53KnujHcV0962zoLigW8d4AUb+1TS3LiySGrXhF5FgjUQhLzI6PCM/hyHPhUat6MTcgWK3kAVInughtNOHXrBI92I1nAdwlMwBPh+F+0UGhQDR5LMaBg7tQq7ebyhy8/QRCtxEO+F0QQYYv0t15RIup+F+08HdWSn

java时间格式转换

最近在项目中开始写后台java,之前一直从事前端开发,公司需要加上个人也想学习些java后台相关知识,慢慢积累实践.下面总结了些JAVA中有关时间格式转换的一些方法,也方便各位初学者参考. 1 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义要输出日期字符串的格式 2 Date startTime = new Date(); 3 4 String startTimeSting = sdf.fo