Skip Navigation Links.

RC2

RC2 ("Rivest Cipher") is seen as a replacement for DES. It was created by Ron Rivest in 1987, and is a 64-bit block code and can have a key size from 40 bits to 128-bits (in increments of 8 bits). The 40-bit key version is seen as weak, as the encryption key is so small, but is favoured by governments for export purposes, as it can be easily cracked. In this case the key is created from a Key and an IV (Initialisation Vector). The Key has 12 characters (96 bits), and the IV has 8 characters (64 bits), which go to make the overall key.

Message:

Encryption key:

Encrypted stream:

 

and to test, the decrypted text is:

 

As a test: "Hello how are you?" with a key of "test" should give:

79D9FF181FCF8416F16B3C766D819B8D483EAA045FF722DE

Also with a test message of "test", and a key of "test", we get:

8F037B11569437CC

which it can be seen has 16 hex characters, which relates to 64-bits (as each hex character is 4 bits), thus it can be seen that the block size is 64-bits (as "test" only fills-up one block).

The code is:

 

protected void Button3_Click(object sender, EventArgs e)
{
string message;

message = this.tbMessage.Text;

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

RC2CryptoServiceProvider rc2 = new System.Security.Cryptography.RC2CryptoServiceProvider();

rc2.Key = StringToByte(this.tbKey.Text,12); // convert to 12 characters
rc2.IV = StringToByte(" ");
byte[] key = rc2.Key;
byte[] IV = rc2.IV;

ICryptoTransform encryptor = rc2.CreateEncryptor(key, IV);

MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

// Write all data to the crypto stream and flush it.
csEncrypt.Write(StringToByte(this.tbMessage.Text), 0, StringToByte(this.tbMessage.Text).Length);
csEncrypt.FlushFinalBlock();

// Get the encrypted array of bytes.
byte[] encrypted = msEncrypt.ToArray();

this.tbEncrypt.Text = ByteToString(encrypted);

ICryptoTransform decryptor = rc2.CreateDecryptor(key, IV);

// Now decrypt the previously encrypted message using the decryptor
MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

this.tbDecrypt.Text = ByteToString(csDecrypt);


}
public static byte[] StringToByte(string StringToConvert)
{

char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[CharArray.Length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}
public static byte[] StringToByte(string StringToConvert, int length)
{

char[] CharArray = StringToConvert.ToCharArray();
byte[] ByteArray = new byte[length];
for (int i = 0; i < CharArray.Length; i++)
{
ByteArray[i] = Convert.ToByte(CharArray[i]);
}
return ByteArray;
}

public static string ByteToString(CryptoStream buff)
{
string sbinary = "";
int b = 0;
do
{
b = buff.ReadByte();
if (b!=-1) sbinary+=((char)b);

} while (b != -1);
return (sbinary);
}
public static string ByteToString(byte [] buff)
{
string sbinary="";
for (int i=0;i<buff.Length;i++)
{
sbinary+=buff[i].ToString("X2"); // hex format
}
return(sbinary);
}