**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / Test / System.Security.Cryptography / CryptoStreamTest.cs
blobc61f06e69f877e2f0dbfa04bac2d8182e91407ca
1 //
2 // CryptoStreamTest.cs - NUnit Test Cases for CryptoStream
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 /* WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING *
32 * DO NOT USE ANY OF THE TEST CASE AS SAMPLES FOR YOUR OWN CODE. MANY
33 * CASES CONTAINS ERRORS AND AREN'T SECURE IN THEIR USE.
35 * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING * WARNING */
37 using NUnit.Framework;
38 using System;
39 using System.IO;
40 using System.Reflection;
41 using System.Runtime.InteropServices;
42 using System.Security.Cryptography;
43 using System.Text;
45 namespace MonoTests.System.Security.Cryptography {
47 // much useful for debugging
48 public class DebugStream : MemoryStream {
50 // constructor
52 public DebugStream () : base () {}
53 public DebugStream (byte[] buffer) : base (buffer) {}
54 public DebugStream (int capacity) : base (capacity) {}
56 public override bool CanRead {
57 get { return base.CanRead; }
60 public override bool CanSeek {
61 get { return base.CanSeek; }
64 public override bool CanWrite {
65 get { return base.CanWrite; }
68 public override int Capacity {
69 get { return base.Capacity; }
70 set { base.Capacity = value; }
73 public override long Length {
74 get { return base.Length; }
77 public override long Position {
78 get { return base.Position; }
79 set { base.Position = value; }
82 // methods
84 public override void Close ()
86 base.Close ();
89 public override void Flush ()
91 base.Flush ();
94 public override byte[] GetBuffer ()
96 return base.GetBuffer ();
99 public override int Read ([In,Out] byte[] buffer, int offset, int count)
101 int len = base.Read (buffer, offset, count);
102 return len;
105 public override int ReadByte ()
107 return base.ReadByte ();
110 public override long Seek (long offset, SeekOrigin loc)
112 return base.Seek (offset, loc);
115 public override void SetLength (long value)
117 base.SetLength (value);
120 public override byte[] ToArray ()
122 return base.ToArray ();
125 public override void Write (byte[] buffer, int offset, int count)
127 base.Write (buffer, offset, count);
130 public override void WriteByte (byte value)
132 base.WriteByte (value);
135 public override void WriteTo (Stream stream)
137 base.WriteTo (stream);
141 [TestFixture]
142 public class CryptoStreamTest : Assertion {
144 Stream readStream;
145 Stream writeStream;
146 ICryptoTransform encryptor;
147 ICryptoTransform decryptor;
148 CryptoStream cs;
149 SymmetricAlgorithm aes;
151 [SetUp]
152 public void SetUp ()
154 if (readStream == null) {
155 readStream = new FileStream ("read", FileMode.OpenOrCreate, FileAccess.Read);
156 writeStream = new FileStream ("write", FileMode.OpenOrCreate, FileAccess.Write);
157 aes = SymmetricAlgorithm.Create ();
158 encryptor = aes.CreateEncryptor ();
159 decryptor = aes.CreateEncryptor ();
163 [TearDown]
164 public void TearDown ()
166 try {
167 if (File.Exists ("read"))
168 File.Delete ("read");
169 if (File.Exists ("write"))
170 File.Delete ("write");
172 catch {}
175 public void AssertEquals (string msg, byte[] array1, byte[] array2)
177 AllTests.AssertEquals (msg, array1, array2);
180 [Test]
181 [ExpectedException (typeof (NullReferenceException))]
182 public void StreamNull ()
184 cs = new CryptoStream (null, encryptor, CryptoStreamMode.Read);
187 [Test]
188 [ExpectedException (typeof (NullReferenceException))]
189 public void TransformNull ()
191 MemoryStream write = new MemoryStream (8);
192 byte[] data = {0, 1, 2, 3, 4, 5, 6, 7};
193 cs = new CryptoStream (write, null, CryptoStreamMode.Write);
194 cs.Write (data, 0, 8);
197 [Test]
198 public void StreamReadModeRead ()
200 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
201 Assert ("Read.CanRead", cs.CanRead);
202 Assert ("Read.CanWrite", !cs.CanWrite);
203 Assert ("Read.CanSeek", !cs.CanSeek);
206 [Test]
207 [ExpectedException (typeof (ArgumentException))]
208 public void StreamReadModeWrite ()
210 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Write);
213 [Test]
214 [ExpectedException (typeof (ArgumentException))]
215 public void StreamWriteModeRead ()
217 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Read);
220 [Test]
221 public void StreamWriteModeWrite ()
223 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
224 Assert ("Read.CanRead", !cs.CanRead);
225 Assert ("Read.CanWrite", cs.CanWrite);
226 Assert ("Read.CanSeek", !cs.CanSeek);
229 [Test]
230 [ExpectedException (typeof (NotSupportedException))]
231 public void GetLength ()
233 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
234 long x = cs.Length;
237 [Test]
238 [ExpectedException (typeof (NotSupportedException))]
239 public void GetPosition ()
241 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
242 long x = cs.Position;
245 [Test]
246 [ExpectedException (typeof (NotSupportedException))]
247 public void SetPosition ()
249 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
250 cs.Position = 1;
253 [Test]
254 [ExpectedException (typeof (NotSupportedException))]
255 public void Seek ()
257 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
258 cs.Seek (0, SeekOrigin.Begin);
261 [Test]
262 [ExpectedException (typeof (NotSupportedException))]
263 public void SetLength ()
265 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
266 cs.SetLength (0);
269 [Test]
270 // LAMESPEC : [ExpectedException (typeof (NotSupportedException))]
271 public void FlushReadStream ()
273 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
274 cs.Flush ();
277 [Test]
278 [ExpectedException (typeof (NotSupportedException))]
279 public void FlushFinalBlockReadStream ()
281 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
282 cs.FlushFinalBlock ();
285 [Test]
286 [ExpectedException (typeof (NotSupportedException))]
287 public void FlushFinalBlock_Dual ()
289 byte[] data = {0, 1, 2, 3, 4, 5, 6, 7};
290 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
291 cs.Write (data, 0, data.Length);
292 cs.FlushFinalBlock ();
293 cs.FlushFinalBlock ();
296 [Test]
297 // LAMESPEC or MS BUG [ExpectedException (typeof (ObjectDisposedException))]
298 [ExpectedException (typeof (ArgumentNullException))]
299 public void FlushFinalBlock_Disposed ()
301 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
302 cs.Clear ();
303 cs.FlushFinalBlock ();
306 [Test]
307 // LAMESPEC or MS BUG [ExpectedException (typeof (ObjectDisposedException))]
308 [ExpectedException (typeof (ArgumentNullException))]
309 public void Read_Disposed ()
311 byte[] buffer = new byte [8];
312 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
313 cs.Clear ();
314 cs.Read (buffer, 0, 8);
317 [Test]
318 // MS BUG [ExpectedException (typeof (ObjectDisposedException))]
319 #if NET_2_0
320 [ExpectedException (typeof (IndexOutOfRangeException))]
321 #else
322 [Ignore ("Test cause System.ExecutionEngineException on MS runtime")]
323 #endif
324 public void Read_Disposed_Break ()
326 byte[] buffer = new byte [8];
327 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
328 int len = cs.Read (buffer, 0, 4);
329 AssertEquals ("Read 4", 4, len);
330 cs.Clear ();
331 len = cs.Read (buffer, 3, 4);
334 [Test]
335 [ExpectedException (typeof (NotSupportedException))]
336 public void Read_WriteStream ()
338 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
339 byte[] buffer = new byte [8];
340 cs.Read (buffer, 0, 8);
343 [Test]
344 // [ExpectedException (typeof (ArgumentNullException))]
345 [ExpectedException (typeof (NullReferenceException))]
346 public void Read_NullBuffer ()
348 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
349 cs.Read (null, 0, 8);
352 [Test]
353 public void Read_EmptyBuffer_ZeroCount ()
355 byte[] buffer = new byte [0];
356 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
357 int len = cs.Read (buffer, 0, 0);
358 AssertEquals ("Read 0", 0, len);
361 [Test]
362 [ExpectedException (typeof (ArgumentOutOfRangeException))]
363 public void Read_NegativeOffset ()
365 byte[] buffer = new byte [8];
366 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
367 cs.Read (buffer, -1, 8);
370 [Test]
371 public void Read_ZeroCount ()
373 byte[] buffer = new byte [8];
374 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
375 int len = cs.Read (buffer, 0, 0);
376 AssertEquals ("Read 0", 0, len);
379 [Test]
380 [ExpectedException (typeof (ArgumentOutOfRangeException))]
381 public void Read_NegativeCount ()
383 byte[] buffer = new byte [8];
384 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
385 cs.Read (buffer, 0, -1);
388 [Test]
389 [ExpectedException (typeof (ArgumentException))]
390 public void Read_OverflowCount ()
392 byte[] buffer = new byte [8];
393 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
394 cs.Read (buffer, 0, Int32.MaxValue);
397 [Test]
398 [ExpectedException (typeof (ArgumentException))]
399 public void Read_InvalidOffset ()
401 byte[] buffer = new byte [8];
402 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
403 cs.Read (buffer, 5, 4);
406 [Test]
407 [ExpectedException (typeof (ArgumentException))]
408 public void Read_OverflowOffset ()
410 byte[] buffer = new byte [8];
411 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
412 cs.Read (buffer, Int32.MaxValue, 4);
415 [Test]
416 // MS BUG [ExpectedException (typeof (ObjectDisposedException))]
417 #if NET_2_0
418 [ExpectedException (typeof (IndexOutOfRangeException))]
419 #else
420 [Ignore ("Test cause System.ExecutionEngineException on MS runtime")]
421 #endif
422 public void Write_Disposed ()
424 byte[] buffer = new byte [8];
425 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
426 cs.Clear ();
427 cs.Write (buffer, 0, 8);
430 [Test]
431 [ExpectedException (typeof (NotSupportedException))]
432 public void Write_ReadStream ()
434 cs = new CryptoStream (readStream, encryptor, CryptoStreamMode.Read);
435 byte[] buffer = new byte [8];
436 cs.Write (buffer, 0, 8);
439 [Test]
440 // [ExpectedException (typeof (ArgumentNullException))]
441 [ExpectedException (typeof (NullReferenceException))]
442 public void Write_NullBuffer ()
444 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
445 cs.Write (null, 0, 8);
448 [Test]
449 public void Write_EmptyBuffer_ZeroCount ()
451 byte[] buffer = new byte [0];
452 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
453 cs.Write (buffer, 0, 0);
456 [Test]
457 [ExpectedException (typeof (ArgumentOutOfRangeException))]
458 public void Write_NegativeOffset ()
460 byte[] buffer = new byte [8];
461 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
462 cs.Write (buffer, -1, 8);
465 [Test]
466 [ExpectedException (typeof (ArgumentException))]
467 public void Write_OverflowOffset ()
469 byte[] buffer = new byte [8];
470 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
471 cs.Write (buffer, Int32.MaxValue, 8);
474 [Test]
475 public void Write_ZeroCount ()
477 byte[] buffer = new byte [8];
478 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
479 cs.Write (buffer, 0, 0);
482 [Test]
483 [ExpectedException (typeof (ArgumentOutOfRangeException))]
484 public void Write_NegativeCount ()
486 byte[] buffer = new byte [8];
487 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
488 cs.Write (buffer, 0, -1);
491 [Test]
492 [ExpectedException (typeof (ArgumentException))]
493 public void Write_InvalidOffset ()
495 byte[] buffer = new byte [8];
496 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
497 cs.Write (buffer, 5, 4);
500 [Test]
501 [ExpectedException (typeof (ArgumentException))]
502 public void Write_OverflowCount ()
504 byte[] buffer = new byte [8];
505 cs = new CryptoStream (writeStream, encryptor, CryptoStreamMode.Write);
506 cs.Write (buffer, 0, Int32.MaxValue);
509 [Test]
510 public void FullRoundtripRead ()
512 byte[] encrypted;
513 using (DebugStream mem1 = new DebugStream ()) {
514 byte[] toEncrypt = Encoding.Unicode.GetBytes ("Please encode me!");
515 using (CryptoStream crypt = new CryptoStream (mem1, aes.CreateEncryptor (), CryptoStreamMode.Write)) {
516 crypt.Write (toEncrypt, 0, toEncrypt.Length);
517 crypt.FlushFinalBlock ();
519 encrypted = mem1.ToArray ();
522 using (DebugStream mem2 = new DebugStream (encrypted)) {
523 byte[] buffer = new byte [1024];
524 CryptoStream cr = new CryptoStream (mem2, aes.CreateDecryptor (), CryptoStreamMode.Read);
525 int len = cr.Read (buffer, 0, buffer.Length);
526 cr.Close ();
527 AssertEquals ("Full Length Read", 34, len);
528 AssertEquals ("Full Block Read", "Please encode me!", Encoding.Unicode.GetString (buffer, 0, len));
532 // bugzilla 46143 (adapted from test case by Joerg Rosenkranz)
533 [Test]
534 public void PartialRoundtripRead ()
536 byte[] encrypted;
537 using (DebugStream mem1 = new DebugStream ()) {
538 byte[] toEncrypt = Encoding.Unicode.GetBytes ("Please encode me!");
539 using (CryptoStream crypt = new CryptoStream (mem1, aes.CreateEncryptor (), CryptoStreamMode.Write)) {
540 crypt.Write (toEncrypt, 0, toEncrypt.Length);
541 crypt.FlushFinalBlock ();
543 encrypted = mem1.ToArray ();
546 using (DebugStream mem2 = new DebugStream (encrypted)) {
547 byte[] buffer = new byte [1024];
548 CryptoStream cr = new CryptoStream (mem2, aes.CreateDecryptor (), CryptoStreamMode.Read);
549 int len = cr.Read (buffer, 0, 20);
550 cr.Clear ();
551 cr.Close ();
552 AssertEquals ("Partial Length Read", 20, len);
553 AssertEquals ("Partial Block Read", "Please enc", Encoding.Unicode.GetString (buffer, 0, len));
557 // bugzilla: 40689 (adapted from test case by Henning Westerholt)
558 [Test]
559 public void WriteOnBlockWithFinal ()
561 byte[] desKey = {0, 1, 2, 3, 4, 5, 6, 7};
562 byte[] desIV = {0, 1, 2, 3, 4, 5, 6, 7};
563 DES des = DES.Create ();
565 MemoryStream msin = new MemoryStream ();
566 CryptoStream enc = new CryptoStream (msin, des.CreateEncryptor (desKey, desIV), CryptoStreamMode.Write);
567 byte[] data = new byte [2200];
568 enc.Write (data, 0, 2200);
569 enc.FlushFinalBlock ();
570 msin.Position = 0;
571 AssertEquals ("Encryped Write Length", 2208, msin.Length); // 2200 + padding
573 MemoryStream msout = new MemoryStream ();
574 msout.SetLength (0);
576 byte[] tmp = new byte [1024];
577 long readlen = 0;
578 long totallen = msin.Length;
580 CryptoStream dec = new CryptoStream (msout, des.CreateDecryptor (desKey, desIV), CryptoStreamMode.Write);
581 int len = msin.Read (tmp, 0, 1024);
582 while (len > 0) {
583 dec.Write (tmp, 0, len);
584 readlen += len;
585 len = msin.Read (tmp, 0, 1024);
587 AssertEquals ("Decryped Write Length", 2200, msout.Length);
589 dec.Close ();
590 dec.Clear ();
591 msout.Close ();
592 msin.Close ();
594 AssertEquals ("Read Length", 2208, readlen); // 2200 + padding
597 [Test]
598 public void PreGeneratedStreams ()
600 byte[] desKey = {0, 1, 2, 3, 4, 5, 6, 7};
601 byte[] desIV = {0, 1, 2, 3, 4, 5, 6, 7};
602 DES des = DES.Create ();
604 for (int i=0; i < 9; i++) {
605 MemoryStream msin = new MemoryStream ();
606 CryptoStream enc = new CryptoStream (msin, des.CreateEncryptor (desKey, desIV), CryptoStreamMode.Write);
607 byte[] data = new byte [i];
608 enc.Write (data, 0, i);
609 enc.FlushFinalBlock ();
611 string msg = "PreGeneratedStream #" + i;
612 string result = BitConverter.ToString (msin.ToArray ());
613 switch (i) {
614 case 0:
615 AssertEquals (msg, "92-C9-DB-45-30-0B-93-2F", result);
616 break;
617 case 1:
618 AssertEquals (msg, "08-CF-A1-37-BD-56-D0-65", result);
619 break;
620 case 2:
621 AssertEquals (msg, "58-87-D4-9B-2C-27-97-0C", result);
622 break;
623 case 3:
624 AssertEquals (msg, "07-35-90-94-68-7D-51-FB", result);
625 break;
626 case 4:
627 AssertEquals (msg, "BF-00-98-C5-20-71-D0-DB", result);
628 break;
629 case 5:
630 AssertEquals (msg, "1A-55-C8-6E-C1-9B-31-82", result);
631 break;
632 case 6:
633 AssertEquals (msg, "2D-2B-76-41-61-0E-00-0C", result);
634 break;
635 case 7:
636 AssertEquals (msg, "DC-FF-73-D2-7F-D7-48-5D", result);
637 break;
638 case 8:
639 AssertEquals (msg, "E1-B2-46-E5-A7-C7-4C-BC-0E-40-4A-FC-08-92-B1-EB", result);
640 break;
645 private byte[] EmptyStream (PaddingMode mode)
647 SymmetricAlgorithm algo = Rijndael.Create ();
648 algo.Key = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
649 algo.IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
650 algo.Padding = mode;
651 MemoryStream ms = new MemoryStream ();
652 CryptoStream cs = new CryptoStream (ms, algo.CreateEncryptor(), CryptoStreamMode.Write);
653 cs.Write (ms.GetBuffer (), 0, (int) ms.Length);
654 cs.FlushFinalBlock ();
655 cs.Flush ();
656 return ms.ToArray ();
659 [Test]
660 public void EmptyStreamWithPaddingNone ()
662 byte[] result = EmptyStream (PaddingMode.None);
663 AssertEquals ("Result Length", 0, result.Length);
666 [Test]
667 public void EmptyStreamWithPaddingPKCS7 ()
669 byte[] expected = { 0x07, 0xFE, 0xEF, 0x74, 0xE1, 0xD5, 0x03, 0x6E, 0x90, 0x0E, 0xEE, 0x11, 0x8E, 0x94, 0x92, 0x93 };
670 byte[] result = EmptyStream (PaddingMode.PKCS7);
671 AssertEquals ("Result Length", 16, result.Length);
672 AssertEquals ("Result", expected, result);
675 [Test]
676 public void EmptyStreamWithPaddingZeros ()
678 byte[] result = EmptyStream (PaddingMode.Zeros);
679 AssertEquals ("Result Length", 0, result.Length);
682 // bugzilla: 49323 (adapted from test case by Carlos Guzmán Álvarez)
683 [Test]
684 public void MultiblocksWithPartial ()
686 SymmetricAlgorithm tdes = new TripleDESCryptoServiceProvider ();
687 tdes.Key = new byte[] {161, 54, 179, 213, 89, 75, 130, 4, 186, 99, 158, 127, 19, 195, 175, 143, 79, 109, 25, 202, 237, 235, 62, 170};
688 tdes.IV = new byte[] {193, 227, 54, 132, 68, 172, 55, 91};
690 byte[] fragment = new byte[] {20, 0, 0, 12, 181, 134, 8, 230, 185, 75, 19, 129, 101, 142, 118, 190};
691 byte[] mac = new byte[] {42, 148, 229, 58, 185, 249, 154, 131, 157, 79, 176, 168, 143, 71, 0, 118, 5, 10, 95, 8};
693 // Encryption ( fragment + mac [+ padding + padding_length] )
694 MemoryStream ms = new MemoryStream ();
695 CryptoStream cs = new CryptoStream (ms, tdes.CreateEncryptor (), CryptoStreamMode.Write);
696 cs.Write (fragment, 0, fragment.Length);
697 cs.Write (mac, 0, mac.Length);
698 // Calculate padding_length
699 int fragmentLength = fragment.Length + mac.Length + 1;
700 int padding = (((fragmentLength / 8) * 8) + 8) - fragmentLength;
701 // Write padding length byte
702 cs.WriteByte ((byte)padding);
703 cs.Close ();
704 byte[] encrypted = ms.ToArray ();
705 byte[] expected = new byte[] { 0x9c, 0x99, 0x56, 0x8e, 0x75, 0x3e, 0x02, 0x95, 0x5b, 0x5c, 0x46, 0x8b, 0xcf, 0xf8, 0x27, 0x21, 0x53, 0x5f, 0x3d, 0xd8, 0x16, 0x95, 0x82, 0x3d, 0x88, 0x9b, 0x9a, 0x47, 0xda, 0x97, 0x90, 0x86, 0x50, 0x0e, 0x48, 0xee, 0xe7, 0x9b, 0x25, 0x41 };
706 AssertEquals ("MultiblocksWithPartial", expected, encrypted);
709 // Adapted from Subba Rao Thirumoorthy email on mono-devel-list (december 2003)
710 private byte[] NonMultipleOfBlockSize_Encrypt (ICryptoTransform ct, byte[] data)
712 DebugStream stream = new DebugStream ();
713 CryptoStream CryptStream = new CryptoStream (stream, ct, CryptoStreamMode.Write);
715 int len = 0;
716 long myLength = 0;
717 byte[] Buffer = new byte [1024];
719 DebugStream fout = new DebugStream (data);
720 while (myLength < data.Length) {
721 len = fout.Read (Buffer, 0, 1023);
722 if (len == 0)
723 break;
724 CryptStream.Write (Buffer, 0, len);
725 CryptStream.Flush ();
726 myLength = myLength + len;
728 CryptStream.FlushFinalBlock ();
729 // we must ensure that the result is correct
730 AssertEquals ("Length(final)", 64, len);
731 byte[] result = stream.ToArray ();
732 string end = BitConverter.ToString (result, 65520, 16);
733 AssertEquals ("End part", "04-70-19-1D-28-C5-BD-9A-23-C6-60-E2-28-96-38-65", end);
735 CryptStream.Close();
736 stream.Close();
737 return result;
740 private byte[] NonMultipleOfBlockSize_Decrypt (ICryptoTransform ct, byte[] data)
742 DebugStream stream = new DebugStream (data);
743 CryptoStream CryptStream = new CryptoStream (stream, ct, CryptoStreamMode.Read);
745 int len = 0;
746 long myLength = 0;
747 byte[] Buffer = new Byte [1024];
749 DebugStream fout = new DebugStream ();
750 // each returned block must be 1023 bytes long
751 // even if this isn't a multiple of the block size
752 while ((len = CryptStream.Read (Buffer, 0, 1023)) != 0) {
753 fout.Write (Buffer, 0, len);
754 fout.Flush ();
755 myLength = myLength + len;
758 byte[] result = fout.ToArray ();
759 CryptStream.Close ();
760 stream.Close ();
761 return result;
764 [Test]
765 public void NonMultipleOfBlockSize ()
767 byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
768 byte[] iv = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
769 byte[] data = new byte [65536];
771 RijndaelManaged aes = new RijndaelManaged ();
772 ICryptoTransform encryptor = aes.CreateEncryptor (key, iv);
773 byte[] encdata = NonMultipleOfBlockSize_Encrypt (encryptor, data);
774 AssertEquals ("Encrypted Data Length", (data.Length + (aes.BlockSize >> 3)), encdata.Length);
776 ICryptoTransform decryptor = aes.CreateDecryptor (key, iv);
777 byte[] decdata = NonMultipleOfBlockSize_Decrypt (decryptor, encdata);
778 AssertEquals ("Decrypted Data Length", data.Length, decdata.Length);
780 int i = 0;
781 bool b = true;
782 while (b && (i < data.Length)) {
783 b = (data [i] == decdata [i]);
784 i++;
786 Assert ("NonMultipleOfBlockSize", b);
789 // bugzilla: 51322 - indirectly related but it explains why my first (unapplied) patch didn't work
790 [Test]
791 public void DecryptPartial_TransformFinalBlock_required ()
793 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
794 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
795 DES des = DES.Create ();
797 byte[] data = Encoding.Unicode.GetBytes ("ximian"); // 12 bytes, 1.5 DES block size
798 DebugStream encrypted = new DebugStream ();
799 cs = new CryptoStream (encrypted, des.CreateEncryptor (key, iv), CryptoStreamMode.Write);
800 cs.Write (data, 0, data.Length);
801 cs.Close ();
803 data = encrypted.ToArray ();
804 DebugStream decrypted = new DebugStream (data);
805 cs = new CryptoStream (decrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read);
806 int len = cs.Read (data, 0, data.Length);
807 cs.Close ();
808 AssertEquals ("Length", 12, len);
809 AssertEquals ("Unicode DES Roundtrip", "ximian", Encoding.Unicode.GetString (data, 0, len));
812 [Test]
813 public void DecryptPartial_TransformFinalBlock_2Pass ()
815 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
816 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
817 DES des = DES.Create ();
819 byte[] data = Encoding.Unicode.GetBytes ("ximian"); // 12 bytes, 1.5 DES block size
820 DebugStream encrypted = new DebugStream ();
821 cs = new CryptoStream (encrypted, des.CreateEncryptor (key, iv), CryptoStreamMode.Write);
822 cs.Write (data, 0, data.Length);
823 cs.Close ();
825 data = encrypted.ToArray ();
826 DebugStream decrypted = new DebugStream (data);
827 cs = new CryptoStream (decrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read);
828 int len = cs.Read (data, 0, 6);
829 AssertEquals ("Length (1st pass)", 6, len);
830 AssertEquals ("Partial DES Roundtrip", "xim", Encoding.Unicode.GetString (data, 0, len));
831 len += cs.Read (data, 6, 8);
832 AssertEquals ("Length (1st+2nd)", 12, len);
833 AssertEquals ("Full DES Roundtrip", "ximian", Encoding.Unicode.GetString (data, 0, len));
834 cs.Close ();
837 // based on http://www.c-sharpcorner.com/Code/2002/May/FileEncryption.asp
838 [Test]
839 public void WriteByteReadByte ()
841 DebugStream original = new DebugStream (Encoding.Unicode.GetBytes ("ximian"));
843 DebugStream encrypted = new DebugStream ();
844 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
845 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
846 DES des = DES.Create ();
847 cs = new CryptoStream (encrypted, des.CreateEncryptor (key, iv), CryptoStreamMode.Write);
849 int data;
850 while ((data = original.ReadByte ()) != -1)
851 cs.WriteByte((byte) data);
852 cs.Close ();
854 byte[] result = encrypted.ToArray ();
855 AssertEquals ("Encrypted", "18-EA-93-3F-20-86-D2-AA-78-02-D7-6F-E4-47-17-9C", BitConverter.ToString (result));
857 encrypted = new DebugStream (result);
858 DebugStream decrypted = new DebugStream ();
859 cs = new CryptoStream (encrypted, des.CreateDecryptor (key, iv), CryptoStreamMode.Read);
861 while ((data = cs.ReadByte ()) != -1)
862 decrypted.WriteByte((byte) data);
863 cs.Close ();
864 decrypted.Close ();
866 AssertEquals ("W/R Byte Roundtrip", "ximian", Encoding.Unicode.GetString (decrypted.ToArray ()));
869 // based http://www.4guysfromrolla.com/webtech/090501-1.shtml
871 public string EncryptData (ICryptoTransform des, string strData)
873 strData = String.Format("{0,5:00000}" + strData, strData.Length);
874 byte[] data = Encoding.ASCII.GetBytes (strData);
876 MemoryStream mStream = new MemoryStream (data);
877 CryptoStream cs = new CryptoStream (mStream, des, CryptoStreamMode.Read);
878 MemoryStream mOut = new MemoryStream ();
880 int bytesRead;
881 byte[] output = new byte [1024];
882 do {
883 bytesRead = cs.Read (output, 0, 1024);
884 if (bytesRead != 0)
885 mOut.Write (output, 0, bytesRead);
887 while (bytesRead > 0);
889 return Convert.ToBase64String (mOut.ToArray ());
892 public string DecryptData (ICryptoTransform des, string strData)
894 MemoryStream mOut = new MemoryStream ();
895 byte[] data = Convert.FromBase64String (strData);
896 CryptoStream cs = new CryptoStream (mOut, des, CryptoStreamMode.Write);
897 cs.Write (data, 0, (int)data.Length);
898 cs.FlushFinalBlock ();
899 return Encoding.ASCII.GetString (mOut.ToArray ()).Substring (5);
902 [Test]
903 public void EncryptOnRead ()
905 SHA1 sha = SHA1.Create ();
906 byte[] vector = sha.ComputeHash (Encoding.ASCII.GetBytes ("s3kr3t"));
907 byte[] key = new byte [8];
908 Buffer.BlockCopy (vector, 0, key, 0, key.Length);
909 byte[] iv = new byte [8];
910 Buffer.BlockCopy (vector, 8, iv, 0, iv.Length);
912 DES des = DES.Create ();
914 StringBuilder sb = new StringBuilder ();
915 sb.Append ("a");
916 string data = sb.ToString ();
917 string encdata = EncryptData (des.CreateEncryptor (key, iv), data);
918 string decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
919 AssertEquals ("Encrypt-" + data, "9YVfvrh5yj0=", encdata);
920 AssertEquals ("Decrypt-" + data, data, decdata);
922 sb.Append ("a");
923 data = sb.ToString ();
924 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
925 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
926 AssertEquals ("Encrypt-" + data, "qNe4d0UlkU8=", encdata);
927 AssertEquals ("Decrypt-" + data, data, decdata);
929 sb.Append ("a");
930 data = sb.ToString ();
931 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
932 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
933 AssertEquals ("Encrypt-" + data, "OcernYAQ1NAME/Gny+ZuaA==", encdata);
934 AssertEquals ("Decrypt-" + data, data, decdata);
936 sb.Append ("a");
937 data = sb.ToString ();
938 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
939 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
940 AssertEquals ("Encrypt-" + data, "H5UveR2lds1T+IWN4pks2Q==", encdata);
941 AssertEquals ("Decrypt-" + data, data, decdata);
943 sb.Append ("a");
944 data = sb.ToString ();
945 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
946 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
947 AssertEquals ("Encrypt-" + data, "dDQ3HAVtTbiRwwUqWANaeA==", encdata);
948 AssertEquals ("Decrypt-" + data, data, decdata);
950 sb.Append ("a");
951 data = sb.ToString ();
952 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
953 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
954 AssertEquals ("Encrypt-" + data, "At1r7dVDjJlQidf4QzCNkw==", encdata);
955 AssertEquals ("Decrypt-" + data, data, decdata);
957 sb.Append ("a");
958 data = sb.ToString ();
959 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
960 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
961 AssertEquals ("Encrypt-" + data, "DFDJWJGaNrFVBDXovsq1ew==", encdata);
962 AssertEquals ("Decrypt-" + data, data, decdata);
964 sb.Append ("a");
965 data = sb.ToString ();
966 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
967 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
968 AssertEquals ("Encrypt-" + data, "gM040QGMPOBj3u1lEK4XHQ==", encdata);
969 AssertEquals ("Decrypt-" + data, data, decdata);
971 sb.Append ("a");
972 data = sb.ToString ();
973 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
974 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
975 AssertEquals ("Encrypt-" + data, "P5hRUhrxOWFX0ER/IjJL/Q==", encdata);
976 AssertEquals ("Decrypt-" + data, data, decdata);
978 sb.Append ("a");
979 data = sb.ToString ();
980 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
981 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
982 AssertEquals ("Encrypt-" + data, "uDIaQ1uXtWUIboGFLt306Q==", encdata);
983 AssertEquals ("Decrypt-" + data, data, decdata);
985 sb.Append ("a");
986 data = sb.ToString ();
987 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
988 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
989 AssertEquals ("Encrypt-" + data, "giJKTXfad5Z8hebhXtYZ4hmKX/EC8w6x", encdata);
990 AssertEquals ("Decrypt-" + data, data, decdata);
992 sb.Append ("a");
993 data = sb.ToString ();
994 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
995 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
996 AssertEquals ("Encrypt-" + data, "lBehBplIrjjrlIrMjYcNz1DOoXLHjZdn", encdata);
997 AssertEquals ("Decrypt-" + data, data, decdata);
999 sb.Append ("a");
1000 data = sb.ToString ();
1001 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1002 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1003 AssertEquals ("Encrypt-" + data, "2elWrUnjmsAOpo2s4voJyZXEJ/rtKB7P", encdata);
1004 AssertEquals ("Decrypt-" + data, data, decdata);
1006 sb.Append ("a");
1007 data = sb.ToString ();
1008 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1009 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1010 AssertEquals ("Encrypt-" + data, "GB3BaIZGf9K+T82j7T8Fri2rQ2/YUdSe", encdata);
1011 AssertEquals ("Decrypt-" + data, data, decdata);
1013 sb.Append ("a");
1014 data = sb.ToString ();
1015 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1016 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1017 AssertEquals ("Encrypt-" + data, "Gc+wkJL+CVjdJchgcIoi8dkH2BVpHJgB", encdata);
1018 AssertEquals ("Decrypt-" + data, data, decdata);
1020 sb.Append ("a");
1021 data = sb.ToString ();
1022 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1023 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1024 AssertEquals ("Encrypt-" + data, "loeuyII/PvWb91M4pFVkyaPxQoQVYpNb", encdata);
1025 AssertEquals ("Decrypt-" + data, data, decdata);
1027 sb.Append ("a");
1028 data = sb.ToString ();
1029 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1030 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1031 AssertEquals ("Encrypt-" + data, "PHXmi/sxNIgApXAfdm+Bf54/nCM//N8o", encdata);
1032 AssertEquals ("Decrypt-" + data, data, decdata);
1034 sb.Append ("a");
1035 data = sb.ToString ();
1036 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1037 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1038 AssertEquals ("Encrypt-" + data, "xpb+wj/8LmH2ScTg3OU4JOsE5Owj6flF", encdata);
1039 AssertEquals ("Decrypt-" + data, data, decdata);
1041 sb.Append ("a");
1042 data = sb.ToString ();
1043 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1044 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1045 AssertEquals ("Encrypt-" + data, "WJz4VfsZ2emzhYWoSf+PNBDpHooxEregqMWnzm4gcqU=", encdata);
1046 AssertEquals ("Decrypt-" + data, data, decdata);
1048 sb.Append ("a");
1049 data = sb.ToString ();
1050 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1051 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1052 AssertEquals ("Encrypt-" + data, "PaouZu1iOKbCMRJSu04y/kB+TcOk4yp8K2BOGDs1PPE=", encdata);
1053 AssertEquals ("Decrypt-" + data, data, decdata);
1055 sb.Append ("a");
1056 data = sb.ToString ();
1057 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1058 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1059 AssertEquals ("Encrypt-" + data, "qbTDs4dFy7eERdn5vV7JRPk2/m9smtwvZjA6+TmGlkI=", encdata);
1060 AssertEquals ("Decrypt-" + data, data, decdata);
1062 sb.Append ("a");
1063 data = sb.ToString ();
1064 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1065 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1066 AssertEquals ("Encrypt-" + data, "f2FsphcpM7Fu90S5V17ptly44lL4GvFCCaFdnnU4twk=", encdata);
1067 AssertEquals ("Decrypt-" + data, data, decdata);
1069 sb.Append ("a");
1070 data = sb.ToString ();
1071 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1072 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1073 AssertEquals ("Encrypt-" + data, "imD+ntHsUmp9ALJedzC1JmAJY0r2O4KkP8271+XuG4g=", encdata);
1074 AssertEquals ("Decrypt-" + data, data, decdata);
1076 sb.Append ("a");
1077 data = sb.ToString ();
1078 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1079 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1080 AssertEquals ("Encrypt-" + data, "80QLLUmHwx1fcEYGeFz1WXlS13kUy994sQLI6GhcjuM=", encdata);
1081 AssertEquals ("Decrypt-" + data, data, decdata);
1083 sb.Append ("a");
1084 data = sb.ToString ();
1085 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1086 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1087 AssertEquals ("Encrypt-" + data, "DtIIlj8BCOppmIgQ9AEdUj7pBB49S/9Q38kbWLjwiVs=", encdata);
1088 AssertEquals ("Decrypt-" + data, data, decdata);
1090 sb.Append ("a");
1091 data = sb.ToString ();
1092 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1093 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1094 AssertEquals ("Encrypt-" + data, "LNkprYaaUFtyan204OzX+a2pzOb/Pg5WXzXJ6WWB1rQ=", encdata);
1095 AssertEquals ("Decrypt-" + data, data, decdata);
1097 sb.Append ("a");
1098 data = sb.ToString ();
1099 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1100 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1101 AssertEquals ("Encrypt-" + data, "FRgx9m2lT2PxtYSIdRwc+SznJetNiRk1MEIZDl3D13pvo2yOtJ1MSQ==", encdata);
1102 AssertEquals ("Decrypt-" + data, data, decdata);
1104 sb.Append ("a");
1105 data = sb.ToString ();
1106 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1107 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1108 AssertEquals ("Encrypt-" + data, "V7JlnpJscrdIpX4z5S+/Q5WDjKzK4aB5TiqI3JZOYJ+KE1CWQNNeow==", encdata);
1109 AssertEquals ("Decrypt-" + data, data, decdata);
1111 sb.Append ("a");
1112 data = sb.ToString ();
1113 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1114 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1115 AssertEquals ("Encrypt-" + data, "wVwPv1c2KQynbwiOBCAhmQlReOQT52qFR34AX4dtjEeQ1oCQ1N1tHg==", encdata);
1116 AssertEquals ("Decrypt-" + data, data, decdata);
1118 sb.Append ("a");
1119 data = sb.ToString ();
1120 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1121 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1122 AssertEquals ("Encrypt-" + data, "Zi+G0yfmuFjSjP455pjVeKBDDWB4qvTb0K0h20UtflrYG6wcWqUzDw==", encdata);
1123 AssertEquals ("Decrypt-" + data, data, decdata);
1125 sb.Append ("a");
1126 data = sb.ToString ();
1127 encdata = EncryptData (des.CreateEncryptor (key, iv), data);
1128 decdata = DecryptData (des.CreateDecryptor (key, iv), encdata);
1129 AssertEquals ("Encrypt-" + data, "0hGoonZ8jrLhMNDKBuWrlvFnq15ZLvnyq+Ilq8r4aYUEDxttQMwi5w==", encdata);
1130 AssertEquals ("Decrypt-" + data, data, decdata);
1133 // based on System.Security assembly XmlDsigBase64TransformTest
1135 [Test]
1136 public void FromBase64_Write ()
1138 string expected = "http://www.go-mono.com/";
1139 byte[] data = Encoding.UTF8.GetBytes (expected);
1140 string temp = Convert.ToBase64String (data, 0, data.Length);
1141 data = Encoding.UTF8.GetBytes (temp);
1143 DebugStream debug = new DebugStream ();
1144 ICryptoTransform base64 = new FromBase64Transform ();
1145 cs = new CryptoStream (debug, base64, CryptoStreamMode.Write);
1146 cs.Write (data, 0, data.Length);
1147 cs.FlushFinalBlock ();
1148 byte[] encoded = debug.ToArray ();
1150 string result = Encoding.UTF8.GetString (encoded);
1151 AssertEquals ("FromBase64_Write", expected, result);
1154 [Test]
1155 public void FromBase64_Read ()
1157 byte[] original = Encoding.UTF8.GetBytes ("aHR0cDovL3d3dy5nby1tb25vLmNvbS8=");
1158 DebugStream debug = new DebugStream (original);
1160 ICryptoTransform base64 = new FromBase64Transform ();
1161 cs = new CryptoStream (debug, base64, CryptoStreamMode.Read);
1163 byte[] data = new byte [1024];
1164 int length = cs.Read (data, 0, data.Length);
1165 cs.Close ();
1167 string result = Encoding.UTF8.GetString (data, 0, length);
1168 AssertEquals ("ToBase64_Read", "http://www.go-mono.com/", result);
1171 [Test]
1172 public void ToBase64_Write ()
1174 byte[] data = Encoding.UTF8.GetBytes ("http://www.go-mono.com/");
1176 DebugStream debug = new DebugStream ();
1177 ICryptoTransform base64 = new ToBase64Transform ();
1178 cs = new CryptoStream (debug, base64, CryptoStreamMode.Write);
1179 cs.Write (data, 0, data.Length);
1180 cs.FlushFinalBlock ();
1181 byte[] encoded = debug.ToArray ();
1183 string result = Encoding.UTF8.GetString (encoded);
1184 AssertEquals ("ToBase64_Write", "aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result);
1187 [Test]
1188 public void ToBase64_Read ()
1190 byte[] original = Encoding.UTF8.GetBytes ("http://www.go-mono.com/");
1191 DebugStream debug = new DebugStream (original);
1193 ICryptoTransform base64 = new ToBase64Transform ();
1194 cs = new CryptoStream (debug, base64, CryptoStreamMode.Read);
1196 byte[] data = new byte [1024];
1197 int length = cs.Read (data, 0, data.Length);
1198 cs.Close ();
1200 string result = Encoding.UTF8.GetString (data, 0, length);
1201 AssertEquals ("ToBase64_Read", "aHR0cDovL3d3dy5nby1tb25vLmNvbS8=", result);
1204 // Cascaded CryptoStream - like sample in book .NET Framework Security, chapter 30
1206 [Test]
1207 public void CascadedCryptoStream_Write ()
1209 DebugStream debug = new DebugStream ();
1211 // calculate both the hash (before encryption) and encrypt in one Write operation
1212 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
1213 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
1214 DES des = DES.Create ();
1215 CryptoStream cse = new CryptoStream (debug, des.CreateEncryptor (key, iv), CryptoStreamMode.Write);
1217 MD5 hash = MD5.Create ();
1218 CryptoStream csh = new CryptoStream (cse, hash, CryptoStreamMode.Write);
1220 byte[] data = Encoding.UTF8.GetBytes ("http://www.go-mono.com/");
1221 csh.Write (data, 0, data.Length);
1222 csh.FlushFinalBlock ();
1224 byte[] result = debug.ToArray ();
1225 AssertEquals ("Encrypted", "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));
1226 byte[] digest = hash.Hash;
1227 AssertEquals ("Hash", "71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest));
1230 [Test]
1231 public void CascadedCryptoStream_Read ()
1233 byte[] encdata = new byte[] { 0x8C, 0x24, 0x76, 0x74, 0x09, 0x79, 0x2B, 0xD3, 0x47, 0xC3, 0x32, 0xF5, 0xF3, 0x1A, 0x5E, 0x57, 0x04, 0x33, 0x2E, 0xB8, 0x50, 0x77, 0xB2, 0xA1 };
1234 DebugStream debug = new DebugStream (encdata);
1236 // decrypt data and validate its hash in one Read operation
1237 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
1238 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
1239 DES des = DES.Create ();
1240 CryptoStream csd = new CryptoStream (debug, des.CreateDecryptor (key, iv), CryptoStreamMode.Read);
1242 MD5 hash = MD5.Create ();
1243 CryptoStream csh = new CryptoStream (csd, hash, CryptoStreamMode.Read);
1245 byte[] data = new byte [1024];
1246 int length = csh.Read (data, 0, data.Length);
1247 csh.Close ();
1249 string result = Encoding.UTF8.GetString (data, 0, length);
1250 AssertEquals ("Decrypted", "http://www.go-mono.com/", result);
1251 byte[] digest = hash.Hash;
1252 AssertEquals ("Hash Validation", "71-04-12-D1-95-01-CF-F9-8D-8F-F8-0D-F9-AA-11-7D", BitConverter.ToString (digest));
1255 // bugzilla: 60573 - the number of block is not reduced for encryptors
1257 [Test]
1258 public void EncryptorWriteBlocks ()
1260 DebugStream debug = new DebugStream ();
1262 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
1263 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
1264 DES des = DES.Create ();
1265 CryptoStream cse = new CryptoStream (debug, des.CreateEncryptor (key, iv), CryptoStreamMode.Write);
1267 byte[] data = new byte [64];
1268 cse.Write (data, 0, 64);
1269 AssertEquals ("Length", 64, debug.Length);
1270 cse.Close ();
1273 [Test]
1274 public void DecryptorWriteBlocks ()
1276 DebugStream debug = new DebugStream ();
1278 byte[] key = {0, 1, 2, 3, 4, 5, 6, 7};
1279 byte[] iv = {0, 1, 2, 3, 4, 5, 6, 7};
1280 DES des = DES.Create ();
1281 CryptoStream csd = new CryptoStream (debug, des.CreateDecryptor (key, iv), CryptoStreamMode.Write);
1283 byte[] data = new byte [64] { 0xE1, 0xB2, 0x46, 0xE5, 0xA7, 0xC7, 0x4C, 0xBC, 0xD5, 0xF0, 0x8E, 0x25, 0x3B, 0xFA, 0x23, 0x80, 0x03, 0x16, 0x18, 0x17, 0xA3, 0x59, 0xBA, 0xAC, 0xFC, 0x47, 0x57, 0x2A, 0xF9, 0x44, 0x07, 0x84, 0x20, 0x74, 0x06, 0x38, 0xC2, 0xF3, 0xA1, 0xCE, 0x8C, 0x73, 0xB1, 0xE3, 0x75, 0x03, 0x66, 0x89, 0xF0, 0x4E, 0x98, 0x68, 0xB1, 0xBD, 0x85, 0x25, 0xFF, 0x4B, 0x11, 0x74, 0xEF, 0x14, 0xC8, 0xE9 };
1284 csd.Write (data, 0, 64);
1285 AssertEquals ("Length", 56, debug.Length);
1286 // last block is kept for later processing