2009-08-14 139 views
0

我在VB.net转换vb.net字节到PHP

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

我试图将其转换为PHP下面的代码。

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

这是行不通的。

完整的PHP代码如下:

<?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); 
?> 

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 

回答

1

PHP预计四是字符串,而不是一个阵列 - 这将转换为字符串“阵列”。

我猜测的字符串就是二进制数据和pack()功能可能是你所需要的。它不接受的阵列作为一个参数,但评论中的一个使用一个循环通过阵列来连接每个数组元素的填充值。

+0

感谢您指出我的错误。由于我的错误,这个问题不再有效。 谢谢! – shaiss 2009-08-14 18:46:01

0

,你说是不实际工作的正常工作就行了。我从来没有使用过PHP的第3版,但至少从那时起就起作用了。

的问题是,的mcrypt_generic_init的$四参数应该是一个字符串,而不是整数的阵列。

这应该转换工作:

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

对不起,我的PHP是生锈:)

+0

我将vb.net代码更改为:
Dim bytIV()As Byte = {100,98,97,101,110,121,100,105,110,116,117,122,119,112, 110,110} ,改变了PHP代码:
$ IV = “dbaenydintuzwpnn”;
仍然没有运气。我在vb.net中得到的回报是垃圾 – shaiss 2009-08-14 18:37:14