[Mono-dev] DES problem
Hi, I've got a service application which uses encrypted data over network. My decrypting function gives different results with the same inputs on Mono which does not occour on .NET. The other mighty thing is that when I grab this function out of the service and test it in itself this problem does not happen again. What is wrong here? My function looks as follows (test version): Friend Function DecryptDataDES(ByVal tbytaData As Byte(), ByVal tbytaDESKey As Byte(), ByVal tbytaDESIV As Byte(), ByRef tbytaDecryptedData As Byte()) As Boolean Dim lobjMemStream As MemoryStream Dim lobjCryptoStream As CryptoStream Dim lbytaDESKey As Byte() Dim lbytaDESIV As Byte() Try 'console.writeline() ReDim lbytaDESKey(tbytaDESKey.Length - 1) ReDim lbytaDESIV(tbytaDESIV.Length - 1) tbytaDESKey.CopyTo(lbytaDESKey, 0) tbytaDESIV.CopyTo(lbytaDESIV, 0) Console.WriteLine("tbytaData: " & Convert.ToBase64String(tbytaData)) Console.WriteLine("lbytaDESKey: " & Convert.ToBase64String(lbytaDESKey)) Console.WriteLine("lbytaDESIV: " & Convert.ToBase64String(lbytaDESIV)) 'teszt If Not InitTripleDESCSP(lbytaDESKey, lbytaDESIV) Then 'ak20050721: ha nem kényszerítjük ki az újrainicializálást, akkor linux/mono-n ugyanolyan bemenet mellett eltérő eredményt fog 'adni a DecryptDataDES függvény. Emiatt InitTripleDESCSP(lbytaDESKey, lbytaDESIV) helyett InitTripleDESCSP(lbytaDESKey, lbytaDESIV, True) 'szerepel a hívásban 'console.writeline("Forced reinit...") 'If Not InitTripleDESCSP(lbytaDESKey, lbytaDESIV, True) Then 'mobjApp.Errors.Add(mstrResName, CommErrEnum.CantDecryptDataDES, , , "InitTripleDESCSP").Raise() Console.WriteLine("InitTripleDESCSP failed") End If Console.WriteLine("lbytaDESKey: " & Convert.ToBase64String(lbytaDESKey)) Console.WriteLine("lbytaDESIV: " & Convert.ToBase64String(lbytaDESIV)) lobjMemStream = New MemoryStream() lobjCryptoStream = New CryptoStream(lobjMemStream, mobjDecryptTransform, CryptoStreamMode.Write) 'Írás közben dekódolja az adatot lobjCryptoStream.Write(tbytaData, 0, tbytaData.Length) lobjCryptoStream.FlushFinalBlock() lobjCryptoStream.Close()'e nélkül nem működik!!! 'Kiolvassuk a dekódolt adatot tbytaDecryptedData = lobjMemStream.ToArray() Console.WriteLine("tbytaDecryptedData: " & Convert.ToBase64String(tbytaDecryptedData)) Return True Catch ex As Exception Console.WriteLine("DecryptDataDES failed") Finally Try If Not lobjMemStream Is Nothing Then lobjMemStream.Close() If Not lobjCryptoStream Is Nothing Then lobjCryptoStream.Close() Catch End Try End Try End Function Thanks! Károly ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
[Mono-dev] DES problem
Hi, There is a little difference between .NET and Mono. When I call the routine below in .NET I get the results: True True With Mono it says: False True. Is it possible to set this CanReuseTransform property to true and reuse the transform once again or shall I reinitialize it every time? (I found that with Mono I can not decrypt two or more times with the same ICryptoTransform. I get different results with same inputs.) The code snippet: Private mobjEncryptTransform As ICryptoTransform Private mobjDecryptTransform As ICryptoTransform Private Function InitTripleDESCSP(ByVal tbytaDESKey As Byte(), ByVal tbytaDESIV As Byte(), Optional ByVal tlogForceInit As Boolean = False) As Boolean Dim lobjTripleDESCSP As TripleDESCryptoServiceProvider Try If lobjTripleDESCSP Is Nothing OrElse mobjDecryptTransform Is Nothing OrElse mobjEncryptTransform Is Nothing OrElse tlogForceInit Then lobjTripleDESCSP = New TripleDESCryptoServiceProvider() mobjDecryptTransform = lobjTripleDESCSP.CreateDecryptor(tbytaDESKey, tbytaDESIV) mobjEncryptTransform = lobjTripleDESCSP.CreateEncryptor(tbytaDESKey, tbytaDESIV) Console.WriteLine(mobjDecryptTransform.CanReuseTransform) Console.WriteLine(mobjDecryptTransform.CanTransformMultipleBlocks) End If Return True Catch ex As Exception End Try End Function ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
Re: [Mono-dev] DES problem
Hello, On Fri, 2006-05-12 at 11:36 +0200, Arnhoffer Károly wrote: > Hi, > > I've got a service application which uses encrypted data over network. > My decrypting function gives different results with the same inputs on > Mono which does not occour on .NET. The other mighty thing is that > when I grab this function out of the service and test it in itself > this problem does not happen again. What is wrong here? Difficult to say. Your code sample, beside being in VB.NET ;-), is incomplete. It only show the decryption process (the problem may be on the encryption side) and doesn't show how you create the ICryptoTransform (encryptor and decryptor) instances. Please fill a bug into bugzilla with a complete sample and state the version of the MS Fx that works and the version of Mono that doesn't. > My function looks as follows (test version): > > Friend Function DecryptDataDES(ByVal tbytaData As Byte(), ByVal > tbytaDESKey As Byte(), ByVal tbytaDESIV As Byte(), ByRef tbytaDecryptedData > As Byte()) As Boolean > Dim lobjMemStream As MemoryStream > Dim lobjCryptoStream As CryptoStream > Dim lbytaDESKey As Byte() > Dim lbytaDESIV As Byte() > Try > 'console.writeline() > ReDim lbytaDESKey(tbytaDESKey.Length - 1) > ReDim lbytaDESIV(tbytaDESIV.Length - 1) > tbytaDESKey.CopyTo(lbytaDESKey, 0) > tbytaDESIV.CopyTo(lbytaDESIV, 0) > > Console.WriteLine("tbytaData: " & > Convert.ToBase64String(tbytaData)) > Console.WriteLine("lbytaDESKey: " & > Convert.ToBase64String(lbytaDESKey)) > Console.WriteLine("lbytaDESIV: " & > Convert.ToBase64String(lbytaDESIV)) > > 'teszt > If Not InitTripleDESCSP(lbytaDESKey, lbytaDESIV) Then > 'ak20050721: ha nem kényszerítjük ki az újrainicializálást, > akkor linux/mono-n ugyanolyan bemenet mellett eltérő eredményt fog > 'adni a DecryptDataDES függvény. Emiatt > InitTripleDESCSP(lbytaDESKey, lbytaDESIV) helyett > InitTripleDESCSP(lbytaDESKey, lbytaDESIV, True) > 'szerepel a hívásban > 'console.writeline("Forced reinit...") > 'If Not InitTripleDESCSP(lbytaDESKey, lbytaDESIV, True) Then > 'mobjApp.Errors.Add(mstrResName, > CommErrEnum.CantDecryptDataDES, , , "InitTripleDESCSP").Raise() > Console.WriteLine("InitTripleDESCSP failed") > End If > > Console.WriteLine("lbytaDESKey: " & > Convert.ToBase64String(lbytaDESKey)) > Console.WriteLine("lbytaDESIV: " & > Convert.ToBase64String(lbytaDESIV)) > > lobjMemStream = New MemoryStream() > lobjCryptoStream = New CryptoStream(lobjMemStream, > mobjDecryptTransform, CryptoStreamMode.Write) > 'Írás közben dekódolja az adatot > lobjCryptoStream.Write(tbytaData, 0, tbytaData.Length) > lobjCryptoStream.FlushFinalBlock() > lobjCryptoStream.Close()'e nélkül nem működik!!! > 'Kiolvassuk a dekódolt adatot > tbytaDecryptedData = lobjMemStream.ToArray() > > Console.WriteLine("tbytaDecryptedData: " & > Convert.ToBase64String(tbytaDecryptedData)) > > Return True > Catch ex As Exception > Console.WriteLine("DecryptDataDES failed") > Finally > Try > If Not lobjMemStream Is Nothing Then lobjMemStream.Close() > If Not lobjCryptoStream Is Nothing Then > lobjCryptoStream.Close() > Catch > End Try > End Try > End Function > > Thanks! > > Károly > ___ > Mono-devel-list mailing list > Mono-devel-list@lists.ximian.com > http://lists.ximian.com/mailman/listinfo/mono-devel-list -- Sebastien Pouliot <[EMAIL PROTECTED]> Blog: http://pages.infinit.net/ctech/ ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list
Re: [Mono-dev] DES problem
Hello Arnhoffer, Your code should *always* check for CanReuseTransform (and should also check for CanTransformMultipleBlock if it's not using a CryptoStream) and, if false, re-create the transform. The reason is the .NET crypto classes are pluggable - so it's possible that another implementation, or a future version of the FX, will change those values. Checking those values will make your code resistant to such changes and more adaptable to other algorithms or different implementations. On Thu, 2006-05-18 at 13:28 +0200, Arnhoffer Károly wrote: > Hi, > > There is a little difference between .NET and Mono. When I call the routine > below in .NET I get the results: > > True > True > > With Mono it says: > > False > True. > > Is it possible to set this CanReuseTransform property to true and reuse the > transform once again or shall I reinitialize it every time? (I found that > with Mono I can not decrypt two or more times with the same ICryptoTransform. > I get different results with same inputs.) > > The code snippet: > > Private mobjEncryptTransform As ICryptoTransform > Private mobjDecryptTransform As ICryptoTransform > > Private Function InitTripleDESCSP(ByVal tbytaDESKey As Byte(), ByVal > tbytaDESIV As Byte(), Optional ByVal tlogForceInit As Boolean = False) As > Boolean > Dim lobjTripleDESCSP As TripleDESCryptoServiceProvider > Try > If lobjTripleDESCSP Is Nothing OrElse mobjDecryptTransform Is > Nothing OrElse mobjEncryptTransform Is Nothing OrElse tlogForceInit Then > lobjTripleDESCSP = New TripleDESCryptoServiceProvider() > mobjDecryptTransform = > lobjTripleDESCSP.CreateDecryptor(tbytaDESKey, tbytaDESIV) > mobjEncryptTransform = > lobjTripleDESCSP.CreateEncryptor(tbytaDESKey, tbytaDESIV) > > Console.WriteLine(mobjDecryptTransform.CanReuseTransform) > > Console.WriteLine(mobjDecryptTransform.CanTransformMultipleBlocks) > End If > Return True > Catch ex As Exception > End Try > End Function > ___ > Mono-devel-list mailing list > Mono-devel-list@lists.ximian.com > http://lists.ximian.com/mailman/listinfo/mono-devel-list -- Sebastien Pouliot <[EMAIL PROTECTED]> Blog: http://pages.infinit.net/ctech/ ___ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list