在PHP中解密使用MCRYPT_RIJNDAEL_256加密的Python中的字符串 [英] Decrypting strings in Python that were encrypted with MCRYPT_RIJNDAEL_256 in PHP

查看:121
本文介绍了在PHP中解密使用MCRYPT_RIJNDAEL_256加密的Python中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数加密($ text)
{
$ Key =MyKey;

return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$ Key,$ text,MCRYPT_MODE_ECB,mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_ECB),MCRYPT_RAND))))
}

如何在Python中解密这些值?

解决方案

要解密此形式的加密,您需要获取Rijndael的版本。可以在这里找到。那么您将需要模拟PHP Mcrypt模块中使用的键和文本填充。他们添加'\0'以将文本和键填充到正确的大小。他们正在使用256位的块大小,并且您给出的密钥使用的密钥大小是128(如果给它一个更大的密钥,它可能会增加)。不幸的是,我链接的Python实现只能一次编码一个块。我创建了Python模拟加密(用于测试)和解密的python函数

  import rijndael 
import base64

KEY_SIZE = 16
BLOCK_SIZE = 32

def加密(密钥,明文):
padded_key = key.ljust(KEY_SIZE,'\0 ')
padded_text = plaintext +(BLOCK_SIZE - len(plaintext)%BLOCK_SIZE)*'\0'

#也可以是
之一#if len(plaintext) %BLOCK_SIZE!= 0:
#padded_text = plaintext.ljust((len(plaintext)/ BLOCK_SIZE)+ 1 * BLOCKSIZE),'\0')
#-OR-
# padded_text = plaintext.ljust((len(plaintext)+(BLOCK_SIZE - len(plaintext)%BLOCK_SIZE)),'\0')

r = rijndael.rijndael(padded_key,BLOCK_SIZE)

ciphertext =''
用于范围开始(0,len(padded_text),BLOCK_SIZE):
ciphertext + = r.encrypt(padded_text [start:start + BLOCK_SIZE])

encoded = base64.b64encode(密文)

返回编码


定义解密(密钥,编码):
padded_key = key.ljust(KEY_SIZE,'\0')

ciphertext = base64.b64decode(编码)

r = rijndael.rijndael(padded_key,BLOCK_SIZE)

padded_text =''
开始范围(0,len(密文),BLOCK_SIZE):
padded_text + = r.decrypt(ciphertext [start:start + BLOCK_SIZE])

plaintext = padded_text.split \\ x00',1)[0]

返回明文

使用如下:

  key ='MyKey'
text ='test'

编码=加密(键,文本)
打印代码(编码)
#打印'I + KlvwIK2e690lPLDQMMUf5kfZmdZRIexYJp1SLWRJY ='

已解码=解密(密钥,编码)
打印repr(解码)
#打印'测试'

为了比较,这里是输出来自PHP与相同的文本:

  $ php -a 
交互式shell

php> $ key ='MyKey';
php> $ text ='test';
php> $ output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$ key,$ text,MCRYPT_MODE_ECB);
php> $ encoded = base64_encode($ output);
php> echo $ encoded;
I + KlvwIK2e690lPLDQMMUf5kfZmdZRIexYJp1SLWRJY =


I have a function in PHP that encrypts text as follows:

function encrypt($text)
{
    $Key = "MyKey";

    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $Key, $text, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}

How do I decrypt these values in Python?

解决方案

To decrypt this form of encryption, you will need to get a version of Rijndael. One can be found here. Then you will need to simulate the key and text padding used in the PHP Mcrypt module. They add '\0' to pad out the text and key to the correct size. They are using a 256 bit block size and the key size used with the key you give is 128 (it may increase if you give it a bigger key). Unfortunately, the Python implementation I've linked to only encodes a single block at a time. I've created python functions which simulate the encryption (for testing) and decryption in Python

import rijndael
import base64

KEY_SIZE = 16
BLOCK_SIZE = 32

def encrypt(key, plaintext):
    padded_key = key.ljust(KEY_SIZE, '\0')
    padded_text = plaintext + (BLOCK_SIZE - len(plaintext) % BLOCK_SIZE) * '\0'

    # could also be one of
    #if len(plaintext) % BLOCK_SIZE != 0:
    #    padded_text = plaintext.ljust((len(plaintext) / BLOCK_SIZE) + 1 * BLOCKSIZE), '\0')
    # -OR-
    #padded_text = plaintext.ljust((len(plaintext) + (BLOCK_SIZE - len(plaintext) % BLOCK_SIZE)), '\0')

    r = rijndael.rijndael(padded_key, BLOCK_SIZE)

    ciphertext = ''
    for start in range(0, len(padded_text), BLOCK_SIZE):
        ciphertext += r.encrypt(padded_text[start:start+BLOCK_SIZE])

    encoded = base64.b64encode(ciphertext)

    return encoded


def decrypt(key, encoded):
    padded_key = key.ljust(KEY_SIZE, '\0')

    ciphertext = base64.b64decode(encoded)

    r = rijndael.rijndael(padded_key, BLOCK_SIZE)

    padded_text = ''
    for start in range(0, len(ciphertext), BLOCK_SIZE):
        padded_text += r.decrypt(ciphertext[start:start+BLOCK_SIZE])

    plaintext = padded_text.split('\x00', 1)[0]

    return plaintext

This can be used as follows:

key = 'MyKey'
text = 'test'

encoded = encrypt(key, text)
print repr(encoded)
# prints 'I+KlvwIK2e690lPLDQMMUf5kfZmdZRIexYJp1SLWRJY='

decoded = decrypt(key, encoded)
print repr(decoded)
# prints 'test'

For comparison, here is the output from PHP with the same text:

$ php -a
Interactive shell

php > $key = 'MyKey';
php > $text = 'test';
php > $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB);
php > $encoded = base64_encode($output);
php > echo $encoded;
I+KlvwIK2e690lPLDQMMUf5kfZmdZRIexYJp1SLWRJY=

这篇关于在PHP中解密使用MCRYPT_RIJNDAEL_256加密的Python中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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