public void DecryptPartial_TransformFinalBlock_2Pass () { byte[] key = {0, 1, 2, 3, 4, 5, 6, 7}; byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7}; DES des = DES.Create (); byte[] data = Encoding.Unicode.GetBytes ("ximian"); // 12 bytes, 1.5 DES block size DebugStream encrypted = new DebugStream (); cs = new CryptoStream (encrypted, des.CreateEncryptor (key, iv), CryptoStreamMode.Write); cs.Write (data, 0, data.Length); cs.Close (); data = encrypted.ToArray (); DebugStream decrypted = new DebugStream (data); cs = new CryptoStream (decrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read); int len = cs.Read (data, 0, 6); Assert.AreEqual (6, len, "Length (1st pass)"); Assert.AreEqual ("xim", Encoding.Unicode.GetString (data, 0, len), "Partial DES Roundtrip"); len += cs.Read (data, 6, 8); Assert.AreEqual (12, len, "Length (1st+2nd)"); Assert.AreEqual ("ximian", Encoding.Unicode.GetString (data, 0, len), "Full DES Roundtrip"); cs.Close (); }
public void WriteByteReadByte () { DebugStream original = new DebugStream (Encoding.Unicode.GetBytes ("ximian")); DebugStream encrypted = new DebugStream (); byte[] key = {0, 1, 2, 3, 4, 5, 6, 7}; byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7}; DES des = DES.Create (); cs = new CryptoStream (encrypted, des.CreateEncryptor (key, iv), CryptoStreamMode.Write); int data; while ((data = original.ReadByte ()) != -1) cs.WriteByte((byte) data); cs.Close (); byte[] result = encrypted.ToArray (); Assert.AreEqual ("18-EA-93-3F-20-86-D2-AA-78-02-D7-6F-E4-47-17-9C", BitConverter.ToString (result), "Encrypted"); encrypted = new DebugStream (result); DebugStream decrypted = new DebugStream (); cs = new CryptoStream (encrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read); while ((data = cs.ReadByte ()) != -1) decrypted.WriteByte((byte) data); cs.Close (); decrypted.Close (); Assert.AreEqual ("ximian", Encoding.Unicode.GetString (decrypted.ToArray ()), "W/R Byte Roundtrip"); }
// Adapted from Subba Rao Thirumoorthy email on mono-devel-list (december 2003) private byte[] NonMultipleOfBlockSize_Encrypt (ICryptoTransform ct, byte[] data) { DebugStream stream = new DebugStream (); CryptoStream CryptStream = new CryptoStream (stream, ct, CryptoStreamMode.Write); int len = 0; long myLength = 0; byte[] Buffer = new byte [1024]; DebugStream fout = new DebugStream (data); while (myLength < data.Length) { len = fout.Read (Buffer, 0, 1023); if (len == 0) break; CryptStream.Write (Buffer, 0, len); CryptStream.Flush (); myLength = myLength + len; } CryptStream.FlushFinalBlock (); // we must ensure that the result is correct Assert.AreEqual (64, len, "Length(final)"); byte[] result = stream.ToArray (); string end = BitConverter.ToString (result, 65520, 16); Assert.AreEqual ("04-70-19-1D-28-C5-BD-9A-23-C6-60-E2-28-96-38-65", end, "End part"); CryptStream.Close(); stream.Close(); return result; }
private byte[] NonMultipleOfBlockSize_Decrypt (ICryptoTransform ct, byte[] data) { DebugStream stream = new DebugStream (data); CryptoStream CryptStream = new CryptoStream (stream, ct, CryptoStreamMode.Read); int len = 0; long myLength = 0; byte[] Buffer = new Byte [1024]; DebugStream fout = new DebugStream (); // each returned block must be 1023 bytes long // even if this isn't a multiple of the block size while ((len = CryptStream.Read (Buffer, 0, 1023)) != 0) { fout.Write (Buffer, 0, len); fout.Flush (); myLength = myLength + len; } byte[] result = fout.ToArray (); CryptStream.Close (); stream.Close (); return result; }
public void PartialRoundtripRead () { byte[] encrypted; using (DebugStream mem1 = new DebugStream ()) { byte[] toEncrypt = Encoding.Unicode.GetBytes ("Please encode me!"); using (CryptoStream crypt = new CryptoStream (mem1, aes.CreateEncryptor (), CryptoStreamMode.Write)) { crypt.Write (toEncrypt, 0, toEncrypt.Length); crypt.FlushFinalBlock (); } encrypted = mem1.ToArray (); } using (DebugStream mem2 = new DebugStream (encrypted)) { byte[] buffer = new byte [1024]; CryptoStream cr = new CryptoStream (mem2, aes.CreateDecryptor (), CryptoStreamMode.Read); int len = cr.Read (buffer, 0, 20); cr.Clear (); cr.Close (); Assert.AreEqual (20, len, "Partial Length Read"); Assert.AreEqual ("Please enc", Encoding.Unicode.GetString (buffer, 0, len), "Partial Block Read"); } }
public void CascadedCryptoStream_Write () { DebugStream debug = new DebugStream (); // calculate both the hash (before encryption) and encrypt in one Write operation byte[] key = {0, 1, 2, 3, 4, 5, 6, 7}; byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7}; DES des = DES.Create (); CryptoStream cse = new CryptoStream (debug, des.CreateEncryptor (key, iv), CryptoStreamMode.Write); MD5 hash = MD5.Create (); CryptoStream csh = new CryptoStream (cse, hash, CryptoStreamMode.Write); byte[] data = Encoding.UTF8.GetBytes ("http://www.go-mono.com/"); csh.Write (data, 0, data.Length); csh.FlushFinalBlock (); byte[] result = debug.ToArray (); Assert.AreEqual ("8C-24-76-74-09-79-2B-D3-47-C3-32-F5-F3-1A-5E-57-04-33-2E-B8-50-77-B2-A1", BitConverter.ToString (result), "Encrypted"); byte[] digest = hash.Hash; Assert.AreEqual ("71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest), "Hash"); }
public void ToBase64_Write () { byte[] data = Encoding.UTF8.GetBytes ("http://www.go-mono.com/"); DebugStream debug = new DebugStream (); ICryptoTransform base64 = new ToBase64Transform (); cs = new CryptoStream (debug, base64, CryptoStreamMode.Write); cs.Write (data, 0, data.Length); cs.FlushFinalBlock (); byte[] encoded = debug.ToArray (); string result = Encoding.UTF8.GetString (encoded); Assert.AreEqual ("aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result, "ToBase64_Write"); }
public void FromBase64_Write () { string expected = "http://www.go-mono.com/"; byte[] data = Encoding.UTF8.GetBytes (expected); string temp = Convert.ToBase64String (data, 0, data.Length); data = Encoding.UTF8.GetBytes (temp); DebugStream debug = new DebugStream (); ICryptoTransform base64 = new FromBase64Transform (); cs = new CryptoStream (debug, base64, CryptoStreamMode.Write); cs.Write (data, 0, data.Length); cs.FlushFinalBlock (); byte[] encoded = debug.ToArray (); string result = Encoding.UTF8.GetString (encoded); Assert.AreEqual (expected, result, "FromBase64_Write"); }
public void DecryptPartial_TransformFinalBlock_required () { byte[] key = {0, 1, 2, 3, 4, 5, 6, 7}; byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7}; DES des = DES.Create (); byte[] data = Encoding.Unicode.GetBytes ("ximian"); // 12 bytes, 1.5 DES block size DebugStream encrypted = new DebugStream (); cs = new CryptoStream (encrypted, des.CreateEncryptor (key, iv), CryptoStreamMode.Write); cs.Write (data, 0, data.Length); cs.Close (); data = encrypted.ToArray (); DebugStream decrypted = new DebugStream (data); cs = new CryptoStream (decrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read); int len = cs.Read (data, 0, data.Length); cs.Close (); AssertEquals ("Length", 12, len); AssertEquals ("Unicode DES Roundtrip", "ximian", Encoding.Unicode.GetString (data, 0, len)); }