2002-10-23 Sebastien Pouliot <spouliot@videotron.ca>
authorSebastien Pouliot <sebastien@ximian.com>
Thu, 24 Oct 2002 01:51:44 +0000 (01:51 -0000)
committerSebastien Pouliot <sebastien@ximian.com>
Thu, 24 Oct 2002 01:51:44 +0000 (01:51 -0000)
* SymmetricAlgorithm.cs: Fixed CFB mode (do encryption while decrypting!)
* TripleDESCryptoServiceProvider.cs: Ajusted for CFB.

svn path=/trunk/mcs/; revision=8503

mcs/class/corlib/System.Security.Cryptography/ChangeLog
mcs/class/corlib/System.Security.Cryptography/SymmetricAlgorithm.cs
mcs/class/corlib/System.Security.Cryptography/TripleDESCryptoServiceProvider.cs

index 8facf17439a0e09f597a59bb56c8316f64dca8dd..65b1bac03e01eb04b3ee4650695f5054875f372e 100644 (file)
@@ -1,4 +1,9 @@
-2002-10-20  Sebastien Pouliot  <spouliot@videotron.ca>
+2002-10-23  Sebastien Pouliot  <spouliot@videotron.ca>
+
+       * SymmetricAlgorithm.cs: Fixed CFB mode (do encryption while decrypting!)
+       * TripleDESCryptoServiceProvider.cs: Ajusted for CFB.
+
+2002-10-22  Sebastien Pouliot  <spouliot@videotron.ca>
 
        * RjindaelManaged.cs: Fixed decryption for 192 and 256 bit block size
 
index c73188f7c9f641d6cefe8893007140ff7245f134..c158c4ad334d3498354d36fdfed010332259c3a5 100755 (executable)
@@ -28,6 +28,7 @@ namespace System.Security.Cryptography {
                private byte[] temp;
                private byte[] temp2;
                private int FeedBackByte;
+               private int FeedBackIter;
                private bool m_disposed = false;
 
                public SymmetricTransform (SymmetricAlgorithm symmAlgo, bool encryption, byte[] rgbIV) 
@@ -39,6 +40,7 @@ namespace System.Security.Cryptography {
                        Array.Copy (rgbIV, 0, temp, 0, BlockSizeByte);
                        temp2 = new byte [BlockSizeByte];
                        FeedBackByte = (algo.FeedbackSize >> 3);
+                       FeedBackIter = (int) BlockSizeByte / FeedBackByte;
                }
 
                ~SymmetricTransform () 
@@ -133,18 +135,31 @@ namespace System.Security.Cryptography {
                // Cipher-FeedBack (CFB)
                protected virtual void CFB (byte[] input, byte[] output) 
                {
-                       ECB (temp, temp2);
-
                        if (encrypt) {
-                               for (int i = 0; i < BlockSizeByte; i++)
-                                       output[i] = (byte)(temp2[i] ^ input[i]);
-                               Array.Copy (temp, FeedBackByte, temp, 0, BlockSizeByte - FeedBackByte);
-                               Array.Copy (output, 0, temp, BlockSizeByte - FeedBackByte, FeedBackByte);
+                               for (int x = 0; x < FeedBackIter; x++) {
+                                       // temp is first initialized with the IV
+                                       ECB (temp, temp2);
+
+                                       for (int i = 0; i < FeedBackByte; i++)
+                                               output[i + x] = (byte)(temp2[i] ^ input[i + x]);
+                                       Array.Copy (temp, FeedBackByte, temp, 0, BlockSizeByte - FeedBackByte);
+                                       Array.Copy (output, x, temp, BlockSizeByte - FeedBackByte, FeedBackByte);
+                               }
                        }
                        else {
-                               Array.Copy (input, 0, temp, 0, BlockSizeByte);
-                               for (int i = 0; i < BlockSizeByte; i++)
-                                       output[i] = (byte)(temp2[i] ^ input[i]);
+                               for (int x = 0; x < FeedBackIter; x++) {
+                                       // we do not really decrypt this data!
+                                       encrypt = true;
+                                       // temp is first initialized with the IV
+                                       ECB (temp, temp2);
+                                       encrypt = false;
+
+                                       Array.Copy (temp, FeedBackByte, temp, 0, BlockSizeByte - FeedBackByte);
+                                       Array.Copy (input, x, temp, BlockSizeByte - FeedBackByte, FeedBackByte);
+                                       for (int i = 0; i < FeedBackByte; i++)
+                                               output[i + x] = (byte)(temp2[i] ^ input[i + x]);
+//                                             output[i + x] = (byte)(temp2[BlockSizeByte - x - i - 1] ^ input[i + x]);
+                               }
                        }
                }
 
index 6a01e47b9430df40eaecd71996dd1f231f06dd4f..48f458709dc8212ab2ae293f9372abb07737a189 100644 (file)
@@ -78,7 +78,8 @@ internal class TripleDESTransform : SymmetricTransform {
                else
                        Array.Copy (key, 16, key3, 0, 8);
 
-               if (encryption) {
+               // note: some modes (like CFB) requires encryption when decrypting
+               if ((encryption) || (algo.Mode == CipherMode.CFB)) {
                        E1 = new DESTransform (des, true, key1, iv);
                        D2 = new DESTransform (des, false, key2, iv);
                        E3 = new DESTransform (des, true, key3, iv);