I have a case where to ensure the same encrypted string for the given input. Say for example If "Hello" is encrypted more than one time then same encrypted string should be i should get as an output (Like in hash algorithms). I have tried pure "ECIES" & "ElGamal" algorithms using bouncycastle but both returns different outputs each time for the same input.
I have tried with RSA by setting the "RSA/ECB/NoPadding" option and it works like charm. But RSA is both performance and memory consuming stuff so i choosen the Elliptic curve which will us both performance and efficient memory. So I choose ECIES initially and also "Elgamal".
What should i set for ECIES and ElGamal to produce same output each time for same input.?
public class AsymmetricElGamalCipher extends BaseAsymmetricCipher {
private final SecureRandom random;
public AsymmetricElGamalCipher(final Config config) throws AndesException {
super(CryptAlgorithmType.ELGAMAL, config);
this.random = KeyPairUtils.createFixedRandom();
}
/**
* Encrypt the given bytes of data using the ECC public key
*
* @param value
* The data to be encoded or encrypted
* @return The encrypted/encoded data
* @throws AndesException
* if an error occurred during the operation
*/
public byte[] encrypt(byte[] value) throws AndesException {
byte[] hexEncodedCipher = null;
try {
this.cipher.init(Cipher.ENCRYPT_MODE, this.publicKey, this.random);
hexEncodedCipher = this.cipher.doFinal(value);
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
throw new AndesException(e);
}
return hexEncodedCipher;
}
/**
* Encrypt the given plain text using the Elgamal private key
*
* @param cipherText
* The data to be decoded/decrypted
* @return The decrypted/decoded bytes of data
* @throws AndesException
* if an error occurred during the operation
*/
public byte[] decrypt(byte[] value) throws AndesException {
byte[] hexEncodedCipher = null;
try {
this.cipher.init(Cipher.DECRYPT_MODE, this.privateKey, this.random);
hexEncodedCipher = this.cipher.doFinal(value);
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
throw new AndesException(e);
}
return hexEncodedCipher;
}
}
Here is the Ecc algorithm code,
public class AsymmetricEccCipher extends BaseAsymmetricCipher {
// generate derivation and encoding vectors
private final byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
private final byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
private final String initVector = "0000000000000000";
private final IESParameterSpec ALGORITHM_PARAMETER_SPEC;
public AsymmetricEccCipher(final Config config) throws AndesException {
super(CryptAlgorithmType.ECIES, config);
try {
ALGORITHM_PARAMETER_SPEC = new IESParameterSpec(d, e, 128, 128, this.initVector.getBytes(IAndes.CHARACTER_TYPE_UTF_8), true);
} catch (UnsupportedEncodingException e) {
throw new AndesException("EC IES parameter spec creation error.");
}
}
/**
* Encrypt the given bytes of data using the ECC public key
*
* @param value
* The data to be encoded or encrypted
* @return The encrypted/encoded data
* @throws AndesException
* if an error occurred during the operation
*/
public byte[] encrypt(byte[] value) throws AndesException {
byte[] hexEncodedCipher = null;
try {
this.cipher.init(Cipher.ENCRYPT_MODE, this.publicKey, this.ALGORITHM_PARAMETER_SPEC);
hexEncodedCipher = this.cipher.doFinal(value);
} catch (InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | InvalidAlgorithmParameterException e) {
throw new AndesException(e);
}
return hexEncodedCipher;
}
/**
* Encrypt the given plain text using the ECC private key
*
* @param cipherText
* The data to be decoded/decrypted
* @return The decrypted/decoded bytes of data
* @throws AndesException
* if an error occurred during the operation
*/
public byte[] decrypt(byte[] value) throws AndesException {
byte[] hexEncodedCipher = null;
try {
this.cipher.init(Cipher.DECRYPT_MODE, this.privateKey, this.ALGORITHM_PARAMETER_SPEC);
hexEncodedCipher = this.cipher.doFinal(value);
} catch (InvalidKeyException | IllegalBlockSizeException
| BadPaddingException | InvalidAlgorithmParameterException e) {
throw new AndesException(e);
}
return hexEncodedCipher;
}
}