开发者

Replicating .Net encryption in PHP

开发者 https://www.devze.com 2023-04-01 11:51 出处:网络
I am attempting to replicate the encryption method that is already in existence for part of an application that was written in VB.Net, in PHP. The resulting encrypted values must be the same. I don\'t

I am attempting to replicate the encryption method that is already in existence for part of an application that was written in VB.Net, in PHP. The resulting encrypted values must be the same. I don't have much experience doing encryption and de开发者_如何学运维spite my best effort in scouring the web for information my encrypted values do not match. Could someone let me know where I am going wrong in my PHP code?

Here is the .Net process. Unfortunately this method cannot be changed at this time.

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

Here is my 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));
}
?>


Looks like a typo

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

try $keyArray=array(42, 16, 93, 156, 78, 4, 218, 32);


I've had similar issues taking RSA-encrypted data from .Net to be decrypted in PHP. Typically it came down to a character set issue. If possible, make sure both systems are handling string values as UTF-8 strings.

0

精彩评论

暂无评论...
验证码 换一张
取 消