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 includes 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.
MaskSensitiveDataWhether sensitive data is masked in log messages.
UseDaemonThreadsWhether threads created by the class are daemon threads.
UseFIPSCompliantAPITells the class whether or not to use FIPS certified APIs.
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 includes 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 includes 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 includes 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 includes the PrivateKey field which holds the PEM formatted private key for ease of use. This is helpful for storing the private key more easily.

The following fields are available:

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.
MaskSensitiveData:   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.

UseFIPSCompliantAPI:   Tells the class whether or not to use FIPS certified APIs.

When set to true, the class will utilize the underlying operating system's certified APIs. Java editions, regardless of OS, utilize Bouncy Castle Federal Information Processing Standards (FIPS), while all other Windows editions make use of Microsoft security libraries.

The Java edition requires installation of the FIPS-certified Bouncy Castle library regardless of the target operating system. This can be downloaded from https://www.bouncycastle.org/fips-java/. Only the "Provider" library is needed. The jar file should then be installed in a JRE search path.

The following classes must be imported in the application in which the component will be used:

import java.security.Security; import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider;

The Bouncy Castle provider must be added as a valid provider and must also be configured to operate in FIPS mode:

System.setProperty("org.bouncycastle.fips.approved_only","true"); Security.addProvider(new BouncyCastleFipsProvider());

When UseFIPSCompliantAPI is true, Secure Sockets Layer (SSL)-enabled classes can optionally be configured to use the Transport Layer Security (TLS) Bouncy Castle library. When SSLProvider is set to sslpAutomatic (default) or sslpInternal, an internal TLS implementation is used, but all cryptographic operations are offloaded to the Bouncy Castle FIPS provider to achieve FIPS-compliant operation. If SSLProvider is set to sslpPlatform, the Bouncy Castle JSSE will be used in place of the internal TLS implementation.

To enable the use of the Bouncy Castle JSSE take the following steps in addition to the steps above. Both the Bouncy Castle FIPS provider and the Bouncy Castle JSSE must be configured to use the Bouncy Castle TLS library in FIPS mode. Obtain the Bouncy Castle TLS library from https://www.bouncycastle.org/fips-java/. The jar file should then be installed in a JRE search path.

The following classes must be imported in the application in which the component will be used:

import java.security.Security; import org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider; //required to use BCJSSE when SSLProvider is set to sslpPlatform import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;

The Bouncy Castle provider must be added as a valid provider and also must be configured to operate in FIPS mode:

System.setProperty("org.bouncycastle.fips.approved_only","true"); Security.addProvider(new BouncyCastleFipsProvider()); //required to use BCJSSE when SSLProvider is set to sslpPlatform Security.addProvider(new BouncyCastleJsseProvider("fips:BCFIPS")); //optional - configure logging level of BCJSSE Logger.getLogger("org.bouncycastle.jsse").setLevel(java.util.logging.Level.OFF); //configure the class to use BCJSSE component.setSSLProvider(1); //platform component.config("UseFIPSCompliantAPI=true"); Note: TLS 1.3 support requires the Bouncy Castle TLS library version 1.0.14 or later.

FIPS mode can be enabled by setting the UseFIPSCompliantAPI configuration setting to true. This is a static setting that applies to all instances of all classes of the toolkit within the process. It is recommended to enable or disable this setting once before the component has been used to establish a connection. Enabling FIPS while an instance of the component is active and connected may result in unexpected behavior.

For more details, please see the FIPS 140-2 Compliance article.

Note: Enabling FIPS compliance requires a special license; please contact sales@nsoftware.com for details.

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.