PHP mcrypt and VB.NET cryptoservice interop
- From: Ville Mattila <ville@xxxxxxxxxx>
- Date: Sun, 07 Jan 2007 11:11:44 +0200
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
---
.
- Prev by Date: Re: Kerberos authentication NOT in AD
- Next by Date: Client Authentication
- Previous by thread: Re: Kerberos authentication NOT in AD
- Next by thread: Client Authentication
- Index(es):
Relevant Pages
|