de.flexiprovider.core.elgamal
Class ElGamal
java.lang.Object
|
+--javax.crypto.CipherSpi
|
+--de.flexiprovider.api.Cipher
|
+--de.flexiprovider.api.AsymmetricBlockCipher
|
+--de.flexiprovider.core.elgamal.ElGamal
- public class ElGamal
- extends AsymmetricBlockCipher
This class implements ElGamal encryption. A ciphertext block consists of the
two numbers B = gbmod p and c = Ab * m mod p (m =
message). For details about the parameters g, p and A see
ElGamalKeyPairGenerator.
To encrypt a message, the following steps have to be performed:
// The message which should be encrypted
String message = "secret message";
byte[] messageBytes = message.getBytes();
// The source of randomness
SecureRandom secureRandom = Registry.getSecureRandom();
// Obtain a ElGamal Cipher Object
Cipher elGamalCipher = Cipher.getInstance("ElGamal");
// 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
cipher.init(Cipher.ENCRYPT_MODE, elGamalKeyPair.getPublic(), secureRandom);
// Finally encrypt the message
byte[] ciphertextBytes = cipher.doFinal(messageBytes);
Decrypting a message is similar to encryption, except the Cipher
must be initialized with Cipher.DECRYPT_MODE and the private key (
elGamalKeyPair.getPrivate()).
On an AMD Athlon 550 MHz with a 1024 bit key the encryption rate is about 5
kB/sec, the decryption rate is about 17 kB/sec.
- Author:
- Thomas Wahrenbruch
- See Also:
ElGamalKeyPairGenerator,
ElGamalPublicKey,
ElGamalPrivateKey
|
Field Summary |
static java.lang.String |
ALG_NAME
The algorithm name. |
static java.lang.String |
OID
The OID of ElGamal. |
| Methods inherited from class de.flexiprovider.api.AsymmetricBlockCipher |
checkLength, doFinal, doFinal, getBlockSize, getIV, getOutputSize, getParameters, initDecrypt, initDecrypt, initEncrypt, initEncrypt, initEncrypt, initEncrypt, setMode, setPadding, update, update |
| Methods inherited from class de.flexiprovider.api.Cipher |
doFinal, doFinal, engineDoFinal, engineDoFinal, engineGetBlockSize, engineGetIV, engineGetKeySize, engineGetOutputSize, engineGetParameters, engineInit, engineInit, engineInit, engineSetMode, engineSetPadding, engineUpdate, engineUpdate, update |
| Methods inherited from class java.lang.Object |
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ALG_NAME
public static final java.lang.String ALG_NAME
- The algorithm name.
OID
public static final java.lang.String OID
- The OID of ElGamal.
ElGamal
public ElGamal()
getName
public java.lang.String getName()
- Return the name of this cipher.
- Overrides:
getName in class Cipher
- Returns:
- "ElGamal"
getKeySize
public int getKeySize(Key key)
throws InvalidKeyException
- Return the key size of the given key object in bits. Checks whether the
key object is an instance of ElGamalPublicKey or
ElGamalPrivateKey.
- Overrides:
getKeySize in class Cipher
- Parameters:
key - the key object- Returns:
- the key size of the given key object.
- Throws:
InvalidKeyException - if key is invalid.
initCipherEncrypt
protected void initCipherEncrypt(Key key,
AlgorithmParameterSpec params,
SecureRandom secureRandom)
throws InvalidKeyException
- Initialize the block cipher with a key for data encryption. Parameters
are currently not supported.
- Overrides:
initCipherEncrypt in class AsymmetricBlockCipher
- Parameters:
key - the key which has to be used to encrypt data.params - the algorithm parameters.secureRandom - a source of randomness.- Throws:
InvalidKeyException - if the given key is inappropriate for initializing this
cipher.
initCipherDecrypt
protected void initCipherDecrypt(Key key,
AlgorithmParameterSpec params)
throws InvalidKeyException
- Initialize the block cipher with a key for data encryption. Parameters
are currently not supported.
- Overrides:
initCipherDecrypt in class AsymmetricBlockCipher
- Parameters:
key - the key which has to be used to decrypt data.params - the algorithm parameters.- Throws:
InvalidKeyException - if the given key is inappropriate for initializing this
cipher.
messageEncrypt
protected byte[] messageEncrypt(byte[] input)
- Encrypt a message.
- Overrides:
messageEncrypt in class AsymmetricBlockCipher
- Parameters:
input - the message- Returns:
- the encrypted message
messageDecrypt
protected byte[] messageDecrypt(byte[] input)
throws BadPaddingException
- Decrypt a ciphertext.
- Overrides:
messageDecrypt in class AsymmetricBlockCipher
- Parameters:
input - the ciphertext- Returns:
- the decrypted ciphertext
- Throws:
BadPaddingException - if the ciphertext is invalid.