Elgamal Class

Properties   Methods   Events   Config Settings   Errors  

Encrypt and decrypt data with the Elgamal asymmetric key encryption algorithm for public-key cryptography.

Syntax

ipworksencrypt.Elgamal

Remarks

The ElGamal class is used to encrypt/decrypt data with the Elgamal asymmetric key encryption algorithm for public-key cryptography.

To begin you must either specify an existing key or create a new key. Existing public or private keys may be specified by setting Key. To create a new key pair call CreateKey.

After specifying a key, specify the data you wish to encrypt or decrypt. Call the Encrypt or Decrypt method to perform the operation.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

An Elgamal key is made up of a number of individual parameters.

The public key consists of the following parameters:

The class also includes the PublicKey field which holds the PEM formatted public key for ease of use. This is helpful if you are in control of both signature creation and verification process. When sending the public key to a recipient note that not all implementations will support using the PEM formatted value in PublicKey in which case the individual parameters must be sent.

The private key consists of the following parameters:

The class also include the PrivateKey field which holds the PEM formatted private key for ease of use. This is helpful for storing the private key more easily.

Property List


The following is the full list of the properties of the class with short descriptions. Click on the links for further details.

InputFileThe file to process.
InputMessageThe message to process.
KeyThe Elgamal key used for decryption.
OutputFileThe output file when encrypting or decrypting.
OutputMessageThe output message after processing.
OverwriteIndicates whether or not the class should overwrite files.
RecipientKeyThe recipient's public key used when encrypting.
UseHexWhether input or output is hex encoded.

Method List


The following is the full list of the methods of the class with short descriptions. Click on the links for further details.

ConfigSets or retrieves a configuration setting.
CreateKeyCreates a new key.
DecryptDecrypts the input data using the specified private key.
EncryptEncrypts the input data using the recipient's public key.
ResetResets the class.
SetInputStreamSets the stream from which the class will read data to encrypt or decrypt.

Event List


The following is the full list of the events fired by the class with short descriptions. Click on the links for further details.

ErrorFired when information is available about errors during data delivery.

Config Settings


The following is a list of config settings for the class with short descriptions. Click on the links for further details.

CloseInputStreamAfterProcessingDetermines whether or not the input stream is closed after processing.
CloseOutputStreamAfterProcessingDetermines whether or not the output stream is closed after processing.
KeySizeThe size, in bits, of the secret key.
BuildInfoInformation about the product's build.
GUIAvailableWhether or not a message loop is available for processing events.
LicenseInfoInformation about the current license.
MaskSensitiveWhether sensitive data is masked in log messages.
UseDaemonThreadsWhether threads created by the class are daemon threads.
UseInternalSecurityAPIWhether or not to use the system security libraries or an internal implementation.

InputFile Property (Elgamal Class)

The file to process.

Syntax


public String getInputFile();


public void setInputFile(String inputFile);

Default Value

""

Remarks

This property specifies the file to be processed. Set this property to the full or relative path to the file which will be processed.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

InputMessage Property (Elgamal Class)

The message to process.

Syntax


public byte[] getInputMessage();


public void setInputMessage(byte[] inputMessage);

Default Value

""

Remarks

This property specifies the message to be processed.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

Key Property (Elgamal Class)

The Elgamal key used for decryption.

Syntax


public ElgamalKey getKey();


public void setKey(ElgamalKey key);

Remarks

This property specifies the Elgamal key used to decrypt data. This property must be set before calling Decrypt.

An Elgamal key is made up of a number of individual parameters.

The public key consists of the following parameters:

The class also includes the PublicKey field which holds the PEM formatted public key for ease of use. This is helpful if you are in control of both signature creation and verification process. When sending the public key to a recipient note that not all implementations will support using the PEM formatted value in PublicKey in which case the individual parameters must be sent.

The private key consists of the following parameters:

The class also include the PrivateKey field which holds the PEM formatted private key for ease of use. This is helpful for storing the private key more easily.

Please refer to the ElgamalKey type for a complete list of fields.

OutputFile Property (Elgamal Class)

The output file when encrypting or decrypting.

Syntax


public String getOutputFile();


public void setOutputFile(String outputFile);

Default Value

""

Remarks

This property specifies the file to which the output will be written when Encrypt or Decrypt is called. This may be set to an absolute or relative path.

This property is only applicable to Encrypt and Decrypt.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

OutputMessage Property (Elgamal Class)

The output message after processing.

Syntax


public byte[] getOutputMessage();


Default Value

""

Remarks

This property will be populated with the output from the operation if OutputFile and SetOutputStream are not set.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

This property is read-only and not available at design time.

Overwrite Property (Elgamal Class)

Indicates whether or not the class should overwrite files.

Syntax


public boolean isOverwrite();


public void setOverwrite(boolean overwrite);

Default Value

False

Remarks

This property indicates whether or not the class will overwrite OutputFile. If Overwrite is False, an error will be thrown whenever OutputFile exists before an operation. The default value is False.

RecipientKey Property (Elgamal Class)

The recipient's public key used when encrypting.

Syntax


public ElgamalKey getRecipientKey();


public void setRecipientKey(ElgamalKey recipientKey);

Remarks

This property specifies the recipient's public key. This property must be set before calling Encrypt.

An Elgamal key is made up of a number of individual parameters.

The public key consists of the following parameters:

The class also includes the PublicKey field which holds the PEM formatted public key for ease of use. This is helpful if you are in control of both signature creation and verification process. When sending the public key to a recipient note that not all implementations will support using the PEM formatted value in PublicKey in which case the individual parameters must be sent.

The private key consists of the following parameters:

The class also include the PrivateKey field which holds the PEM formatted private key for ease of use. This is helpful for storing the private key more easily.

Please refer to the ElgamalKey type for a complete list of fields.

UseHex Property (Elgamal Class)

Whether input or output is hex encoded.

Syntax


public boolean isUseHex();


public void setUseHex(boolean useHex);

Default Value

False

Remarks

This property specifies whether the encrypted data is hex encoded.

If set to True, when Encrypt is called the class will perform the encryption as normal and then hex encode the output. OutputMessage or OutputFile will hold hex encoded data.

If set to True, when Decrypt is called the class will expect InputMessage or InputFile to hold hex encoded data. The class will then hex decode the data and perform decryption as normal.

Config Method (Elgamal Class)

Sets or retrieves a configuration setting.

Syntax

public String config(String configurationString);

Remarks

Config is a generic method available in every class. It is used to set and retrieve configuration settings for the class.

These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, access to these internal properties is provided through the Config method.

To set a configuration setting named PROPERTY, you must call Config("PROPERTY=VALUE"), where VALUE is the value of the setting expressed as a string. For boolean values, use the strings "True", "False", "0", "1", "Yes", or "No" (case does not matter).

To read (query) the value of a configuration setting, you must call Config("PROPERTY"). The value will be returned as a string.

CreateKey Method (Elgamal Class)

Creates a new key.

Syntax

public void createKey();

Remarks

This method creates a new public and private key.

When calling CreateKey the Key property is populated with a new private and public key.

An Elgamal key is made up of a number of individual parameters.

The public key consists of the following parameters:

The class also includes the PublicKey field which holds the PEM formatted public key for ease of use. This is helpful if you are in control of both signature creation and verification process. When sending the public key to a recipient note that not all implementations will support using the PEM formatted value in PublicKey in which case the individual parameters must be sent.

The private key consists of the following parameters:

The class also include the PrivateKey field which holds the PEM formatted private key for ease of use. This is helpful for storing the private key more easily.

Decrypt Method (Elgamal Class)

Decrypts the input data using the specified private key.

Syntax

public void decrypt();

Remarks

This method decrypts the input data using the private key specified in Key. Alternatively, a certificate may be specified by setting Certificate

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

Key Size and the Maximum Length of Data

RSA has an upper limit to the amount of data that can be encrypted or decrypted, also known as message length. This can typically be calculated as the size of the key minus the size of the RSA header and padding.

When not using OAEP, the following formula and table can be referenced. (RSA Key Bytes) - (Header Bytes) = Length of data, where Header Bytes is always 11.

RSA Key Length (bits)Length (bits)Length (bytes)
1024 936 117
2048 1960 245
3072 2984 373
4096 4008 501

When using OAEP, the following formula and table can be referenced. (RSA Key Bytes) - (2 * Hash Length Bytes) - 2 = Length of data. The table below assumes SHA-256 for the hash, so Hash Length Bytes is 32.

RSA Key Length (bits)Length (bits)Length (bytes)
1024 496 62
2048 1520 190
3072 2544 318
4096 3568 446

Encrypt Method (Elgamal Class)

Encrypts the input data using the recipient's public key.

Syntax

public void encrypt();

Remarks

This method encrypts the input data using the public key specified in RecipientKey. Alternatively, a certificate may be specified by setting RecipientCert

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

Key Size and the Maximum Length of Data

RSA has an upper limit to the amount of data that can be encrypted or decrypted, also known as message length. This can typically be calculated as the size of the key minus the size of the RSA header and padding.

When not using OAEP, the following formula and table can be referenced. (RSA Key Bytes) - (Header Bytes) = Length of data, where Header Bytes is always 11.

RSA Key Length (bits)Length (bits)Length (bytes)
1024 936 117
2048 1960 245
3072 2984 373
4096 4008 501

When using OAEP, the following formula and table can be referenced. (RSA Key Bytes) - (2 * Hash Length Bytes) - 2 = Length of data. The table below assumes SHA-256 for the hash, so Hash Length Bytes is 32.

RSA Key Length (bits)Length (bits)Length (bytes)
1024 496 62
2048 1520 190
3072 2544 318
4096 3568 446

Reset Method (Elgamal Class)

Resets the class.

Syntax

public void reset();

Remarks

When called, the class will reset all of its properties to their default values.

SetInputStream Method (Elgamal Class)

Sets the stream from which the class will read data to encrypt or decrypt.

Syntax

public void setInputStream(java.io.InputStream inputStream);

Remarks

This method sets the stream from which the class will read data to encrypt or decrypt.

Input and Output Properties

The class will determine the source and destination of the input and output based on which properties are set.

The order in which the input properties are checked is as follows:

When a valid source is found the search stops. The order in which the output properties are checked is as follows:

  • SetOutputStream
  • OutputFile
  • OutputMessage: The output data is written to this property if no other destination is specified.

When using streams you may need to additionally set CloseInputStreamAfterProcessing or CloseOutputStreamAfterProcessing.

Error Event (Elgamal Class)

Fired when information is available about errors during data delivery.

Syntax

public class DefaultElgamalEventListener implements ElgamalEventListener {
  ...
  public void error(ElgamalErrorEvent e) {}
  ...
}

public class ElgamalErrorEvent {
  public int errorCode;
  public String description;
}

Remarks

The Error event is fired in case of exceptional conditions during message processing. Normally the class throws an exception.

The ErrorCode parameter contains an error code, and the Description parameter contains a textual description of the error. For a list of valid error codes and their descriptions, please refer to the Error Codes section.

ElgamalKey Type

Contains the typical parameters for the Elgamal algorithm.

Remarks

This type is made up of fields that represent the private and public key parameters used by the Elgamal algorithm.

An Elgamal key is made up of a number of individual parameters.

The public key consists of the following parameters:

The class also includes the PublicKey field which holds the PEM formatted public key for ease of use. This is helpful if you are in control of both signature creation and verification process. When sending the public key to a recipient note that not all implementations will support using the PEM formatted value in PublicKey in which case the individual parameters must be sent.

The private key consists of the following parameters:

The class also include the PrivateKey field which holds the PEM formatted private key for ease of use. This is helpful for storing the private key more easily.

Fields

G
String

Default Value: ""

Represents the G parameter for the Elgamal algorithm.

GB
byte[]

Default Value: ""

Represents the G parameter for the Elgamal algorithm.

P
String

Default Value: ""

Represents the P parameter for the Elgamal algorithm.

PB
byte[]

Default Value: ""

Represents the P parameter for the Elgamal algorithm.

PrivateKey
String

Default Value: ""

This field is a PEM formatted private key. The purpose of this field is to allow easier management of the private key parameters by using only a single value.

PublicKey
String

Default Value: ""

This field is a PEM formatted public key. The purpose of this field is to allow easier management of the public key parameters by using only a single value.

X
String

Default Value: ""

Represents the X parameter for the Elgamal algorithm.

XB
byte[]

Default Value: ""

Represents the X parameter for the Elgamal algorithm.

Y
String

Default Value: ""

Represents the Y parameter for the Elgamal algorithm.

YB
byte[]

Default Value: ""

Represents the Y parameter for the Elgamal algorithm.

Constructors

public ElgamalKey();

The default constructor creates a new ElgamalKey instance but does not assign a public or private key.

public ElgamalKey( P,  G,  Y);

The public key constructor assigns an existing public key.

public ElgamalKey( P,  G,  Y,  X);

The private key constructor assigns an existing private key.

Config Settings (Elgamal Class)

The class accepts one or more of the following configuration settings. Configuration settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the class, access to these internal properties is provided through the Config method.

Elgamal Config Settings

CloseInputStreamAfterProcessing:   Determines whether or not the input stream is closed after processing.

Determines whether or not the input stream set by SetInputStream is closed after processing is complete. The default value is True.

CloseOutputStreamAfterProcessing:   Determines whether or not the output stream is closed after processing.

Determines whether or not the output stream set by SetOutputStream is closed after processing is complete. The default value is True.

KeySize:   The size, in bits, of the secret key.

This specifies the size, in bits, of the secret key. The minimum key size for Elgamal is 512. The maximum key size is 4096. Note that large values such as 4096 will impact performance. The default value is 1024.

Base Config Settings

BuildInfo:   Information about the product's build.

When queried, this setting will return a string containing information about the product's build.

GUIAvailable:   Whether or not a message loop is available for processing events.

In a GUI-based application, long-running blocking operations may cause the application to stop responding to input until the operation returns. The class will attempt to discover whether or not the application has a message loop and, if one is discovered, it will process events in that message loop during any such blocking operation.

In some non-GUI applications, an invalid message loop may be discovered that will result in errant behavior. In these cases, setting GUIAvailable to false will ensure that the class does not attempt to process external events.

LicenseInfo:   Information about the current license.

When queried, this setting will return a string containing information about the license this instance of a class is using. It will return the following information:

  • Product: The product the license is for.
  • Product Key: The key the license was generated from.
  • License Source: Where the license was found (e.g., RuntimeLicense, License File).
  • License Type: The type of license installed (e.g., Royalty Free, Single Server).
  • Last Valid Build: The last valid build number for which the license will work.
MaskSensitive:   Whether sensitive data is masked in log messages.

In certain circumstances it may be beneficial to mask sensitive data, like passwords, in log messages. Set this to true to mask sensitive data. The default is true.

This setting only works on these classes: AS3Receiver, AS3Sender, Atom, Client(3DS), FTP, FTPServer, IMAP, OFTPClient, SSHClient, SCP, Server(3DS), Sexec, SFTP, SFTPServer, SSHServer, TCPClient, TCPServer.

UseDaemonThreads:   Whether threads created by the class are daemon threads.

If set to True (default), when the class creates a thread, the thread's Daemon property will be explicitly set to True. When set to False, the class will not set the Daemon property on the created thread. The default value is True.

UseInternalSecurityAPI:   Whether or not to use the system security libraries or an internal implementation.

When set to false, the class will use the system security libraries by default to perform cryptographic functions where applicable.

Setting this configuration setting to true tells the class to use the internal implementation instead of using the system security libraries.

This setting is set to false by default on all platforms.

Trappable Errors (Elgamal Class)

Elgamal Errors

102   No Key specified.
104   Cannot read or write file.
105   key parameters incorrect.
111   OutputFile already exists and Overwrite is False.
112   Invalid length of input message.
304   Cannot write file.
305   Cannot read file.
306   Cannot create file.