TripleDES加密在PHP中没有与VB中相同的值

时间:2015-02-26 21:24:04

标签: php vb.net tripledes

我找到了许多解释如何查看C#与PHP的解决方案,但没有一个特定于VB。我确实找到了一个有关使用零填充PHP填充的有用提示,但这并没有解决问题,因为我在VB中匹配它并没有帮助。无论如何,我熟悉VB,我们有另一个熟悉PHP的开发人员,我们正在努力使我们的函数匹配,以便tripleDES加密在两者中吐出相同的值。我们正在与第三方应用程序集成,我知道VB代码正在吐出正确的值,但PHP不是。我有下面列出的VB和PHP代码,那里的任何人都应该熟悉两者,以便知道为什么PHP代码没有返回正确的值?我知道从PHP返回的值是正确的长度,但有些东西是关闭的,它不匹配。请帮忙。

VB:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim dataToHash As String
    Dim encryptedText As String = ""

    dataToHash = "hereismystring"
    Dim buffer As Byte() = Encryption(dataToHash, "abcd1234")
    encryptedText = Convert.ToBase64String(buffer)

    lblToken.Text = encryptedText.ToString()
End Sub

Public Shared Function Encryption(ByVal PlainText As String, ByVal key As String) As Byte()
    Dim des As TripleDES = CreateDES(key)
    Dim ct As ICryptoTransform = des.CreateEncryptor()
    Dim input As Byte() = Encoding.Unicode.GetBytes(PlainText)
    Return ct.TransformFinalBlock(input, 0, input.Length)
End Function

Private Shared Function CreateDES(ByVal key As String) As TripleDES
    Dim md5 As MD5 = New MD5CryptoServiceProvider()
    Dim des As TripleDES = New TripleDESCryptoServiceProvider()
    des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key))
    des.IV = New Byte(des.BlockSize / 8 - 1) {}
    des.Padding = PaddingMode.Zeros
    Return des
End Function

PHP:     

$start = "hereismystring";

$cipher = MCRYPT_TRIPLEDES;
$mode = MCRYPT_MODE_CBC;

$rawKey = "abcd1234";
$ssoKey = md5($key_encoded,true);

$key_size = strlen($ssoKey);
$iv_size = mcrypt_get_iv_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_CBC);
$ssoIV = mcrypt_create_iv($iv_size, MCRYPT_RAND);

$user_str = mb_convert_encoding($start,"UTF-16LE");    

$key_blocksize = mcrypt_get_block_size($cipher,$mode);
$key_padding_size = $key_blocksize - (strlen($user_str) % $key_blocksize);

$user_str .= str_repeat(chr($key_padding_size), $key_padding_size);
$key_iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$mode),"");
$key_triple = substr($key_encoded,0,mcrypt_get_key_size($cipher,$mode));

$key_encoded_text = mcrypt_encrypt($cipher,$key_triple,$user_str,$mode,$key_iv);

$final = base64_encode($key2_encoded_text);

echo "<p>" . $final </p>";     
?>

谢谢, 乔

0 个答案:

没有答案