PHP中的AES/CBC/PKCS#5加密算法 [英] AES/CBC/PKCS#5 Encryption Algorithm In PHP

查看:95
本文介绍了PHP中的AES/CBC/PKCS#5加密算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用表单集成"方法将SagePay付款网关集成到网站中.

Im attempting to integrate the SagePay payment gateway into a website using the 'Form Integration' method.

基本上,表单集成方法的工作原理是:在网页中插入FORM并在每次选择FORM的Submit按钮时将信息发布到SagePay的服务器上.在将信息发送到SagePay的服务器之前,必须先使用AES/CBC/PKCS#5算法对其进行加密,然后再对Base 64进行编码.

Basically the form integration method works by inserting a FORM in a webpage and POSTing information to SagePay's servers whenever the Submit button for the FORM is selected. Before the information can be sent to SagePay's servers it must be encrypted using the AES/CBC/PKCS#5 algorithm, before being Base 64 encoded.

我具有加密的基本知识,但是我没有在PHP中使用加密的经验.有人可以帮我用PHP制定AES/CBC/PKCS#5算法吗?

I have a basic knowledge of encryption but I have no experience of using it in PHP. Can anyone please help me formulate the AES/CBC/PKCS#5 algorithm in PHP please?

到目前为止,这是我的努力:

Here's my efforts so far:

$CRYPT = "Text Goes Here";

$blocksize = 16;//Does 16 Refer To 16 Bytes Or 16 Bits? 16 Bytes = 128 Bits.
$cryptLength = strlen($CRYPT);
$cryptLengthModuloBlocksize = $cryptLength % $blocksize;
$pad = $blocksize - $cryptLengthModuloBlocksize;
$padString = str_repeat(chr($pad), $pad);
$CRYPT = $CRYPT . $padString;

$encryptionPassword = 'password';
$Encrypted_CRYPT = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $encryptionPassword, $CRYPT, MCRYPT_MODE_CBC);

$Base64_Encrypted_CRYPT = base64_encode($Encrypted_CRYPT);

echo    "<form action = 'https://test.sagepay.com/gateway/service/vspform-register.vsp' method = 'POST'>

            <input type = 'hidden' name = 'VPSProtocol' value = '3.00' />//
            <input type = 'hidden' name = 'TxType' value = 'PAYMENT' />
            <input type = 'hidden' name = 'Vendor' value = 'vendorName' />
            <input type = 'hidden' name = 'Crypt' value = '" . $Base64_Encrypted_CRYPT . "' />
            <input type= 'submit' value = 'Submit'>

        </form>";

非常感谢您的帮助.

推荐答案

是的,很好.(一旦有了适用于v3.00的PHP工具包,我们将在我们的网站上显示该工具包.)

Yes, that's fine. (As soon as we have a PHP kit for v3.00 we'll display it on our website).

希望以下内容对您有所帮助.

Hope the below helps you to.

示例:

采用Base64编码的AES加密AES加密,使用PKCS5填充进行CBC阻止,然后进行HEX编码

AES encryption with Base64 encoding AES encryption, CBC blocking with PKCS5 padding then HEX encoding

//** Wrapper function do encrypt an encode based on strEncryptionType setting **
function encryptAndEncode($strIn) {

global $strEncryptionType
      ,$strEncryptionPassword;

{
    //** AES encryption, CBC blocking with PKCS5 padding then HEX encoding **

    //** use initialization vector (IV) set from $strEncryptionPassword
    $strIV = $strEncryptionPassword;

    //** add PKCS5 padding to the text to be encypted
    $strIn = addPKCS5Padding($strIn);

    //** perform encryption with PHP's MCRYPT module
    $strCrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $strEncryptionPassword, $strIn, MCRYPT_MODE_CBC, $strIV);

    //** perform hex encoding and return
    return "@" . bin2hex($strCrypt);
}
}


//** Wrapper function do decode then decrypt based on header of the encrypted field **
function decodeAndDecrypt($strIn) {

global $strEncryptionPassword;

if (substr($strIn,0,1)=="@") 
{
    //** HEX decoding then AES decryption, CBC blocking with PKCS5 padding  **

    //** use initialization vector (IV) set from $strEncryptionPassword
    $strIV = $strEncryptionPassword;

    //** remove the first char which is @ to flag this is AES encrypted
    $strIn = substr($strIn,1); 

    //** HEX decoding
    $strIn = pack('H*', $strIn);

    //** perform decryption with PHP's MCRYPT module
    return removePKCS5Padding(
        mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $strEncryptionPassword, $strIn, MCRYPT_MODE_CBC, $strIV));
} 

}

这篇关于PHP中的AES/CBC/PKCS#5加密算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆