Hello all.
I've got this problem:
I've downloaded the BouncyCastle library.
I found that there is implemented ElGamal algorithm, but only for cipher, and not for signature.
But I thought that the signing documents it's just ciphering theirs hash with one of the keys in asimetric key cipher algorithm.
So I tried to do something like this:
Generate keys for ElGamal.
Get the hash from file.
Encrypt it with private key. (create the sign)
Decrypt with public key.
And there is the problem: when I try to encrypt with public key the Exception is thrown that initialize method of the cipher requested ElgamalaPublicKeyParameters.
So I tried to switch places - encrypt with public ky, and decrypt with private key.
KeyPairGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC");
generator.initialize(algorithm.getKeySize(), new SecureRandom());
KeyPair keys = generator.generateKeyPair();
Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, keys.getPublic(), new SecureRandom());
md5 = MessageDigest.getInstance("SHA1");
fis = new FileInputStream(file);
initSignTime = endTime - startTime;
for (long i = 0; i < file.length(); i += SIZE) {
fis.read(array);
md5.update(array);
}
cipher.update(md5.digest());
byte[] sign = cipher.doFinal();
cipher.init(Cipher.DECRYPT_MODE, keys.getPrivate());
byte[] out = cipher.doFinal();
fis = new FileInputStream(file);
for (long i = 0; i < file.length(); i += SIZE) {
fis.read(array);
md5.update(array);
}
boolean verified = Arrays.equals(sign, out);
verifingTime = endTime - startTime;
It worked.
But now i don't know if it is good, beaceause I don't know if the private key has to be private, or it's just matter of name
- so I can treat private key, as public, and public key as private?
I think, that in RSA doesn't matter which key is which, cause you cannot gues the secon one having one of them. (But I'm not the expert in Cryptography)
Is the Elgamal the same at this (my theory)?
Sincerelly.
Black.