Encrypting and Decrypting with RC4

This article shows how to encrypt and decrypt data using the RC4 component of IPWorks Encrypt.

Date Entered: 11/12/2014    Last Updated: 11/12/2014

IPWorks Encrypt makes it very easy to encrypt and decrypt data. This article will detail the process of performing RC4 encryption and decryption.

RC4 Encryption

RC4 Encryption is as simple as selecting a KeyPassword and specifying the data to be encrypted. Please see the following example:

Rc4 rc4 = new Rc4(); rc4.KeyPassword = "MyKeyPassword"; rc4.InputFile = "C:\\Path\\to\\UnEncryptedFile.txt"; rc4.OutputFile = "C:\\Path\\to\\EncryptedFile.dat"; rc4.Encrypt();

RC4 Decryption

RC4 Decryption is just as simple as RC4 Encryption. All that is needed is the encrypted data and the KeyPassword that was used during the encryption.

Rc4 rc4 = new Rc4(); rc4.KeyPassword = "MyKeyPassword"; rc4.InputFile = "C:\\Path\\to\\EncryptedFile.dat"; rc4.OutputFile = "C:\\Path\\to\\DecryptedFile.txt"; rc4.Decrypt();

Changing the Key Size

The RC4 Encryption algorithm supports keys of sizes from 8 bits to 2048 bits in increments of 8 (e.g., 8, 16, 24, 32, etc). You can specify the key size to be used for Encryption and Decryption via the "KeySize" configuration setting as detailed below.

rc4.Config("KeySize=192"); //Set Key Size to 192 bits.

Note: The KeySize configuration option is only valid when KeyPassword is set and the component automatically generates the Key and IV values.

Configuring RC4 to use Hex-encoded strings

The RC4 component can be instructed to use Hex-encoded strings for its encryption and decryption operations. When the UseHex property is set to true, the Encrypt operation will Hex-encode the encrypted data. Similarly, if the UseHex property is set to true, the Decrypt operation will assume that the encrypted data has been Hex-encoded.

Additional Options

The RC4 component supports a variety of options to offer more control over the encryption and decryption mechanisms used. Options include the ability to specify the Cipher Mode and Padding Mode used, as well as specifying the Key and IV values. Please see the IPWorks Encrypt help documentation for more information on these options.

We appreciate your feedback.  If you have any questions, comments, or suggestions about this entry please contact our support team at kb@nsoftware.com.