2017-02-02 39 views
-1

我正在尝试使用Python和Flask与Sagepay进行表单集成。该方法的Python使用PKCS#5填充AES MODE_CBC填充

部分是执行以下操作:

地穴领域

  1. 地穴字段应包含以纯文本的所有其他交易信息作为名称=值字段由'分隔&'字符。确保所有必填字段都存在,并且'&'字符后没有空格。
  2. 然后使用AES(块大小为128位)在CBC模式下使用PKCS#5填充使用提供的密码作为密钥和初始化矢量并以十六进制编码结果(确保字母在大写)。
  3. Prependthe'@'signtothebeginningoftheencodedresult.*

我完全被卡住,试图做到这一点 - 我已经建立了网址为每这一部分:

VendorTxCode=TxCode-1310917599-223087284&Amount=36.95&Currency=GBP&Description=description&CustomerName=Fname Surname&[email protected]&BillingSurname=Surname&BillingFirstnames=Fname&BillingAddress1=BillAddress Line 1&BillingCity=BillCity&BillingPostCode=W1A 1BL&BillingCountry=GB&BillingPhone=447933000000&DeliveryFirstnames=Fname&DeliverySurname=Surname&DeliveryAddress1=BillAddress Line 1&DeliveryCity=BillCity&DeliveryPostCode=W1A 1BL&DeliveryCountry=GB&DeliveryPhone=447933000000&SuccessURL=https://example.com/success&FailureURL=https://example.com/failur e 

但现在需要使用带有PKCS#5填充的AES MODE_CBC对其进行加密。

PHP中的示例代码:

static public function encryptAes($string, $key) 
{ 
    // AES encryption, CBC blocking with PKCS5 padding then HEX encoding. 
    // Add PKCS5 padding to the text to be encypted. 
    $string = self::addPKCS5Padding($string); 

    // Perform encryption with PHP's MCRYPT module. 
    $crypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string, MCRYPT_MODE_CBC, $key); 

    // Perform hex encoding and return. 
    return "@" . strtoupper(bin2hex($crypt)); 
} 

我已经试过如下:

from Crypto.Cipher import AES 
mycrptobj = AES.new('55a51621a6648525', AES.MODE_CBC, os.urandom(16)) 
ciphertext = mycryptobj.encrypt('somewords') 

,但得到的输入字符串必须是16的长度的倍数。

所以。

我试过下面就这个其他堆栈溢出的问题,但:

  1. 我不知道IV是什么!我正确地生成一个随机ID吗?重新阅读显示使用的关键是初始化矢量,我认为是第四个是

  2. 这是正确的过程?

感谢

亚历

+0

IV应该与加密密码相同。 –

+0

@RikBlacow - 感谢您提供的信息,我昨晚得到了这个信息(终于!),但仍然在为这个实际的python挣扎(包括甚至可以使用的库)! –

回答