5

编辑:

对不起这个问题,我自己立即解决了......我唯一要做的就是将填充设置为零:

aes.Padding = PaddingMode.Zeros

我正在尝试在没有 SSL 的情况下加密我的 VB.Net 应用程序之间的流量(因为用户不应该只能看到一些敏感参数)。所以尝试在 PHP 和 VB.Net 中实现相同的加密/解密结果,但它不起作用。

起初我的小型 VB.Net 加密功能:

Friend Enum CryptionMode
    Encrypt
    Decrypt
End Enum

Friend Function cryptAes(ByVal mode As CryptionMode, ByVal input As Byte(), ByVal pw As String) As Byte()
    Dim salt As Byte() = {42, 248, 0, 22, 19, 46, 162, 81, 192, 167, 174, 102, 233}
    Dim aes As New RijndaelManaged

    Dim keyGen As New Rfc2898DeriveBytes(pw, salt)
    aes.Key = keyGen.GetBytes(aes.Key.Length)
    aes.IV = keyGen.GetBytes(aes.IV.Length)

    Debug.WriteLine(Convert.ToBase64String(aes.Key))
    Debug.WriteLine(Convert.ToBase64String(aes.IV))

    Dim ms As New MemoryStream
    Dim cs As New CryptoStream(ms, If(mode = CryptionMode.Decrypt, aes.CreateDecryptor, aes.CreateEncryptor), CryptoStreamMode.Write)
    Try
        cs.Write(input, 0, input.Length)
        cs.Close()
        Dim output As Byte() = ms.ToArray
        Debug.WriteLine(output.Length)
        ms.Close()
        Return output
    Catch ex As Exception
        Debug.WriteLine("Error")
        Return Nothing
    End Try
End Function

Friend Function cryptAesToBase64(ByVal mode As CryptionMode, ByVal input As Byte(), ByVal pw As String) As String
    Return Convert.ToBase64String(cryptAes(mode, input, pw))
End Function

通过执行这一行

cryptAesToBase64(CryptionMode.Encrypt, System.Text.Encoding.ASCII.GetBytes("hallihallo"), "pass")

我得到了以下

9sNq3BjYaU6ZIrLEfG1hXrkdOoGc6FoeCQ3T2asXSs4= (key)
oxBgLuPha+Rvm7KV5+3V3A== (IV)
16 (output length)
"hwcc7Cog9FornwAzo6hIuA==" (output)

但是现在有了 PHP 加密:起初我需要定义一个等效于 Rfc2898DeriveBytes 函数的函数:

function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
{
    $algorithm = strtolower($algorithm);
    if(!in_array($algorithm, hash_algos(), true))
        die('PBKDF2 ERROR: Invalid hash algorithm.');
    if($count <= 0 || $key_length <= 0)
        die('PBKDF2 ERROR: Invalid parameters.');

    $hash_length = strlen(hash($algorithm, "", true));
    $block_count = ceil($key_length / $hash_length);

    $output = "";
    for($i = 1; $i <= $block_count; $i++) {
        // $i encoded as 4 bytes, big endian.
        $last = $salt . pack("N", $i);
        // first iteration
        $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
        // perform the other $count - 1 iterations
        for ($j = 1; $j < $count; $j++) {
            $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
        }
        $output .= $xorsum;
    }
    return substr($output, 0, $key_length);
}

现在加密命令:

$mySalt = base64_decode("KvgAFhMuolHAp65m6Q==");
$dev = pbkdf2("sha1", "pass", $mySalt, 1000, 48, true);
$key = substr($dev, 0, 32); //Keylength: 32
$iv = substr($dev, 32, 16); // IV-length: 16

echo "key: ".base64_encode($key)."<br>";
echo "iv: ".base64_encode($iv)."<br>";

$text = base64_decode("aGFsbGloYWxsbw=="); //"hallihallo";
$crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
echo strlen($crypttext)."<br>";
echo base64_encode($crypttext);

通过执行此操作,我得到以下输出:

key: 9sNq3BjYaU6ZIrLEfG1hXrkdOoGc6FoeCQ3T2asXSs4=
iv: oxBgLuPha+Rvm7KV5+3V3A==
16 (output length)
BeyMKI9PAy5IThiM6XcsVQ==

现在我有点困惑,因为我在 PHP 和 VB.Net 中有相同的键、相同的 IV、相同的输入、相同的 Ciphermode,但输出不同。有人现在有什么问题吗?

谢谢你,对不起我的英语不好:)

4

0 回答 0