PHP mcrypt and VB.NET cryptoservice interop



Hi readers,

I am consuming a PHP-based web service over VB.NET but having some problems with encryption. Web service sends an TripleDES-encrypted value (Base64 encoded) that VB.NET application should decrypt with the known key and IV.

The process is almost successful, but for some reason - the characters in positions 3-6 of the total length of 64 are different compared to original string at PHP side. All other characters are correct. I haven't been able to find any solution why this happens and does it happen at PHP or VB.NET side.

Here is one example of difference:
Original:
SXC6Y9JHfwpc6NPk7eSJ6auuJRpfVuvEiYA6XaE3xT6txLEvPn4Mop7X7m3THonPd
Decrypted:
SXE0/V^Nfwpc6NPk7eSJ6auuJRpfVuvEiYA6XaE3xT6txLEvPn4Mop7X7m3THonPd

As you can see, the only difference is from positions 3 to 6. This behaviour repeats every time when running the application.

Thank you for any help,

Ville




The decryption code follows:
----
Public Function TripleDesB64decode(ByVal EncryptedBase64 As String, ByVal Key As String, ByVal IV As String) As String
'initialize our key
Dim tripleDESKey As SymmetricAlgorithm = SymmetricAlgorithm.Create("TripleDES")

tripleDESKey.Key = Encoding.ASCII.GetBytes(Key)
tripleDESKey.IV = Encoding.ASCII.GetBytes(IV)
tripleDESKey.Mode = CipherMode.CBC
tripleDESKey.Padding = PaddingMode.PKCS7

'load our encrypted value into a memory stream
Dim encryptedStream As MemoryStream = New MemoryStream

encryptedStream.Write(Convert.FromBase64String(EncryptedBase64), 0, Convert.FromBase64String(EncryptedBase64).Length)
encryptedStream.Position = 0

'set up a stream to do the decryption
Dim cs As CryptoStream = New CryptoStream(encryptedStream, tripleDESKey.CreateDecryptor, CryptoStreamMode.Read)
Dim decryptedStream As MemoryStream = New MemoryStream
Dim buf() As Byte = New Byte(2048) {}
Dim bytesRead As Integer

'keep reading from encrypted stream via the crypto stream
'and store that in the decrypted stream
bytesRead = cs.Read(buf, 0, buf.Length)
While (bytesRead > 0)
decryptedStream.Write(buf, 0, bytesRead)
bytesRead = cs.Read(buf, 0, buf.Length)
End While

'reassemble the decrypted stream into a string
Dim decryptedValue As String =
Encoding.ASCII.GetString(decryptedStream.ToArray())

Return decryptedValue
End Function
---
.



Relevant Pages

  • Length of the data to decrypt is invalid
    ... I found this code on a site for doing string encryption/decryption. ... // Create a symmetric algorithm. ... // This is done to make encryption more secure. ... // This will tell it that we have done our decryption ...
    (microsoft.public.dotnet.framework.aspnet)
  • How do I get this to work ?
    ... Public Function encrypt(ByRef textPIN As String, ByRef textKey As String, ... Dim cryptPINReadBinary As New BinaryReader ... Dim decryption As ICryptoTransform ...
    (microsoft.public.dotnet.security)
  • Re: encrypting/ decrypting with RSA
    ... should not be able to distinguish encryption of two messages of his/her own ... otherwise semantic security is totally compromised*. ... > Function RSADecrypt(ByVal instring As String) As String ... > Dim RSA As RSACryptoServiceProvider = New ...
    (microsoft.public.dotnet.security)
  • Re: Encrypt My.Settings setting?
    ... dim EncClass as new Encryption ... dim txtPlainTextPassword as string = "ThisIsMyNewPasswordSoThere!" ... Here's the Encryption Class... ... ' Create the encoder to write to the stream. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: cryptography
    ... The Rijndael class is intended to be the ... > For a couple examples of simple encryption using the RijndaelManaged class ... >> internal static void Encrypt(string StringToCrypt, string fileOut, string ... >> when i run the above code, encryption is smooth,however when decryption ...
    (microsoft.public.dotnet.languages.csharp)