Why Elgamal encryption algorithm generating different cipher for the same string, key pair and random value, in java?
<p>This is the code I used. For the same string it is generating two different ciphers. Though the random number , key pair is same. i have checked through out the program the random number and keys are same. But still the generated cipher text is different for the same input. While I am decrypting those different ciphers the plaintexts are same.  Why the ciphers are different? </p>

<pre><code>     Security.addProvider(new BouncyCastleProvider());
     String[] message = {"secret message","secret message"};
     byte[] messageBytes1 = message[0].getBytes();
     byte[] messageBytes2 = message[1].getBytes();
     SecureRandom rn = new SecureRandom();
     rn.setSeed(1234);
     System.out.println("random"+rn.nextInt());

     // Obtain a ElGamal Cipher Object
     Cipher elGamalCipher = Cipher.getInstance("ElGamal/None/PKCS1Padding", "BC");   
     // Obtain the corresponding key pair generator
     KeyPairGenerator elGamalKPG = KeyPairGenerator.getInstance("ElGamal");
     // Initialize the key pair generator with the desired strength
     elGamalKPG.initialize(1024);        
     // Generate a key pair
     KeyPair elGamalKeyPair = elGamalKPG.genKeyPair();

     // Initialize the cipher

     System.out.println("random key :"+secureRandom.toString());

     elGamalCipher.init(Cipher.ENCRYPT_MODE, elGamalKeyPair.getPublic(), rn);
     System.out.println("cipher1::");
     // Finally encrypt the message
     byte[] ciphertextBytes1 = elGamalCipher.doFinal(messageBytes1);         
     for(int i = 0; i&lt;ciphertextBytes1.length; i++)
         System.out.print(ciphertextBytes1[i]);
     System.out.println();       
     System.out.println("cipher2::");
     elGamalCipher.init(Cipher.ENCRYPT_MODE, elGamalKeyPair.getPublic(), rn);
     byte[] ciphertextBytes2 = elGamalCipher.doFinal(messageBytes2);
     for(int i = 0; i&lt;ciphertextBytes2.length; i++)
         System.out.print(ciphertextBytes2[i]);

     //initialize plain text

     elGamalCipher.init(Cipher.DECRYPT_MODE, elGamalKeyPair.getPrivate());
     byte[] plainText1 = elGamalCipher.doFinal(ciphertextBytes1);

     //plaintext1
     System.out.println("plainText1:");
     for(int a=0 ; a&lt;plainText1.length;a++)
         System.out.print(plainText1[a]);
     System.out.println();
     elGamalCipher.init(Cipher.DECRYPT_MODE, elGamalKeyPair.getPrivate());
     byte[] plainText2 = elGamalCipher.doFinal(ciphertextBytes2);

     //plain text 2
     System.out.println("plainText2:");
     for(int a=0 ; a&lt;plainText2.length;a++)
         System.out.print(plainText2[a]);
     System.out.println();
</code></pre>
