To encrypt a message with ElGamal scheme in java code, I proceed as follow:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("Elgamal/NOne/NoPadding", "BC");
KeyPaireGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC");
SecureRandom random = new SecureRandom();
generator.initialize(512, random);
KeyPair pair = generator.generateKeyPair();
String message = "myMessageToEncrypt";
cipher.init(Cipher.ENCRYPT_MODE, pair.getPublic(), random);
[]byte cipherText = cipher.doFinal(message);
I know from the ELGamal scheme that cipherText byte array contains (c1, c2) and I need to access c1 as an BigInteger.
So my question is: how to make the conversion between the byte array and the tuple (c1, c2) ?
Thank you
The
byte[]with the ciphertext has twice the length of the key size, where the first half corresponds toc0and the second half toc1. The conversion ofciis achievable e.g. withnew BigInteger(1, ci).Verification is easily possible by performing the decryption manually with the
BigIntegers converted in this way:with e.g. the following output:
Note that a key size of 512 bits is too small nowadays, see e.g. here, and a missing padding is insecure, e.g. here.