Note that there are some explanatory texts on larger screens.

plurals
  1. PORSA decryption using modulus and exponent
    primarykey
    data
    text
    <p><strong>My task:</strong> I have encrypted (RSA) data and public key as modulus and exponent. I have to write decryption code.<br/> <strong>My problem with it:</strong> My implementation doesn't work ;) As far as I know philosophy is simple <strike>"open text" == rsa(public_key, rsa(private_key, "open text"))</strike> <strong>Edit: Exactly my assumption was wrong (Assumption is mother of all fu..ups ;) ). It should be <code>"open text" == rsa(private_key, rsa(public_key, "open text"))</code> because in RSA, public key is used for encryption and private for decryption.</strong> <br/><br/>I assumed that I can have public key which doesn't correspond to private key using during encryption so for tests I created own keys in such way:</p> <pre><code>openssl genrsa -des3 -out server.key 1024 openssl req -new -key server.key -out server.csr openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt </code></pre> <p>I got public key modulus and exponent using command:</p> <pre><code>openssl x509 -in server.crt -text </code></pre> <p>For encryption testing I'm using code</p> <pre><code>//Reads private key from file //StringPasswordFinder is my tmp implementation of PasswordFinder PEMReader pemReader = new PEMReader(new FileReader("/path/to/server.key"), new StringPasswordFinder()); KeyPair keyPair = (KeyPair) pemReader.readObject(); PrivateKey pk = keyPair.getPrivate(); //text for encryption String openText = "openText"; //encryption Cipher rsaCipher = Cipher.getInstance("RSA", "BC"); rsaCipher.init(Cipher.ENCRYPT_MODE, pk); byte[] encrypted = rsaCipher.doFinal(openText.getBytes("utf-8")); </code></pre> <p>And for decryption of encrypted text I use code</p> <pre><code>//modulus hex got using openssl byte[] modulus = Hex.decodeHex("very long hex".toCharArray()); //exponent hex got using openssl byte[] exponent = Hex.decodeHex("010001".toCharArray()); //initialization of rsa decryption engine RSAEngine rsaEngine = new RSAEngine(); rsaEngine.init(false, new RSAKeyParameters(false, new BigInteger(modulus), new BigInteger(exponent))); //input - encrypted stream ByteArrayInputStream bais = new ByteArrayInputStream(encrypted); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //reading blocks from the input stream and decrypting them int bytesRead = 0; byte[] block = new byte[rsaEngine.getInputBlockSize()]; while ((bytesRead = bais.read(block)) &gt; -1) { baos.write(rsaEngine.processBlock(block, 0, bytesRead)); } //dispalying decrypted text System.out.println(new String(baos.toByteArray(), "utf-8")); </code></pre> <p>And after all displayed text is not. Can anybody show me where I'm wrong?</p> <p><strong>Edit: Summing up this problem has no solution. Because it's not possible encrypt message using private key and later decrypt it using public one. At general I mixed up encryption with signing message and decryption with verification. Because during making signature private key is used and public is used during verification. Btw, MByD thx for important clue.</strong></p>
    singulars
    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