RC2 ExampleRC2 ("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. [Lecture] [Tutorial] [Paper] Encrypted (Base-64) Encrypted (Hex) Decrypted |
Code Used
public void rc2(string message, string inkey) { bool file = false; byte[] b = null; if (message == null) return; if (inkey == null) return; try { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); RC2CryptoServiceProvider rc2 = new System.Security.Cryptography.RC2CryptoServiceProvider(); if (inkey == "") { inkey = "test123"; } rc2.Key = StringToByte(inkey, 12); // convert to 12 characters rc2.IV = StringToByte("", 8); 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. if (file == false) { csEncrypt.Write(StringToByte(message), 0, StringToByte(message).Length); csEncrypt.FlushFinalBlock(); } else { csEncrypt.Write(b, 0, b.Length); csEncrypt.FlushFinalBlock(); } // Get the encrypted array of bytes. byte[] encrypted1 = msEncrypt.ToArray(); encrypted = ByteToString(encrypted1); ICryptoTransform decryptor = rc2.CreateDecryptor(key, IV); // Now decrypt the previously encrypted message using the decryptor MemoryStream msDecrypt = new MemoryStream(encrypted1); CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read); decrypted = ByteToString(csDecrypt); } catch (Exception ex) { encrypted = ex.Message.ToString(); } }