8

I have the byte array of the RSA Public Key. I found on the internet that I can create a real PublicKey object of it by using this code:

PublicKey publicKey = 
    KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));

But every time I run this code, I'm getting another result for the encrypted data using that key. I'm sure the data I want to encrypt is always the same, so does the byte array representing the key.

Is this normal?

Here is my code always producing another output:

byte[] keyBytes = Base64.decodeBase64(rsa_1024_public_key);
      // rsa_1024_public key is a constant String

Cipher c = Cipher.getInstance("RSA");

PublicKey publicKey =
   KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(keyBytes));

c.init(Cipher.ENCRYPT_MODE, publicKey);

return c.doFinal(password.getBytes());

This is probably a part of the asymmetric encryption algorithm?

Thanks.

4
  • RSA is non-determinstic. Commented Dec 12, 2011 at 21:16
  • @SLaks: Do you want to post it as an answer? It's correct :D I'll give 35 rep points. Commented Dec 19, 2011 at 21:16
  • Actually, 15; I hit the rep limit long ago. :) Commented Dec 19, 2011 at 21:29
  • See also stackoverflow.com/questions/5488401/… Commented Dec 19, 2011 at 21:30

1 Answer 1

7

RSA is non-determinstic.

You can make it deterministic by selecting a non-random padding mode; however, that will not be secure.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't bother; I generally hit the rep limit every weekday.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.