3DES Example (with clear text keys)In this example we will generate the 3DES key from three components. Example: 447FC2AA6EFFFEE5405A559E88DC958C, 1086F0493DB0EFE42EDF1BC99541E96F and D1C603D64D1EDC9D3CA78CD95D168E40, and which are X-ORed together to give a key of 853F31351E51CD9C5222C28E408BF2A3: [3DES][Try!] The results are then:
|
Test Vectors
If we use ECB, we do not use the IV. The following are test vectors:
- Message= "The quick brown fox jumped over the lazy dog" and key 447FC2AA6EFFFEE5405A559E88DC958C, 1086F0493DB0EFE42EDF1BC99541E96F and D1C603D64D1EDC9D3CA78CD95D168E40 gives a key of 853F31351E51CD9C5222C28E408BF2A3 should gives a key of 447FC2AA6EFFFEE5405A559E88DC958C, 1086F0493DB0EFE42EDF1BC99541E96F and D1C603D64D1EDC9D3CA78CD95D168E40 gives a key of 853F31351E51CD9C5222C28E408BF2A3: Ref. Try!
Key generation
The key is generated by X-OR'ing each byte in the sub-keys:
public static byte[] gendKey(byte[] key1, byte[] key2, byte[] key3) { byte[] result = new byte[cc1.Length]; int i = 0; foreach (byte by1 in key1) { byte by2 = key2[i]; byte by3 = key3[i]; result[i] = (byte)(yb1 ^ by2 ^ by3); i++; } return result; }
Here is the code to encrypt and decrypt:
using System; using System.Security.Cryptography; using System.IO; class MainClass { public static void Main (string[] args) { String meth="cbc"; String iv = "12345678"; String inkey = "447FC2AA6EFFFEE5405A559E88DC958C"; String message="Hello"; String encrypted=""; String decrypted=""; try { TripleDES threedes = new TripleDESCryptoServiceProvider(); if (meth == "cbc") threedes.Mode = CipherMode.CBC; else if (meth == "ofb") threedes.Mode = CipherMode.OFB; else if (meth == "cbf") threedes.Mode = CipherMode.CFB; else threedes.Mode = CipherMode.ECB; threedes.Key = StringToByte(inkey, 24); // convert to 24 characters - 192 bits threedes.IV = StringToByte(iv); byte[] key = threedes.Key; byte[] IV = threedes.IV; ICryptoTransform encryptor = threedes.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(message), 0, StringToByte(message).Length); csEncrypt.FlushFinalBlock(); // Get the encrypted array of bytes. byte[] encrypted1 = msEncrypt.ToArray(); encrypted = ByteToString(encrypted1); ICryptoTransform decryptor = threedes.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); StreamReader reader = new StreamReader(csDecrypt); decrypted = reader.ReadToEnd(); Console.Write("Message: {0}\n",message); Console.Write("Encrypted: {0}\n",encrypted); Console.Write("Decrypted: {0}",decrypted); } catch (Exception ex) { encrypted = "Error:" + ex.Message.ToString(); } } 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 < length; i++) { try { ByteArray[i] = Convert.ToByte(CharArray[i]); } catch (Exception ex) { ByteArray[i] = 0; } } return ByteArray; } 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); } }