栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

解密Python中用MCRYPT_RIJNDAEL_256加密的字符串

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解密Python中用MCRYPT_RIJNDAEL_256加密的字符串

要解密这种加密形式,您将需要获得Rijndael版本。在这里可以找到一个。然后,您将需要模拟PHP
Mcrypt模块中使用的键和文本填充。它们增加

''
了填充文本和键的正确大小。他们使用的是256位块大小,并且您提供的密钥使用的密钥大小为128(如果您提供更大的密钥,则密钥大小可能会增加)。不幸的是,我链接到的Python实现一次只能编码一个块。我创建了python函数,用于模拟Python中的加密(用于测试)和解密

import rijndaelimport base64KEY_SIZE = 16BLOCK_SIZE = 32def encrypt(key, plaintext):    padded_key = key.ljust(KEY_SIZE, '')    padded_text = plaintext + (BLOCK_SIZE - len(plaintext) % BLOCK_SIZE) * ''    # could also be one of    #if len(plaintext) % BLOCK_SIZE != 0:    #    padded_text = plaintext.ljust((len(plaintext) / BLOCK_SIZE) + 1 * BLOCKSIZE), '')    # -OR-    #padded_text = plaintext.ljust((len(plaintext) + (BLOCK_SIZE - len(plaintext) % BLOCK_SIZE)), '')    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])    enpred = base64.b64enpre(ciphertext)    return enpreddef decrypt(key, enpred):    padded_key = key.ljust(KEY_SIZE, '')    ciphertext = base64.b64depre(enpred)    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

可以如下使用:

key = 'MyKey'text = 'test'enpred = encrypt(key, text)print repr(enpred)# prints 'I+KlvwIK2e690lPLDQMMUf5kfZmdZRIexYJp1SLWRJY='depred = decrypt(key, enpred)print repr(depred)# prints 'test'

为了进行比较,这是PHP的输出,带有相同的文本:

$ php -aInteractive shellphp > $key = 'MyKey';php > $text = 'test';php > $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB);php > $enpred = base64_enpre($output);php > echo $enpred;I+KlvwIK2e690lPLDQMMUf5kfZmdZRIexYJp1SLWRJY=


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617719.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号