【发布时间】:2011-11-07 20:14:19
【问题描述】:
我正在尝试复制已经存在的加密方法,该方法用于在 PHP 中用 VB.Net 编写的应用程序的一部分。生成的加密值必须相同。我没有太多的加密经验,尽管我尽最大努力在网上搜索信息,但我的加密值不匹配。有人可以告诉我我的 PHP 代码哪里出错了吗?
这是 .Net 进程。很遗憾,目前无法更改此方法。
Public Class Encrypt
'8 bytes randomly selected for both the Key and the Initialization Vector
'the IV is used to encrypt the first block of text so that any repetitive
'patterns are not apparent
Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32}
Private Shared IV_64() As Byte = {55, 103, 246, 79, 36, 99, 167, 3}
Public Function EncryptPwd(ByVal value As String) As String
Try
Dim cryptoProvider As DESCryptoServiceProvider = _
New DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = _
New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), _
CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)
sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()
'convert back to a string
Return Convert.ToBase64String(ms.GetBuffer(), 0, CInt(ms.Length))
Finally
End Try
End Function
End Class
这是我的 PHP。
<?php
function addpadding($string, $blocksize = 8)
{
$len = strlen($string);
$pad = $blocksize - ($len % $blocksize);
$string .= str_repeat(chr($pad), $pad);
return $string;
}
?>
<form id="form1" name="form1" method="post" action="">
enter text
<input name="data" type="text" />
<input type="hidden" value="op" name="op" />
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
if(!isset($_POST['op'])) {
}else {
$buffer = $_POST['data'];
$keyArray=array( 42, 16, 93, 18, 156, 78, 4, 32 );
$key=null;
foreach ($keyArray as $element)
$key.=CHR($element);
$ivArray=array( 55, 103, 246, 79, 36, 99, 167, 3 );
$iv=null;
foreach ($ivArray as $element)
$iv.=CHR($element);
echo "Key: " .$key. "<br>";
echo "IV: " .$iv. "<br>";
echo "Result: " .base64_encode(mcrypt_cbc(MCRYPT_DES, $key, addpadding($buffer), MCRYPT_ENCRYPT, $iv));
}
?>
【问题讨论】:
标签: php asp.net encryption