2009-08-14 5 views
0

Je le code suivant dans VB.netoctet vb.net Convertir php

Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62} 

Je suis en train de le convertir en php.

$iv = array(121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62); 

Cela ne fonctionne pas.

Le code complet php ci-dessous:

<?php 
    $key = "lvvxmzmfrqeephxwmifwvyyllivhzbdi"; 
    $input = "this is a secret keythis is a secret keythis is a secret keythis is a secret key"; 

    $td = mcrypt_module_open('rijndael-128', '', 'ofb', ''); 
    //$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
    $iv = array(121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62); 
    mcrypt_generic_init($td, $key, $iv); 
    $encrypted_data = mcrypt_generic($td, $input); 
    mcrypt_generic_deinit($td); 
    mcrypt_module_close($td); 

    echo "IV: $iv <br><br>"; 

    echo base64_encode($encrypted_data); 
?> 

Code VB.net:

Public Function DecryptString128Bit(ByVal vstrStringToBeDecrypted As String, _ 
            ByVal vstrDecryptionKey As String) As String 

    Dim bytDataToBeDecrypted() As Byte 
    Dim bytTemp() As Byte 
    Dim bytIV() As Byte = {121, 241, 10, 1, 132, 74, 11, 39, 255, 91, 45, 78, 14, 211, 22, 62} 
    Dim objRijndaelManaged As New RijndaelManaged() 
    Dim objMemoryStream As MemoryStream 
    Dim objCryptoStream As CryptoStream 
    Dim bytDecryptionKey() As Byte 

    Dim intLength As Integer 
    Dim intRemaining As Integer 
    Dim intCtr As Integer 
    Dim strReturnString As String = String.Empty 
    Dim achrCharacterArray() As Char 
    Dim intIndex As Integer 

    ' ***************************************************************** 
    ' ****** Convert base64 encrypted value to byte array  ****** 
    ' ***************************************************************** 

    bytDataToBeDecrypted = Convert.FromBase64String(vstrStringToBeDecrypted) 

    ' ******************************************************************** 
    ' ****** Encryption Key must be 256 bits long (32 bytes)  ****** 
    ' ****** If it is longer than 32 bytes it will be truncated. ****** 
    ' ****** If it is shorter than 32 bytes it will be padded  ****** 
    ' ****** with upper-case Xs.         ****** 
    ' ******************************************************************** 

    intLength = Len(vstrDecryptionKey) 

    If intLength >= 32 Then 
     vstrDecryptionKey = Strings.Left(vstrDecryptionKey, 32) 
    Else 
     intLength = Len(vstrDecryptionKey) 
     intRemaining = 32 - intLength 
     vstrDecryptionKey = vstrDecryptionKey & Strings.StrDup(intRemaining, "X") 
    End If 

    bytDecryptionKey = Encoding.ASCII.GetBytes(vstrDecryptionKey.ToCharArray) 

    ReDim bytTemp(bytDataToBeDecrypted.Length) 

    objMemoryStream = New MemoryStream(bytDataToBeDecrypted) 

    ' *********************************************************************** 
    ' ****** Create the decryptor and write value to it after it is ****** 
    ' ****** converted into a byte array        ****** 
    ' *********************************************************************** 

    Try 

     objCryptoStream = New CryptoStream(objMemoryStream, _ 
      objRijndaelManaged.CreateDecryptor(bytDecryptionKey, bytIV), _ 
      CryptoStreamMode.Read) 

     objCryptoStream.Read(bytTemp, 0, bytTemp.Length) 

     objCryptoStream.FlushFinalBlock() 
     objMemoryStream.Close() 
     objCryptoStream.Close() 

    Catch 

    End Try 

    ' ***************************************** 
    ' ****** Return decypted value  ****** 
    ' ***************************************** 

    Return StripNullCharacters(Encoding.ASCII.GetString(bytTemp)) 

End Function 


Public Function StripNullCharacters(ByVal vstrStringWithNulls As String) As String 

    Dim intPosition As Integer 
    Dim strStringWithOutNulls As String 

    intPosition = 1 
    strStringWithOutNulls = vstrStringWithNulls 

    Do While intPosition > 0 
     intPosition = InStr(intPosition, vstrStringWithNulls, vbNullChar) 

     If intPosition > 0 Then 
      strStringWithOutNulls = Left$(strStringWithOutNulls, intPosition - 1) & _ 
           Right$(strStringWithOutNulls, Len(strStringWithOutNulls) - intPosition) 
     End If 

     If intPosition > strStringWithOutNulls.Length Then 
      Exit Do 
     End If 
    Loop 

    Return strStringWithOutNulls 

End Function 

Répondre

1

PHP attend la IV d'être une chaîne, pas un tableau - qui sera jeté à la chaîne " Array ".

Je devine que la chaîne est juste des données binaires et la fonction pack() peut être ce dont vous avez besoin. Il n'accepte pas de tableau en tant que paramètre, mais l'un des commentaires utilise une boucle dans le tableau pour concaténer les valeurs condensées de chaque élément du tableau.

+0

Merci d'avoir signalé mon erreur. En raison de mon erreur, cette question n'est plus vraiment valable. Merci beaucoup! – shaiss

0

La ligne que vous dites ne fonctionne pas fonctionne réellement bien. Je n'ai jamais utilisé PHP avant la version 3, mais cela a fonctionné depuis au moins.

Le problème est que le paramètre $ iv de mcrypt_generic_init est censé être une chaîne et non un tableau d'ints.

Cela devrait fonctionner pour la conversion:

$iv_string = ""; 
foreach ($iv as $char) 
    $iv_string .= chr($char); 

Désolé, mon PHP est rouillé :)

+0

J'ai changé le code de vb.net en:
Dim bytIV() As Byte = {100, 98, 97, 101, 110, 121, 100, 105, 110, 116, 117, 122, 119, 112, 110, 110} Et changé le code php à:
$ iv = "dbaenydintuzwpnn";
toujours pas de chance. Le retour que je reçois dans vb.net est la poubelle – shaiss

Questions connexes