RC4 Component

Properties   Methods   Events   Config Settings   Errors  

Encrypt and decrypt data with the RC4 (ARC4 / ARCFOUR ) stream cipher.

Syntax

nsoftware.IPWorksCData.RC4

Remarks

The RC4 component is used to encrypt and decrypt data with the RC4 (ARC4 / ARCFOUR ) stream cipher.

To begin simply specify the data you wish to encrypt or decrypt.

Input and Output Properties

The component 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:

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

Before encrypting you must have a valid Key and IV. There are a few options available to you in regards to key management. The easiest option is to simply set KeyPassword. When KeyPassword is set the component will automatically create a Key and IV using the PKCS5 password digest algorithm. This means there is only one value you need to keep track of.

If you wish to have more control over the Key and IV values you may specify the properties yourself. If IV is left empty, one will be created for you when you call Encrypt or Decrypt.

A simple example: Component.InputFile = "C:\MyFile.txt"; Component.OutputFile = "C:\Encrypted.txt"; Component.KeyPassword = "password"; Component.Encrypt();

Property List


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

InputFile The file to process.
InputMessage The message to process.
IV The initialization vector (IV).
Key The secret key for the symmetric algorithm.
KeyPassword A password to generate the Key and IV .
OutputFile The output file when encrypting or decrypting.
OutputMessage The output message after processing.
Overwrite Indicates whether or not the component should overwrite files.
UseHex Whether input or output is hex encoded.

Method List


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

Config Sets or retrieves a configuration setting.
Decrypt Decrypts the data.
DecryptBlock Decrypts a block and returns the decrypted data.
Encrypt Encrypts the data.
EncryptBlock Encrypts data and returns the encrypted block.
Reset Resets the component.
SetInputStream Sets the stream from which the component will read data to encrypt or decrypt.
SetOutputStream Sets the stream to which the component will write encrypted or decrypted data.

Event List


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

Error Fired when information is available about errors during data delivery.
Progress Fired as progress is made.

Config Settings


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

CloseInputStreamAfterProcessing Determines whether or not the input stream is closed after processing.
CloseOutputStreamAfterProcessing Determines whether or not the output stream is closed after processing.
KeySize The size, in bits, of secret key for the symmetric algorithm.

InputFile Property (RC4 Component)

The file to process.

Syntax

public string InputFile { get; set; }
                
Public Property InputFile As String
                

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 component 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:

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

InputMessage Property (RC4 Component)

The message to process.

Syntax

public string InputMessage { get; set; }
public byte[] InputMessageB { get; set; }
                
Public Property InputMessage As String
Public Property InputMessageB As Byte()
                

Default Value

""

Remarks

This property specifies the message to be processed.

Input and Output Properties

The component 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:

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

IV Property (RC4 Component)

The initialization vector (IV).

Syntax

public string IV { get; set; }
public byte[] IVB { get; set; }
                
Public Property IV As String
Public Property IVB As Byte()
                

Default Value

""

Remarks

This property specifies the initialization vector (IV). By default this property is empty and the component will automatically generate a new IV value if KeyPassword or Key is set before Encrypt or EncryptBlock is called. The size of the IV property must be equal to the BlockSize divided by 8.

Key Property (RC4 Component)

The secret key for the symmetric algorithm.

Syntax

public string Key { get; set; }
public byte[] KeyB { get; set; }
                
Public Property Key As String
Public Property KeyB As Byte()
                

Default Value

""

Remarks

This secret key is used both for encryption and decryption. The secret key should be known only to the sender and the receiver. The legal key size varies depending on the algorithm.

If this property is left empty and KeyPassword is specified, a Key value will be generated by the component as necessary.

Legal Key and Block Sizes (in bits)

AES Rijndael CAST DES IDEA RC2 RC4 TripleDES Blowfish Twofish TEA
Minimum Key Size 128 128 112 64 128 112 112 128 112 128 128
Maximum Key Size 256 256 128 64 128 128 2048 192 448 256 128
Key Size Step 64 64 8 0 0 8 8 64 1 8 0
Block Size 128 128/192/256 64 64 64 64 N/A 64 64 128 64*

Note: When using TEA if Algorithm is set to XXTEA valid block sizes are 64 + n * 32. Where n is any positive integer.

The default KeySize is the Maximum Key Size.

KeyPassword Property (RC4 Component)

A password to generate the Key and IV .

Syntax

public string KeyPassword { get; set; }
                
Public Property KeyPassword As String
                

Default Value

""

Remarks

When this property is set the component will calculate values for Key and IV using the PKCS5 password digest algorithm. This provides a simpler alternative to creating and managing Key and IV values directly.

The size of the Key generated is dependent on the value of KeySize.

OutputFile Property (RC4 Component)

The output file when encrypting or decrypting.

Syntax

public string OutputFile { get; set; }
                
Public Property OutputFile As String
                

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 component 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:

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

OutputMessage Property (RC4 Component)

The output message after processing.

Syntax

public string OutputMessage { get; }
public byte[] OutputMessageB { get; }
                
Public ReadOnly Property OutputMessage As String
Public ReadOnly Property OutputMessageB As Byte()
                

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 component 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:

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 (RC4 Component)

Indicates whether or not the component should overwrite files.

Syntax

public bool Overwrite { get; set; }
                
Public Property Overwrite As Boolean
                

Default Value

False

Remarks

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

UseHex Property (RC4 Component)

Whether input or output is hex encoded.

Syntax

public bool UseHex { get; set; }
                
Public Property UseHex As Boolean
                

Default Value

False

Remarks

This property specifies whether the encrypted data is hex encoded.

If set to True, when Encrypt is called the component 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 component will expect InputMessage or InputFile to hold hex encoded data. The component will then hex decode the data and perform decryption as normal.

Config Method (RC4 Component)

Sets or retrieves a configuration setting.

Syntax

public string Config(string configurationString);
                
Public Function Config(ByVal ConfigurationString As String) As String
                

Remarks

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

These settings are similar in functionality to properties, but they are rarely used. In order to avoid "polluting" the property namespace of the component, 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.

Decrypt Method (RC4 Component)

Decrypts the data.

Syntax

public void Decrypt();
                
Public Sub Decrypt()
                

Remarks

This method will decrypt the specified data. The following properties are applicable:

  • IV (required)
  • Key (required)
  • CipherMode
  • PaddingMode

Note that CipherMode must be set to the same value used during encryption or the results may be unexpected. If the CipherMode value does not match the value used during encryption the operation may succeed but the decrypted data may not be correct.

Input and Output Properties

The component 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:

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

DecryptBlock Method (RC4 Component)

Decrypts a block and returns the decrypted data.

Syntax

public byte[] DecryptBlock(byte[] inputBuffer);
                
Public Function DecryptBlock(ByVal InputBuffer As Byte()) As Byte()
                

Remarks

This method decrypted the data specified by InputBuffer and returns the decrypted data.

Encrypt Method (RC4 Component)

Encrypts the data.

Syntax

public void Encrypt();
                
Public Sub Encrypt()
                

Remarks

This method will encrypt the specified data. The following properties are applicable:

  • IV (required)
  • Key (required)
  • PaddingMode
  • CipherMode

Input and Output Properties

The component 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:

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

EncryptBlock Method (RC4 Component)

Encrypts data and returns the encrypted block.

Syntax

public byte[] EncryptBlock(byte[] inputBuffer);
                
Public Function EncryptBlock(ByVal InputBuffer As Byte()) As Byte()
                

Remarks

This method encrypts the data specified by InputBuffer and returns the encrypted block.

Reset Method (RC4 Component)

Resets the component.

Syntax

public void Reset();
                
Public Sub Reset()
                

Remarks

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

SetInputStream Method (RC4 Component)

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

Syntax

public void SetInputStream(System.IO.Stream inputStream);
                
Public Sub SetInputStream(ByVal InputStream As System.IO.Stream)
                

Remarks

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

Input and Output Properties

The component 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:

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

SetOutputStream Method (RC4 Component)

Sets the stream to which the component will write encrypted or decrypted data.

Syntax

public void SetOutputStream(System.IO.Stream outputStream);
                
Public Sub SetOutputStream(ByVal OutputStream As System.IO.Stream)
                

Remarks

This method sets the stream to which the component will write encrypted or decrypted data.

Input and Output Properties

The component 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 (RC4 Component)

Fired when information is available about errors during data delivery.

Syntax

public event OnErrorHandler OnError;

public delegate void OnErrorHandler(object sender, RC4ErrorEventArgs e);

public class RC4ErrorEventArgs : EventArgs {
  public int ErrorCode { get; }
  public string Description { get; }
}
                
Public Event OnError As OnErrorHandler

Public Delegate Sub OnErrorHandler(sender As Object, e As RC4ErrorEventArgs)

Public Class RC4ErrorEventArgs Inherits EventArgs
  Public ReadOnly Property ErrorCode As Integer
  Public ReadOnly Property Description As String
End Class
                

Remarks

The Error event is fired in case of exceptional conditions during message processing. Normally the component 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.

Progress Event (RC4 Component)

Fired as progress is made.

Syntax

public event OnProgressHandler OnProgress;

public delegate void OnProgressHandler(object sender, RC4ProgressEventArgs e);

public class RC4ProgressEventArgs : EventArgs {
  public long BytesProcessed { get; }
  public int PercentProcessed { get; }
}
                
Public Event OnProgress As OnProgressHandler

Public Delegate Sub OnProgressHandler(sender As Object, e As RC4ProgressEventArgs)

Public Class RC4ProgressEventArgs Inherits EventArgs
  Public ReadOnly Property BytesProcessed As Long
  Public ReadOnly Property PercentProcessed As Integer
End Class
                

Remarks

This event is fired automatically as data is processed by the component.

The PercentProcessed parameter indicates the current status of the operation.

The BytesProcessed parameter holds the total number of bytes processed so far.

Config Settings (RC4 Component)

The component 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 component, access to these internal properties is provided through the Config method.

RC4 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 secret key for the symmetric algorithm.

The legal key sizes vary depending on the algorithm. The KeySize and BlockSize configuration settings may be set to specify the key and block size (in bits).

This setting is only applicable when KeyPassword is specified.

Note that when using the EzCrypt component, KeySize should be set after setting the Algorithm property.

Trappable Errors (RC4 Component)

RC4 Errors

101   Unsupported algorithm.
102   No Key specified.
103   No IV specified.
104   Cannot read or write file.
107   Block size is not valid for this algorithm.
108   Key size is not valid for this algorithm.
111   OutputFile already exists and Overwrite is False.
121   The specified key is invalid.
123   IV size is not valid for this algorithm.
304   Cannot write file.
305   Cannot read file.
306   Cannot create file.
2004   Invalid padding. This may be an indication that the key is incorrect.