Note that there are some explanatory texts on larger screens.

plurals
  1. PODecryption result using RSA differs in plain Java and Android
    primarykey
    data
    text
    <p>I encrypt/decrypt a message like the following: Encrypt string -> base64 encode bytes -> serialize string -> deserialize string -> decode b64 -> decrypt bytes.</p> <p>The encryption looks like this:</p> <pre><code>PublicKey pubKey = readPublicKey(); Cipher cipher; cipher = Cipher.getInstance(CRYPTO_ALG); cipher.init(Cipher.ENCRYPT_MODE, pubKey); byte[] cipherData; cipherData = cipher.doFinal(message.getBytes()); return cipherData; </code></pre> <p>Decryption is done like so:</p> <pre><code>PrivateKey pk = readPrivateKey(); Cipher cipher = Cipher.getInstance(CRYPTO_ALG); cipher.init(Cipher.DECRYPT_MODE, pk); return new String(cipher.doFinal(data)); </code></pre> <p>The keys are read like this:</p> <pre><code>ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(is)); BigInteger m = (BigInteger) oin.readObject(); BigInteger e = (BigInteger) oin.readObject(); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); PrivateKey privKey = fact.generatePrivate(keySpec); return privKey; </code></pre> <p>I have omitted the b64 stuff here, but I have verified that the problem does not affect that code.</p> <p>What happens now is that I actually get the correct answer, but it is prepended with binary gibberish. If I encrypt "TESTDATA", I will get TESTDATA. The code works fine in plain Java, but fails in Android. Does anyone know how to fix this? </p> <p>EDIT: Encrypting/decrypting using RSA/NONE/NoPadding does not seem to help. I also use org.bouncycastle in the plain JRE.</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.
 

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