[Mono-bugs] [Bug 81008][Nor] New - Exception thrown, Bad PKCS7 padding.

bugzilla-daemon at bugzilla.ximian.com bugzilla-daemon at bugzilla.ximian.com
Fri Mar 2 14:57:25 EST 2007


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by dtemes at infoco.es.

http://bugzilla.ximian.com/show_bug.cgi?id=81008

--- shadow/81008	2007-03-02 14:57:25.000000000 -0500
+++ shadow/81008.tmp.16515	2007-03-02 14:57:25.000000000 -0500
@@ -0,0 +1,136 @@
+Bug#: 81008
+Product: Mono: Class Libraries
+Version: 1.2
+OS: GNU/Linux [Other]
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: Unknown
+Priority: Normal
+Component: System.Security
+AssignedTo: sebastien at ximian.com                            
+ReportedBy: dtemes at infoco.es               
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Exception thrown, Bad PKCS7 padding.
+
+Please fill in this template when reporting a bug, unless you know what 
+you are doing.
+Description of Problem: Trying to decrypt a previously encrypted string 
+throws "Unhandled Exception: 
+System.Security.Cryptography.CryptographicException: Bad PKCS7 padding. 
+Invalid length 0."
+
+It runs as expected in windows and linux compiled with mcs
+
+Steps to reproduce the problem:
+1. compile the sample code provided with "gmcs":
+
+// start code
+sing System;
+using System.Text;
+using System.IO;
+using System.Security.Cryptography;
+
+namespace ConsoleApplication1
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+
+            string key = "12345678";
+            string plain = "hello world";
+
+            string encResult = DESEncrypt(key, plain);
+
+            string plainAgain = DESDecrypt(key, encResult);
+
+            System.Console.WriteLine(plain);
+            System.Console.WriteLine(encResult);
+            System.Console.WriteLine(plainAgain);
+
+
+        }
+
+
+
+        public static string DESEncrypt(string key, string data)
+        {
+            MemoryStream output = new MemoryStream();
+            byte[] byteData = new ASCIIEncoding().GetBytes(data);
+
+            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
+            des.Mode = CipherMode.ECB;
+            CryptoStream crypt = new CryptoStream(output, 
+des.CreateEncryptor(new ASCIIEncoding().GetBytes(key), null), 
+CryptoStreamMode.Write);
+            crypt.Write(byteData, 0, byteData.Length);
+
+            crypt.Close(); output.Close();
+            return Convert.ToBase64String(output.ToArray());
+        }
+
+
+        public static string DESDecrypt(string key, string data)
+        {
+            MemoryStream output = new MemoryStream();
+            byte[] byteData = Convert.FromBase64String(data);
+
+            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
+            des.Mode = CipherMode.ECB;
+            CryptoStream crypt = new CryptoStream(output, 
+des.CreateDecryptor(new ASCIIEncoding().GetBytes(key), null), 
+CryptoStreamMode.Write);
+
+            crypt.Write(byteData, 0, byteData.Length);
+
+            crypt.Close(); output.Close();
+            return new ASCIIEncoding().GetString(output.ToArray());
+        }
+
+
+    }
+}
+//end code
+
+2. run the code
+
+
+Actual Results:
+
+Unhandled Exception: System.Security.Cryptography.CryptographicException: 
+Bad PKCS7 padding. Invalid length 0.
+  at 
+Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException 
+(PaddingMode padding, Int32 length, Int32 position) [0x00000]
+  at Mono.Security.Cryptography.SymmetricTransform.FinalDecrypt 
+(System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000]
+  at Mono.Security.Cryptography.SymmetricTransform.TransformFinalBlock 
+(System.Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount) [0x00000]
+  at System.Security.Cryptography.CryptoStream.FlushFinalBlock () [0x00000]
+  at System.Security.Cryptography.CryptoStream.Close () [0x00000]
+  at ConsoleApplication1.Program.DESDecrypt (System.String key, 
+System.String data) [0x00000]
+  at ConsoleApplication1.Program.Main (System.String[] args) [0x00000]
+
+
+
+Expected Results:
+the plain code is output, then the base64 encrypted sting and then the 
+plain text again:
+
+hello world
+KNugLrX23UddguNoHIO7dw==
+hello world
+
+
+
+
+How often does this happen? 
+always
+
+Additional Information:
+compiles and runs under windows and also in linux compiling with "mcs"


More information about the mono-bugs mailing list