Question:
I have some code in PHP that allows me to encrypt a string that I then send as a header in the HTTP request to a web service. I am trying to emulate this encrypt function in VB.Net code but so far I am having absolutely no luck. I have downloaded the trial of the Chilkat .Net 4 component and modified the code found in the "AES Encryption to Match PHP's Mcrypt Extension" example. Still no luck. The result I get from the VB app is different than the result I get from the PHP code.
Here is the PHP code:
function encrypt($data) {
$key = 'd40da16b8bb6b5e33ee3ae5f704edb2b';
$iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
//Here I am padding the string that needs to be encrypted with the same ascii character as the number of characters left until the 16 characters block is completed. So if the string has 3 characters we will add 13 characters of chr(13)
$mod = strlen($data)%16;
$padding = 16-$mod;
for ($i=0;$i<$padding;$i++) {
$data = $data.chr($padding);
}
$enc = "";
$enc = mcrypt_cbc (MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_ENCRYPT, $iv);
return base64_encode($enc);
}
Here is what I am trying in VB.Net:
Public Shared Function ChilkatEncrypt(ByVal strData As String)
Dim crypt As New Chilkat.Crypt2()
Dim key as string = "d40da16b8bb6b5e33ee3ae5f704edb2b"
Dim IV as string = "0000000000000000"
Dim success As Boolean
success = crypt.UnlockComponent("Anything for 30-day trial")
If (success <> True) Then
MsgBox("Crypt component unlock failed")
Exit Function
End If
' AES is also known as Rijndael.
crypt.CryptAlgorithm = "aes"
' CipherMode may be "ecb" or "cbc"
crypt.CipherMode = "cbc"
' KeyLength may be 128, 192, 256
crypt.KeyLength = 128
' Pad with NULL bytes (PHP pads with NULL bytes)
crypt.PaddingScheme = 3
' EncodingMode specifies the encoding of the output for
' encryption, and the input for decryption.
' It may be "hex", "url", "base64", or "quoted-printable".
crypt.EncodingMode = "base64"
crypt.SetEncodedIV(strIV, "hex")
' The secret key must equal the size of the key. For
' 256-bit encryption, the binary secret key is 32 bytes.
' For 128-bit encryption, the binary secret key is 16 bytes.
crypt.SetEncodedKey(strKey, "hex")
Dim plainText As String
plainText = strData
Dim cipherText As String
cipherText = crypt.EncryptStringENC(plainText)
MsgBox(cipherText)
Return cipherText
End Function
If I try to encrypt the string
2||
the PHP function returns: NvjAZbgLuIc6g3kH+Y2RTw==
and the VB.Net function returns:H6mOmvHnXPp+NJGH9V1tmw==
I would appreciate anyone who could tell me what I am missing.
Thanks.
Did you use this php code earlier? Here are the modified codes:
function encrypt($data) {
$key = "d40da16b8bb6b5e33ee3ae5f704edb2b";
$iv = "0000000000000000";
//Here I am padding the string that needs to be encrypted with the same ascii character as the number of characters left until the 16 characters block is completed. So if the string has 3 characters we will add 13 characters of chr(13)
$mod = strlen($data)%16;
$padding = 16-$mod;
for ($i=0;$i<$padding;$i++) {
$data = $data.chr($padding);
}
$enc = "";
$enc = mcrypt_cbc (MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_ENCRYPT, $iv);
return base64_encode($enc);
}
Public Shared Function ChilkatEncrypt(ByVal strData As String)
Dim crypt As New Chilkat.Crypt2()
Dim key As String = "d40da16b8bb6b5e33ee3ae5f704edb2b"
Dim IV As String = "0000000000000000"
Dim success As Boolean
success = crypt.UnlockComponent("Anything for 30-day trial")
If (success <> True) Then
MsgBox("Crypt component unlock failed")
Exit Function
End If
' AES is also known as Rijndael.
crypt.CryptAlgorithm = "aes"
' CipherMode may be "ecb" or "cbc"
crypt.CipherMode = "cbc"
' KeyLength may be 128, 192, 256
crypt.KeyLength = 128
' Pad with NULL bytes (PHP pads with NULL bytes)
crypt.PaddingScheme = 0
' EncodingMode specifies the encoding of the output for
' encryption, and the input for decryption.
' It may be "hex", "url", "base64", or "quoted-printable".
crypt.EncodingMode = "base64"
crypt.SetEncodedIV(IV, "ascii")
' The secret key must equal the size of the key. For
' 256-bit encryption, the binary secret key is 32 bytes.
' For 128-bit encryption, the binary secret key is 16 bytes.
crypt.SetEncodedKey(key, "ascii")
Dim plainText As String
plainText = strData
Dim cipherText As String
cipherText = crypt.EncryptStringENC(plainText)
MsgBox(cipherText)
Return cipherText
End Function
Test string: 2||
vb return: mu+H+0S1FPvUHNKK96+/MQ==
php return: mu+H+0S1FPvUHNKK96+/MQ==
$iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
in php is null char, not 0 so in the VB code you have to use null char isntead of 0.
And you have to change: crypt.SetEncodedKey(strKey, "hex")
to: crypt.SetEncodedKey(strKey, "ascii")