Python 等价于 PHP Mcrypt

Posted

技术标签:

【中文标题】Python 等价于 PHP Mcrypt【英文标题】:Python equivalent of PHP Mcrypt 【发布时间】:2013-04-20 14:11:15 【问题描述】:

我用 php 编写了以下 DES 加密方案。它使用静态初始化向量来确保输出和输入是一对一映射的

PHP 代码:

function encrypt($plaintext, $key)

    # use an explicit encoding for the plain text
    $plaintext_utf8 = utf8_encode($plaintext);

    # create a random IV to use with CBC encoding
    # $iv_size = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC);
    # $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    # defining a constant IV to use with CBC encoding
    $iv = "kritanj ";

    # creates the DES cipher text 
    $ciphertext = mcrypt_encrypt(MCRYPT_DES, $key, $plaintext_utf8, MCRYPT_MODE_CBC, $iv);
    # prepend the IV for it to be available for decryption
    $ciphertext = $iv . $ciphertext;   

    # encode the resulting cipher text so it can be represented by a string
    $ciphertext_base64 = base64_encode($ciphertext); 

    return $ciphertext_base64;


function decrypt($ciphertext_base64, $key)
   
    $ciphertext_dec = base64_decode($ciphertext_base64);

    # retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
    # $iv_size = mcrypt_get_iv_size(MCRYPT_DES, MCRYPT_MODE_CBC);
    # $iv = substr($ciphertext_dec, 0, $iv_size);
    $iv = "kritanj ";

    # retrieves the cipher text (everything except the $iv_size in the front)
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);

    # decrypting the DES cipher text 
    $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_DES, $key, $ciphertext_dec, MCRYPT_MODE_CBC, $iv);
    return $plaintext_utf8_dec;


$plaintext = "The secret message is : " ;
$key = "7chrkey" ;
$ciphertext = encrypt($plaintext, $key);
echo "Encrypted: ".$ciphertext ."<br>";
$plaintext1 = decrypt($ciphertext, $key);
echo "Decrypted: ".$plaintext1  ;

输出:

Encrypted: a3JpdGFuaiB3DY63WHnE9led43FyFe53HlhUEr+vVJg=
Decrypted: The secret message is :

现在我尝试在 python 中编写等效代码 这是我管理的

Python 代码:

import binascii
def conv_sevnCharKey_to_64bit_DESkey(key):
  st = bin(int(binascii.hexlify(key), 16))
  st = st[2:]
  des_key = ''
  for i in xrange(8) :
    parity = 0
    sevnBits = st[i*7:i*7+7]
    for c in sevnBits :
      if c in '1' :
        parity += 1
    if parity % 2 == 0 :
      eigthBytes = sevnBits + '1'
    else :
      eigthBytes = sevnBits + '0'
  des_key += eigthBytes

  n = int('0b'+des_key, 2)
  DESkey =  binascii.unhexlify('%x' % n)

  return DESkey

from pyDes import *
plaintext = "The secret message is : "
iv = 'kritanj '
key = conv_sevnCharKey_to_64bit_DESkey('7chrkey')

plaintext_utf8 = plaintext.encode('utf-8')
# iniltalizing DES in cbc mode     
k = des(key, CBC, iv)
# encrypting
ciphertext = k.encrypt(plaintext_utf8)
# prepending the IV
ciphertext = iv + ciphertext;
# encoding to base64 
ciphertext_base64 = ciphertext.encode('base64','strict')

print "Encrypted: ", ciphertext_base64

# decoding base64 
ciphertext= ciphertext_base64.decode('base64','strict')
# striping the IV and decrypting
plaintext_utf8 = k.decrypt(ciphertext[8:])
# decoding utf-8 (if nessacary)
plaintext1 = plaint`ext_utf8.decode('utf-8') 

print "Decrypted: ", plaintext1

assert plaintext1== plaintext

输出:

Encrypted:  a3JpdGFuaiD+sGHb2GfZSXDac1r6mH+JDx7535yxL9k=

Decrypted:  The secret message is : 

为什么密文不同?有没有办法确保它们是相同的?

[我想对使用上述 php 代码在双重 DES 加密下加密的文本进行“中间相遇”攻击。 但由于我无法重新创建加密,我无法实施攻击]

【问题讨论】:

【参考方案1】:

看起来您填充键的方案与 PHP 代码不同:

from pyDes import *
plaintext = "The secret message is : "
iv = 'kritanj '
key = '7chrkey'
plaintext = plaintext.encode("utf-8")
iv = 'kritanj '
k = des(key+'\0', CBC, iv)
print (iv + k.encrypt(plaintext)).encode("base64", "strict")

输出:

'a3JpdGFuaiB3DY63WHnE9led43FyFe53HlhUEr+vVJg=\n'

简而言之:用 NULL 字节填充加密密钥将产生与您的 PHP 代码相同的结果。

【讨论】:

以上是关于Python 等价于 PHP Mcrypt的主要内容,如果未能解决你的问题,请参考以下文章

mcrypt“ncfb”模式的Java等价物是啥?

PHP 关联数组等价于 Python dict.get()

Python 等价于 phpinfo()

javascript 等价于 php unpack() 函数

PHP 等价于 Ruby 符号

Perl 的等价于 PHP 的 print_r() 是啥?