Search code examples
javaencryptionencryption-asymmetric

Asymmetric Encryption in Java: How to avoid different results of the same phrase?


I know that one characteristic of asymetric encryption is, that if you encryt a text with the public key, you get each time different results.

Is there a way to get always the same result (without RSA)?

Sample-Code:

PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(
    new RSAPublicKeySpec(
        new BigInteger("83087..."),
        new BigInteger("65537")));

PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(
    new RSAPrivateKeySpec(
        new BigInteger("830874..."),
        new BigInteger("514268...")));


Cipher cipher = Cipher.getInstance( "RSA" ); 
cipher.init( Cipher.ENCRYPT_MODE, publicKey );        
cipher.doFinal( "test");

Solution

  • A good encryption must give different results, as it hides information more securely. It hides a fact that a plain text for two separate cipher texts is equal. It's called semantic security.

    It's achieved by http://en.wikipedia.org/wiki/Initialization_vector.

    And yes, of course, the encryption is asymmetric.