RSA对于程序本身是没有区别的,其格式都是相同的。对于不同的程序来说,存储使用的语法(包装的类)会有所不同。
RSA语法和语法标准有很多,大的类型大概分为ASN.1、PKCS、X.509。
RSA语法介绍
ASN.1、PKCS是最初的,也是最主要的RSA公钥和私钥的语法标准,被RSA Lab所维护。
ASN.1、PKCS#1都定义了公钥和私钥的类型——序列化的数字。为了下一个层次的抽象(适当的包装),现在一般使用的组合是:PKCS#8的私钥,X.509的公钥 。
PKCS语法主要用于私钥,其内部标准目前有10种。目前JAVA普遍使用的是PKCS#8,用作私钥格式。
X.509语法主要用于公钥,广泛使用于web浏览器和SLL。
3种语法标准的公私钥可以实现相互转化,其核心为ASN1语法中的整数值(modulus,PublicExponent,privateExponent ),其余的数值都可以经过计算后得出。
.NET使用的是标准的RSA格式,然后将其中的数字base64编码后,生成XML进行存储。
java中使用的是PKCS#8,、X.509的公私钥语法,存储的都是相应的JAVA类自动生成的base64字符串。
由于存储格式的区别,在相互转换、读取的时候,需要理解RSA相关的知识,才能正确使用类来转换。
密钥格式
java密钥:
- 私钥:
- MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKSZSSEdPhv77O5ocnLNGXeQ21qJDArC1+yId+9/pY5bXkZk5vCB49EpfMwNukLv6AJqofThZPNOs1t015fdEYIUnkIc2QVxRwa0xTeFP
- 6N8D4WQXRWs4fNG27JK5kP45s+9KlJtx5hs7G97aMczehIWpHaO6j9inOmjlU8l62KZAgMBAAECgYEAmK3TRtMwRJb33OGnn9OeFumYfy92qxi3X6Hq1o6qDBW2qkd4bImfv+ni6AinyOVuaadt2Y+lq4
- dKGcCVJzoZvPm1VKxD2y7xKa8/vEbPRiRTt0qnPq9T7UJkpDsiXf/zOMfWdjc3uA1bPnQ65RWHSJ7zAE+Gd7xnyCE5MEyijLECQQDVYqQWDqOVLZ5lJUuIfUIrhv26GvuoTX8v60+opCz4/Mdfh6JlefI
- CVD6SAaYvufXBHVFY26JicNlR62ZOiBoNAkEAxXhsu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fG4SmmUpsuMRRdt+SWdmbnzpr7peo++hQ==</DP>
- <DQ>MWi2W04sBEEGaKFi4QTuo2FAeTrdBvIn2t0M/lCCjYyDijtC5drpVKvhBk+xQZAFf9iIMsWzxQtTciBX3PI0SQ==</DQ>
- <InverseQ>wGEfA3LWM4NHgRnXDqwwOUs3OtqWK0tsJPPcFMci+Bcgy96JpFTCr7bubXHMu14bdopCWUann2d3UuwEpvP+</InverseQ>
- <D>mK3TRtMwRJb33OGnn9OeFumYfy92qxi3X6Hq1o6qDBW2qkd4bImfv+ni6AinyOVuaadt2Y+lq4dKGcCVJzoZvPm1VKxD2y7xKa8/vEbPRiRTt0qnPq9T7UJkpDsiXf/zOMfWdjc3uA1bPnQ65RWHS
- J7zAE+Gd7xnyCE5MEyijLE=</D>
- </RSAKeyValue>
-
- 公钥:
- <RSAKeyValue>
- <Modulus>pJlJIR0+G/vs7mhycs0Zd5DbWokMCsLX7Ih373+ljlteRmTm8IHj0Sl8zA26Qu/oAmqh9OFk806zW3TXl90RghSeQhzZBXFHBrTFN4U/o3wPhZBdFazh80bbskrmQ/jmz70qUm3HmGzsb3tox
- zN6Ehakdo7qP2Kc6aOVTyXrYpk=</Modulus>
- <Exponent>AQAB</Exponent>
- </RSAKeyValue>
C#转JAVA(C#代码)
C#中的公私钥使用XML字符串进行存储,读取时直接读入字符串即可。
由于C#使用的是标准的RSA格式,因此JAVA的RSAPublicKeySpec、RSAPrivateKeySpec配置类的核心参数(modulus,PublicExponent,privateExponent )都可以从对应XML中的节点值(Modulus-modulus 、Exponent-PublicExponent 、D-privateExponent )base64解码后获取。然后将其传入JAVA配置类中,然后根据配置类生成相应的RSA公私钥。
手动转换
- void Convert()
- {
-
- byte[] m = Base64.decodeBase64("mX/9zl8rflH5pLaP5P1Qd/9wXwNBSx7OpLlYDnGr7wD0njiDfPSUkgf9oF5NcvZwl24qdJ1SLmrgUtnr+yeXBNZNKaan1xXKISHdlHvbW5G8nJCJW6CuaHMkVw3Y7kwaIIlUdv09vxfjj0AoabttjbtF1kqETzbQ6fK3EN6sY5U=");
- byte[] e = Base64.decodeBase64("AQAB");
- BigInteger b1 = new BigInteger(1, m);
- BigInteger b2 = new BigInteger(1, e);
-
- byte[] m1 = Base64.decodeBase64("3RgqP5YOYUXft8YOlDphyaCoof27MSfTD2eVCFVXB5hatrls1fSUcmUuWuGV970sS6KQZZtyWHQ5970sCzKFlq82He8Uoe0JM3axBvd6PbSGjulUJr62qNW5hgkIEfxSRYl8AQsbbusFtks4obfepsfE02cLmmZepnZAdIOWifE=");
- byte[] e1 = Base64.decodeBase64("QcSZdLbHakolxX4GAjPnuNmwsBdRIsss7o0qeQMh02GPwoEgDfkmW20bv+8Q9FPypEEkYQU/m25ffAFq453QvLegYYi8OvWN+dvgchQRdeb22d+s6xYGGN9DRcPFRE48INde8FBHf/lzVgToV75h1H7g+jB4hLmLeuIuHsB43/0=");
- BigInteger b11 = new BigInteger(1, m1);
- BigInteger b21 = new BigInteger(1, e1);
-
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
- RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(keySpec);
-
- RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(b11, b21);
- RSAPrivateKey priKey = (RSAPrivateKey) keyFactory.generatePrivate(priKeySpec);
- }
BouncyCastle Crypto转换
格式转换要用到一个开源加密库Bouncy Castle Crypto APIs,
官网地址:http://www.bouncycastle.org/csharp/ 具体实现代码如下:
- using System;
- using System.Xml;
- 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.BouncyCastle.X509;
-
- public class RSAKeyConvert
- {
-
-
-
-
-
- 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 RSAPrivateKeyDotNet2Java(string privateKey)
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(privateKey);
- BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
- BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
- BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
- BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
- BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
- BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
- BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
- BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].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 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()));
- }
-
-
-
-
-
-
- public static string RSAPublicKeyDotNet2Java(string publicKey)
- {
- XmlDocument doc = new XmlDocument();
- doc.LoadXml(publicKey);
- BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
- BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
- RsaKeyParameters pub = new RsaKeyParameters(false, m, p);
-
- SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
- byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
- return Convert.ToBase64String(serializedPublicBytes);
- }
- }
JAVA转C#(java代码)
JAVA中的公私钥使用base64进行存储,解码成字节数组后,首先需要生成相应的配置对象(PKCS#8,、X.509 ),根据配置对象生成RSA公私钥。
私钥 C#使用的是标准的RSA格式,PKCS#1语法中包含了标准RSA格式私钥中的所有整数值。配置对象需要生成PKCS#1语法的RSA对象(RSAPrivateCrtKey),获取对象属性,自行构造私钥XML。 就算不会JAVA的新手都可以转换的,只需要把以下的代码添加进eclipse中就可以了,具体的怎么建java工程就自己去查一下。 把一对密钥转成C#中格式的密钥在JAVA中的转换代码如下:
|
请发表评论