Jun 12, 2014 04:29 AM
(This post was last modified: Jun 12, 2014 04:31 AM by mcly.)
TL;DR : The aes decryption does not remove padding from my php encrypted data
Now lets show you some code im using, first of all, yes the keysize, the blocksize, the Ciphermode and the padding for both php and vbnet match (i use mcrypt_encrypt for php, which only supports zero padding as ive read, vbnet uses zero padding too)
Anyway, here is my php aes encryption code
Seems pretty simple right? Anyway, here is my matching vb.net code
Now this will result in me getting a message that is properly decrypted, but has the padding not removed (as i desribed, "hello" gets decrypted fine, but it contains 0's from the padding.. (the amount of 0's are always different and go from 20-40)
Can someone help me or guide me to what im doing wrong?
Quote:So i have a php page that returns encrypted content, the client downloads this content and then decrypts it using aes, viewing this decrypted "text" it looks just like the original message, but when i tried to display the data, it seems like there are like 20 spaces after the decrypted text ( the stauts code displays the message up until to the decrypted message, then there is space that covers the rest of the message)
So i've when you debug this, you can only see the decrypted message, no space, no "0", no nothing, just the decrypted message in quotes
for example : "hello"
Now when i viewed the lenght of this string, the lenght was way above it was supposed to be (32) which leads to the conclusion that there is some random space/nulls (which is probably padding) that didnt get removed from the aes decryption function..
hexing this string showed that the "hello" part existed, but after that it was filled with around twenty 0's
So ive made a temporary fix by hexing the string, then removing the 0's and converting the hex back to a string..
Now lets show you some code im using, first of all, yes the keysize, the blocksize, the Ciphermode and the padding for both php and vbnet match (i use mcrypt_encrypt for php, which only supports zero padding as ive read, vbnet uses zero padding too)
Anyway, here is my php aes encryption code
PHP Code
<?php
function encryptRJ256($string_to_encrypt){
$key = "_mykeyhere_";
$iv = "_mykeyhere_";
$rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv);
$rtn = base64_encode($rtn);
return($rtn);}
Seems pretty simple right? Anyway, here is my matching vb.net code
PHP Code
<?php
Public Function PHPDecrypt(ByVal prm_text_to_decrypt As String) As String
Try
Dim sEncryptedString As String = prm_text_to_decrypt
Dim myRijndael As New RijndaelManaged
myRijndael.Padding = PaddingMode.Zeros
myRijndael.Mode = CipherMode.CBC
myRijndael.KeySize = 256
myRijndael.BlockSize = 256
Dim key() As Byte = System.Text.Encoding.ASCII.GetBytes("_mykeyhere_") ' Key
Dim IV() As Byte = System.Text.Encoding.ASCII.GetBytes("_mykeyhere_") 'IV
Dim decryptor As ICryptoTransform = myRijndael.CreateDecryptor(key, IV)
Dim sEncrypted As Byte() = Convert.FromBase64String(sEncryptedString)
Dim fromEncrypt() As Byte = New Byte(sEncrypted.Length) {}
Dim msDecrypt As New MemoryStream(sEncrypted)
Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)
Return System.Text.Encoding.ASCII.GetString(fromEncrypt)
Catch ex As Exception
Return prm_text_to_decrypt
End Try
End Function
Now this will result in me getting a message that is properly decrypted, but has the padding not removed (as i desribed, "hello" gets decrypted fine, but it contains 0's from the padding.. (the amount of 0's are always different and go from 20-40)
Can someone help me or guide me to what im doing wrong?
