Conversion between RSA key C # format and Java format,
Preface
Recently, due to project requirements, the server is written by c # And the client is written by java. Communication Data is encrypted using RSA asymmetric encryption. However, the key formats generated by java and c # are different, so you need to convert the format to use it normally. I found the code using java for format conversion (such as: http://blog.csdn.net/road2010/article/details/40071881), this article will give a c # implementation method.
The java key format is as follows:
Private Key:
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKSZSSEdPhv77O5ocnLNGXeQ21qJDArC1+yId+9/pY5bXkZk5vCB49EpfMwNukLv6AJqofThZPNOs1t015fdEYIUnkIc2QVxRwa0xTeFP6N8D4WQXRWs4fNG27JK5kP45s+9KlJtx5hs7G97aMczehIWpHaO6j9inOmjlU8l62KZAgMBAAECgYEAmK3TRtMwRJb33OGnn9OeFumYfy92qxi3X6Hq1o6qDBW2qkd4bImfv+ni6AinyOVuaadt2Y+lq4dKGcCVJzoZvPm1VKxD2y7xKa8/vEbPRiRTt0qnPq9T7UJkpDsiXf/zOMfWdjc3uA1bPnQ65RWHSJ7zAE+Gd7xnyCE5MEyijLECQQDVYqQWDqOVLZ5lJUuIfUIrhv26GvuoTX8v60+opCz4/Mdfh6JlefICVD6SAaYvufXBHVFY26JicNlR62ZOiBoNAkEAxXhsuEnNJNQcQHEVTPZoulbMbTV1VZIDQ1zjG8fvu8sv6IBYcR5+EsC8n3/6RkW8/iCJDzxE++VHzhoSQSoDvQJBAM6/63J/rpndAIrJ7vyJOPLJsc9/U3SH2gMJAT7KC9UXvuldlsixtf3xuEpplKbLjEUXbfklnZm586a+6XqPvoUCQDFotltOLARBBmihYuEE7qNhQHk63QbyJ9rdDP5Qgo2Mg4o7QuXa6VSr4QZPsUGQBX/YiDLFs8ULU3IgV9zyNEkCQADAYR8DctYzg0eBGdcOrDA5Szc62pYrS2wk89wUxyL4FyDL3omkVMKvtu5tccy7Xht2ikJZRqefZ3dS7ASm8/4=
Public Key:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCkmUkhHT4b++zuaHJyzRl3kNtaiQwKwtfsiHfvf6WOW15GZObwgePRKXzMDbpC7+gCaqH04WTzTrNbdNeX3RGCFJ5CHNkFcUcGtMU3hT+jfA+FkF0VrOHzRtuySuZD+ObPvSpSbceYbOxve2jHM3oSFqR2juo/Ypzpo5VPJetimQIDAQAB
The. NET key format is as follows:
Private Key:
<RSAKeyValue> <Modulus>pJlJIR0+G/vs7mhycs0Zd5DbWokMCsLX7Ih373+ljlteRmTm8IHj0Sl8zA26Qu/oAmqh9OFk806zW3TXl90RghSeQhzZBXFHBrTFN4U/o3wPhZBdFazh80bbskrmQ/jmz70qUm3HmGzsb3toxzN6Ehakdo7qP2Kc6aOVTyXrYpk=</Modulus> <Exponent>AQAB</Exponent> <P>1WKkFg6jlS2eZSVLiH1CK4b9uhr7qE1/L+tPqKQs+PzHX4eiZXnyAlQ+kgGmL7n1wR1RWNuiYnDZUetmTogaDQ==</P> <Q>xXhsuEnNJNQcQHEVTPZoulbMbTV1VZIDQ1zjG8fvu8sv6IBYcR5+EsC8n3/6RkW8/iCJDzxE++VHzhoSQSoDvQ==</Q> <DP>zr/rcn+umd0Aisnu/Ik48smxz39TdIfaAwkBPsoL1Re+6V2WyLG1/fG4SmmUpsuMRRdt+SWdmbnzpr7peo++hQ==</DP> <DQ>MWi2W04sBEEGaKFi4QTuo2FAeTrdBvIn2t0M/lCCjYyDijtC5drpVKvhBk+xQZAFf9iIMsWzxQtTciBX3PI0SQ==</DQ> <InverseQ>wGEfA3LWM4NHgRnXDqwwOUs3OtqWK0tsJPPcFMci+Bcgy96JpFTCr7bubXHMu14bdopCWUann2d3UuwEpvP+</InverseQ> <D>mK3TRtMwRJb33OGnn9OeFumYfy92qxi3X6Hq1o6qDBW2qkd4bImfv+ni6AinyOVuaadt2Y+lq4dKGcCVJzoZvPm1VKxD2y7xKa8/vEbPRiRTt0qnPq9T7UJkpDsiXf/zOMfWdjc3uA1bPnQ65RWHSJ7zAE+Gd7xnyCE5MEyijLE=</D></RSAKeyValue>
Public Key:
<RSAKeyValue> <Modulus>pJlJIR0+G/vs7mhycs0Zd5DbWokMCsLX7Ih373+ljlteRmTm8IHj0Sl8zA26Qu/oAmqh9OFk806zW3TXl90RghSeQhzZBXFHBrTFN4U/o3wPhZBdFazh80bbskrmQ/jmz70qUm3HmGzsb3toxzN6Ehakdo7qP2Kc6aOVTyXrYpk=</Modulus> <Exponent>AQAB</Exponent></RSAKeyValue>
Conversion implementation code (c #)
An open source encryption library is used for format conversion.Bouncy Castle Crypto APIs,Address: http://www.bouncycastle.org/csharp/
The specific implementation code is as follows:
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; /// <summary> // RSA key format conversion /// </summary> public class RSAKeyConvert {// <summary> // RSA private key format conversion, java->. net /// </summary> /// <param name = "privateKey"> RSA private key generated by java </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 ();} // <summary> // RSA private key format conversion ,. net-> java // </summary> // <param name = "privateKey">. net-generated private key </param> // <returns> </returns> public static string rs20.vatekeydotnet2java (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 rs?vatecrtkeyparameters (m, exp, d, p, q, dp, dq, qinv); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory. createPrivateKeyInfo (privateKeyParam); byte [] serializedPrivateBytes = privateKeyInfo. toAsn1Object (). getEncoded (); return Convert. toBase64String (serializedPrivateBytes);} // <summary> // RSA public key format conversion, java->. net /// </summary> /// <param name = "publicKey"> public Key generated by java </param> /// <returns> </returns> 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 ();} // <summary> // RSA public key format conversion ,. net-> java // </summary> /// <param name = "publicKey">. public Key generated by net </param> /// <returns> </returns> 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 );}}