Rijndael 128在vb.net和php之间加密/解密

时间:2012-07-21 19:49:54

标签: php vb.net encryption rijndael

修改

抱歉这个问题,我自己马上解决了...... 我唯一要做的就是将填充设置为零:

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,但不是相同的输出。现在有人在这里错了吗?

谢谢你,抱歉我的英文不好:)

0 个答案:

没有答案