Note that there are some explanatory texts on larger screens.

plurals
  1. PORSA Key pair Exception on importing the private key
    primarykey
    data
    text
    <p>I am new to RSA and Cryptography and i need to encrypt and decrypt data using RSA, I have a java program which generates a key pair and save them in a <code>.key</code> files with XML format (It is absolutely OK and it was tested by encrypting and decrypting data ), then I want to use them in .NET application , I am importing the keys to be used for encrypt and decrypt. The public key is OK and encryption getting done without problem but the private key Import fails with the following exception message</p> <pre><code>Bad data (CryptographicException.ThrowCryptogaphicException(Int32 hr)) </code></pre> <p>This is the encoded public key:</p> <pre><code>&lt;RSAKeyValue&gt; &lt;Modulus&gt;iFouk9viRs5dcvJCvDM1vXC4sBuSB9SPcdJhRyFLoNW/pka6MNAiu4cOksFRejiuM1ZswyJMy+ow lmLflJ/XrfnUQxLwLp61oij4CrzHKl9jjHorqIA7uEQKY8RBiUjZ7kbO5nFaIWs1NWMVks8Srdhv 8pVd1sLKKUs66c/ndAk=&lt;/Modulus&gt; &lt;Exponent&gt;AQAB&lt;/Exponent&gt; &lt;/RSAKeyValue&gt; </code></pre> <p>and this is the encoded public key:</p> <pre><code>&lt;RSAKeyValue&gt; &lt;Modulus&gt;iFouk9viRs5dcvJCvDM1vXC4sBuSB9SPcdJhRyFLoNW/pka6MNAiu4cOksFRejiuM1ZswyJMy+ow lmLflJ/XrfnUQxLwLp61oij4CrzHKl9jjHorqIA7uEQKY8RBiUjZ7kbO5nFaIWs1NWMVks8Srdhv 8pVd1sLKKUs66c/ndAk=&lt;/Modulus&gt; &lt;Exponent&gt;AQAB&lt;/Exponent&gt; &lt;P&gt;AO9WnDNOt9Xewnoy8KTed56Z+3Nfto6J8wCXKzX3LhuuiKNUBe8qFoinrteQJq/9NAEXnNCafxDW ThIkr9GtMxE=&lt;/P&gt; &lt;Q&gt;AJHYMk0bOEGZlQbaJk3VDovvOJuRt5NI3WtXWl1v5VUW6aQQO3rV3+3GSN6Xa3hTKXtCVVL26Awy OkDykUPjQXk=&lt;/Q&gt; &lt;DP&gt;KIHsJfLowlXVbIE6oWzVqg49tKU6bJ2Ed1Eeix+uuhisH5iU+ImTDsXynaFUKu0b5CNu8w9y+hKL XB7BcydxQQ==&lt;/DP&gt; &lt;DQ&gt;di267NIersF1idzhZvY62FdbBmx4VaeYi+93sPkH2wA7CI+CsxF1Z6XhzETkd9bjaRaiLx0VgTR+ Eby8y0bt+Q==&lt;/DQ&gt; &lt;InverseQ&gt;HYF8gahVyzsz0IotzKI2Oh53sJMZWVxsvzkhqGlDtY1THFGZE5j8kl/UK0+FSN6yOYxBIuKNZ7om MgLQEMK1PQ==&lt;/InverseQ&gt; &lt;D&gt;DERQvGyjxsr6DUVOS7AvvYNOmklgseOlpA/RQJz2ONoCC+uBBLM07LoRzZImymAfC+9SiZukXRQM mvr6MlzPAm04NWyZNzbjhLvmn1gmvDclDZ9X9bhYp8MBftPWU5PFBALOjVpD+mlbI2lTYCugf6pJ MHEMe17mNJ0eWCerfAE=&lt;/D&gt; &lt;/RSAKeyValue&gt; </code></pre> <p>Please help me to understand what is happening and what's wrong with the private key.</p> <p>this is the code that is working ok after solving the problem : </p> <pre><code> private String getPublicKeyXml(RSAPublicKey pk) throws UnsupportedEncodingException { StringBuilder builder = new StringBuilder(); builder.append("&lt;RSAKeyValue&gt;\n"); byte[] m = pk.getModulus().toByteArray(); byte[] mm = stripLeadingZeros(m); write(builder, "Modulus", mm); write(builder, "Exponent", pk.getPublicExponent()); builder.append("&lt;/RSAKeyValue&gt;"); return builder.toString(); } private String getPrivateKeyXml(PrivateKey pk) throws UnsupportedEncodingException { RSAPrivateCrtKey privKey = (RSAPrivateCrtKey) pk; BigInteger n = privKey.getModulus(); BigInteger e = privKey.getPublicExponent(); BigInteger d = privKey.getPrivateExponent(); BigInteger p = privKey.getPrimeP(); BigInteger q = privKey.getPrimeQ(); BigInteger dp = privKey.getPrimeExponentP(); BigInteger dq = privKey.getPrimeExponentQ(); BigInteger inverseQ = privKey.getCrtCoefficient(); StringBuilder builder = new StringBuilder(); builder.append("&lt;RSAKeyValue&gt;\n"); write(builder, "Modulus", stripLeadingZeros(n.toByteArray())); write(builder, "Exponent", stripLeadingZeros(e.toByteArray())); write(builder, "P", stripLeadingZeros(p.toByteArray())); write(builder, "Q", stripLeadingZeros(q.toByteArray())); write(builder, "DP", stripLeadingZeros(dp.toByteArray())); write(builder, "DQ", stripLeadingZeros(dq.toByteArray())); write(builder, "InverseQ", stripLeadingZeros(inverseQ.toByteArray())); write(builder, "D", stripLeadingZeros(d.toByteArray())); builder.append("&lt;/RSAKeyValue&gt;"); return builder.toString(); } private void write(StringBuilder builder, String tag, byte[] bigInt) throws UnsupportedEncodingException { builder.append("\t&lt;"); builder.append(tag); builder.append("&gt;"); builder.append(encode(bigInt).trim()); builder.append("&lt;/"); builder.append(tag); builder.append("&gt;\n"); } private void write(StringBuilder builder, String tag, BigInteger bigInt) throws UnsupportedEncodingException { builder.append("\t&lt;"); builder.append(tag); builder.append("&gt;"); builder.append(encode(bigInt)); builder.append("&lt;/"); builder.append(tag); builder.append("&gt;\n"); } private static String encode(BigInteger bigInt) throws UnsupportedEncodingException { return new String(new sun.misc.BASE64Encoder().encode(bigInt.toByteArray())); } private static String encode(byte[] bigInt) throws UnsupportedEncodingException { return new String(new sun.misc.BASE64Encoder().encode(bigInt)); } private byte[] stripLeadingZeros(byte[] a) { int lastZero = -1; for (int i = 0; i &lt; a.length; i++) { if (a[i] == 0) { lastZero = i; } else { break; } } lastZero++; byte[] result = new byte[a.length - lastZero]; System.arraycopy(a, lastZero, result, 0, result.length); return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload