Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have more than one problem:</p> <ol> <li><p>Java does not support RSA keys of size less than 512. 2048 bit is a better choice. So change the key length:</p> <pre><code>kpg.initialize(2048); </code></pre></li> <li><p>String.getBytes() is not the inverse operation to your bytes2String(). After encrypting you convert the bytes to a hexadecimal string. But then you convert this hexadecimal string to its ASCII representation before decrypting, which yields a byte-array that is too long. Instead, use something like this to convert the hexadecimal string back:</p> <pre><code>private byte[] string2bytes(String s) { int len = s.length(); byte[] res = new byte[len / 2]; for (int i = 0; i &lt; len; i += 2) { res[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) &lt;&lt; 4) + Character.digit(s.charAt(i+1), 16)); } return res; } </code></pre> <p>and then call that instead of String.getBytes():</p> <pre><code>// Note, this line is not completely fixed yet String decString = myEncrypt.bytes2String( myEncrypt.rsaDecrypt( myEncrypt.string2bytes(encString) ) ); </code></pre></li> <li><p>Finally you have the opposite problem. Your bytes2String() method does not reverse the String.getBytes() operation. You encrypted the output of "pritesh".getBytes(), so that is what you got out of the decrypt operation. Now you have to convert that back to a String. The String(byte[])-constructor will do that for you:</p> <pre><code>String decString = new String( myEncrypt.rsaDecrypt( myEncrypt.string2bytes(encString) ) ); </code></pre></li> </ol>
    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.
    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