将 vb.net 字节转换为 php

发布于 2024-08-02 03:08:51 字数 4625 浏览 7 评论 0原文

我在 VB.net 中有以下代码,

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

I have the following code in VB.net

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

I'm trying to convert it to php.

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

That doesn't work.

The complete php code below:

    <?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 code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2024-08-09 03:08:51

PHP 期望 IV 是一个字符串,而不是一个数组——它将被转换为字符串“Array”。

我猜测该字符串只是二进制数据, pack() 函数可能正是您所需要的。 它不接受数组作为参数,但其中一个注释使用数组循环来连接每个数组元素的打包值。

PHP expects the IV to be a string, not an array - which will be cast to the string "Array".

I'm guessing that the string is just binary data and the pack() function may be what you need. It doesn't accept an array as a parameter, but one of the comments uses a loop through the array to concatenate the packed values of each array element.

笑饮青盏花 2024-08-09 03:08:51

你所说的行不通实际上工作正常。 我从未使用过 PHP 3 之前的版本,但至少从那时起它就一直有效。

问题是 mcrypt_generic_init 的 $iv 参数应该是字符串,而不是整数数组。

这应该适用于转换:

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

抱歉,我的 PHP 生锈了:)

The line that you say isn't working actually works fine. I have never used PHP pre version 3, but it has worked since then at least.

The problem is that the $iv parameter of mcrypt_generic_init is supposed to be a string, not an array of ints.

This should work for conversion:

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

Sorry, my PHP is rusty :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文