由于项目的原因,原来的项目使用.net 进行开发,现在需要转成java, 所以原来的加解密就成了一个棘手的问题。由于数据使用RSA签名加密,又因为.net 和 Java 加解密算法上的差异,并不能使用同一个私钥来对原始数据进行解密。所以考虑的方向就是签名密钥之间互相转换。
有幸的是,网络上也有很多遇到此问题的朋友,网上也有类似的很多解决方法。但是寻找最终的解决方案还是花了很长时间。再加上自己对程序上的修改,得出了比较适合自己转换的一个解决方案吧。
转换需要使用BouncyCastle.Crypto.dll
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Xml; usingSystem.Security.Cryptography; usingOrg.BouncyCastle.Crypto.Parameters; usingOrg.BouncyCastle.Asn1.Pkcs; usingOrg.BouncyCastle.Pkcs; usingOrg.BouncyCastle.Math; usingOrg.BouncyCastle.Asn1.X509; usingOrg.BouncyCastle.X509; usingOrg.BouncyCastle.Security; namespaceRSADemo { publicclassRSAKeyConvert { ///<summary> ///RSA私钥格式转换,java->.net ///</summary> ///<param name="privateKey">java生成的RSA私钥</param> ///<returns></returns> publicstaticstringRSAPrivateKeyJava2DotNet(stringprivateKey) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); returnstring.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())); } ///<summary> ///RSA私钥格式转换,.net->java ///</summary> ///<param name="privateKey">.net生成的私钥</param> ///<returns></returns> publicstaticstringRSAPrivateKeyDotNet2Java(stringprivateKey) { XmlDocument doc = newXmlDocument(); doc.LoadXml(privateKey); BigInteger m = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText)); BigInteger exp = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText)); BigInteger d = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText)); BigInteger p = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText)); BigInteger q = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText)); BigInteger dp = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText)); BigInteger dq = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText)); BigInteger qinv = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText)); RsaPrivateCrtKeyParameters privateKeyParam = newRsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam); byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded(); returnConvert.ToBase64String(serializedPrivateBytes); } ///<summary> ///RSA公钥格式转换,java->.net ///</summary> ///<param name="publicKey">java生成的公钥</param> ///<returns></returns> publicstaticstringRSAPublicKeyJava2DotNet(stringpublicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); returnstring.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } ///<summary> ///RSA公钥格式转换,.net->java ///</summary> ///<param name="publicKey">.net生成的公钥</param> ///<returns></returns> publicstaticstringRSAPublicKeyDotNet2Java(stringpublicKey) { XmlDocument doc = newXmlDocument(); doc.LoadXml(publicKey); BigInteger m = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText)); BigInteger p = newBigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText)); RsaKeyParameters pub = newRsaKeyParameters(false, m, p); SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub); byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded(); returnConvert.ToBase64String(serializedPublicBytes); } } }