2011-08-31 65 views
0

我正在嘗試複製在PHP中用VB.Net編寫的應用程序的一部分已經存在的加密方法。得到的加密值必須相同。我沒有太多的加密經驗,儘管我努力在網上搜索我的加密值不匹配的信息。有人能讓我知道我的PHP代碼中哪裏出錯嗎?在PHP中複製.Net加密

這是.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)); 
} 
?> 

回答

1

看起來像一個錯字

Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32} 
$keyArray=array(42, 16, 93, 18, 156, 78, 4, 32); 

嘗試 $ keyArray =陣列(42,16,93,156,78,4,218,32);

+0

謝謝,我做了這個改變,果然它的工作原理。 – Jason

0

我有類似的問題,從.Net RSA加密數據在PHP解密。通常它歸結爲一個字符集問題。如果可能,請確保兩個系統都將字符串值處理爲UTF-8字符串。