1

I am trying to encrypt a string with a key and iv with Rijndael in PHP.

I have wrote this in VB .Net and the result is the expected one:

'This is the byte Array of "1234567890ABCDEF1234567890ABCDEF". I am using this for IV
Private _key1 As Byte() = New Byte(15) {CByte(18), CByte(52), CByte(86), CByte(120), CByte(144), CByte(171), _
        CByte(205), CByte(239), CByte(18), CByte(52), CByte(86), CByte(120), _
        CByte(144), CByte(171), CByte(205), CByte(239)} 


Public Sub Init()
    Dim sKey As String = "D6850B89370BD603BD48CEAD43488639DE3ABE73D2CF7C54B0CF72D2FB06E162"
    Dim resBytes As Byte() = Me.EncryData("a", sKey)
    File.WriteAllBytes("C:/myFile.txt", resBytes)
End Sub


Public Function EncryData(sPath As String, sKey As String) As Byte()       
    Return Me.AESEncrypt(plainText, Me.Data_Hex_Asc(sKey))
End Function


Public Function Data_Hex_Asc(ByRef Data As String) As Byte()
    Dim str1 As String = ""            
    Dim list As New List(Of Byte)()
    While Data.Length > 0
        Dim num As Integer = CInt(Convert.ToChar(Convert.ToUInt32(Data.Substring(0, 2), 16)))
        Dim str2 As String = Convert.ToChar(Convert.ToUInt32(Data.Substring(0, 2), 16)).ToString()
        list.Add(CByte(Convert.ToUInt32(Data.Substring(0, 2), 16)))
        str1 += str2
        Data = Data.Substring(2, Data.Length - 2)
    End While            
    Return list.ToArray()
End Function


Public Function AESEncrypt(plainText As String, aByte As Byte()) As Byte()          
    Dim symmetricAlgorithm As SymmetricAlgorithm = DirectCast(Rijndael.Create(), SymmetricAlgorithm)
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(plainText)    
    symmetricAlgorithm.Key = aByte
    symmetricAlgorithm.IV = Me._key1

    Dim memoryStream As New MemoryStream()
    Dim cryptoStream As New CryptoStream(DirectCast(memoryStream, Stream), symmetricAlgorithm.CreateEncryptor(), CryptoStreamMode.Write)
    cryptoStream.Write(bytes, 0, bytes.Length)
    cryptoStream.FlushFinalBlock()

    Dim numArray As Byte() = memoryStream.ToArray()
    cryptoStream.Close()
    memoryStream.Close()

    Return numArray
End Function

Now the PHP version of that:

$AES_KEY = "D6850B89370BD603BD48CEAD43488639DE3ABE73D2CF7C54B0CF72D2FB06E162";  
$key = pack('H*', $AES_KEY);

$plaintext =  "a"; ;    
$iv = pack('H*', "1234567890ABCDEF1234567890ABCDEF");    

$enc_text = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_CBC, $iv); 

The result from PHP code is different from the VB's. Any suggestions for different php approach?

VB .net implementation of Rijndael uses as default padding: PKCS7 and mode: CBC

Results: vb .net: PFΑ&\O?„\LϋAμCt php:_yJ_½%sAj«SUhA 16 chars length, both of them.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
kokazani
  • 213
  • 2
  • 13
  • 2
    Different padding is the post likely candidate. Search for a PKCS7/5 implementation for php. – CodesInChaos Feb 08 '15 at 21:42
  • Always print out ciphertext in an encoded form for human inspection; hex is best. It is meaningless as characters. Note that php assumes zero padding, not PKCS7 padding. You will have to implement PKCS7 padding yourself. – President James K. Polk Feb 08 '15 at 21:59

0 Answers0