login about faq

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.

asked Jun 07 '12 at 23:26

keiffer6's gravatar image

keiffer6
1111


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==

link

answered Jun 08 '12 at 04:31

dss's gravatar image

dss
16810

edited Jun 08 '12 at 04:40

The web service that this code will be sending the request to is not something I have control over. That being said, the PHP code that I originally posted worked correctly and achieved the correct output. I need to get the VB.Net code to produce the same output as the PHP code does. So, the PHP code does not need to be modified. Only the VB.Net code is in question.

(Jun 08 '12 at 09:26) keiffer6

$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")

link

answered Jun 08 '12 at 09:36

dss's gravatar image

dss
16810

edited Jun 08 '12 at 09:38

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×43
×35
×33
×12

Asked: Jun 07 '12 at 23:26

Seen: 24,263 times

Last updated: Jun 08 '12 at 09:38

powered by OSQA