|
security
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CryptographicException problem on Server 2003following code is meant to encrypt an image on a client, then decrypt the image on a server. When the client and server are both running on the same machine running Windows XP SP2 w/ .NET 1.1 the code below works. However, when the server code is put on a machine running Windows Server 2003, CryptoStream.Read() throws: System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid. below the code is a snippet of the end of the encrypted image. Any help would be greatly appreciated. private byte[] EncryptImage(Stream ImageStream, byte[] Key, byte[] IV) { try { byte[] EncryptedImage = new byte[ImageStream.Length]; MemoryStream encImageStream = new MemoryStream(); RijndaelManaged rijn = new RijndaelManaged(); ICryptoTransform trans = rijn.CreateEncryptor(Key, IV); CryptoStream crypt = new CryptoStream(encImageStream, trans, CryptoStreamMode.Write); ImageStream.Position = 0; byte[] rawImage = new Byte[ImageStream.Length]; ImageStream.Read(rawImage, 0, rawImage.Length); crypt.Write(rawImage, 0, rawImage.Length); crypt.FlushFinalBlock(); encImageStream.Position = 0; encImageStream.Read(EncryptedImage, 0, EncryptedImage.Length); crypt.Close(); ImageStream.Close(); encImageStream.Close(); return EncryptedImage; } catch(Exception ex) { MessageBox.Show(ex.ToString()); EventLog.WriteEntry("ScriptScanner", ex.Message + "\n" + ex.ToString(), EventLogEntryType.Error, 0, 0); return null; } } Then decrypt the image on server side: private byte[] DecryptImage(byte[] EncImage, byte[] Key, byte[] IV) { try { byte[] decImage = new Byte[EncImage.Length]; MemoryStream encImageStream = new MemoryStream(EncImage); RijndaelManaged rijn = new RijndaelManaged(); ICryptoTransform trans = rijn.CreateDecryptor(Key, IV); CryptoStream crypt = new CryptoStream(encImageStream, trans, CryptoStreamMode.Read); crypt.Read(decImage, 0, decImage.Length); crypt.Close(); encImageStream.Close(); return decImage; } catch(Exception ex) { EventLog.WriteEntry("ScriptService", ex.ToString(), EventLogEntryType.Error, EVENT_ERROR_DECRYPTION, EVENT_ERROR); return null; } } Encrypted Image ..... 173f0: aa e9 ee 47 a2 32 b0 5d ªéîG¢2°] 173f8: 8c a2 75 00 c7 2b 8c 19 Œ¢u.Ç+Œ. 17400: e5 b3 cb dc e0 a6 3f 0d å³ËÜà ¦?. 17408: 3e 58 >X Never mind, the problem was the length of the byte array I was decrypting. I
changed encImageStream.Read(EncryptedImage, 0, EncryptedImage.Length); to EncryptedImage = encImageStream.ToArray(); and then trimmed the extra bytes on the server side after decrypting. I'm still kinda stumped why the same code worked on Windows XP and not on Server 2003.
Error instantiating RSACryptoServiceProvider
Error from CAS Config util Custom IPrincipal and declarative security checking Newbie error on output Code Access Security - User Policy Level best practice for encrypting in app Secure files on virtual Howto secure a local file? Error registering ServicedComponent waitable timer callback and waking up action |
|||||||||||||||||||||||