python 3 pycrypto iv 必须是 16 字节长 [英] python 3 pycrypto iv must be 16 bytes long

查看:19
本文介绍了python 3 pycrypto iv 必须是 16 字节长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在尝试基于 github pycrypto 指南构建一个 AES 加密程序链接到 github 但是当我去解码时出现错误:

回溯(最近一次调用最后一次): 中的文件/home/pi/Desktop/aes/newAES.py",第 24 行打印(解密(密钥,味精,iv))文件/home/pi/Desktop/aes/newAES.py",第 13 行,解密密码 = AES.new(key,AES.MODE_CFB)新文件/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py",第 94 行返回 AESipher(key, *args, **kwargs)文件/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py",第 59 行,在 __init__blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)文件/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py",第 141 行,在 __init__ 中self._cipher = factory.new(key, *args, **kwargs)ValueError: IV 必须是 16 字节长

我的代码是:

from Crypto.Cipher import AES从 Crypto.Random 导入 get_random_bytesdef 加密(密钥,味精):如果键 == 0:键=get_random_bytes(16)打印(键:"+键)iv = get_random_bytes(16)打印('iv:'+str(iv))密码 = AES.new(key,AES.MODE_CFB,iv)密文= cipher.decrypt(msg)return("你的加密信息:"+str(密文))def解密(密钥,密文,iv):密码 = AES.new(key,AES.MODE_CFB)msg = cipher.decrypt(密文)ed = input('(e)ncrypt 或 (d)ecrypt: ')如果 ed=='e':key = input('16 位键:')msg = input('消息:')打印(加密(密钥,味精))elif ed =='d':key = input('16 位键:')iv = 字节(输入('iv:'),'utf-8')msg = bytes(input('加密消息:'),'utf-8')打印(解密(密钥,味精,iv))

我将不胜感激任何有关解决此问题的帮助,希望这不是一些愚蠢的错误

解决方案

iv 的问题在于它由随机的字节组成,但它正在被读入您的程序作为字符串.对该字符串调用 bytes 并没有达到您的预期.

<预><代码>>>>iv = b'\xba\x0eyO8\x17\xcf\x97=\xf2&l34#('>>>siv = str(iv)>>>西弗"b'\\xba\\x0eyO8\\x17\\xcf\\x97=\\xf2&l34#('" # 注意 'b' 是字符串的一部分>>>biv = 字节(siv,'utf-8')>>>身临其境b"b'\\xba\\x0eyO8\\x17\\xcf\\x97=\\xf2&l34#('" # 现在有两个 'b's!

您可以使用ast.literal_eval:

<预><代码>>>>ast.literal_eval(siv)b'\xba\x0eyO8\x17\xcf\x97=\xf2&l34#('

这是您的代码的工作版本 - 我删除了复制/粘贴 iv 的需要,但关于输入字节的相同观察适用于密文.

导入 ast从 Crypto.Cipher 导入 AES从 Crypto.Random 导入 get_random_bytesdef 加密(密钥,味精):iv = get_random_bytes(16)密码 = AES.new(key, AES.MODE_CFB, iv)ciphertext = cipher.encrypt(msg) # 这里使用正确的方法返回 iv + 密文def解密(密钥,密文):iv = 密文[:16]密文=密文[16:]密码 = AES.new(key, AES.MODE_CFB, iv)msg = cipher.decrypt(密文)返回 msg.decode("utf-8")如果 __name__ == "__main__":ed = input("(e)ncrypt 或 (d)ecrypt:")如果 ed == "e":key = input("16 位键:")msg = input("消息:")print("加密信息:", encrypt(key, msg))elif ed == "d":key = input("16 位键:")smsg = input("加密信息:")msg = ast.literal_eval(smsg)打印(解密消息:",解密(密钥,味精))

正在运行的代码:

(e)ncrypt 或 (d)ecrypt: e16 位密钥:abcdabcdabcdabcd消息:垃圾邮件,垃圾邮件,垃圾邮件加密消息:b'\xa4?\xa9RI>\x1f\xb5*\xb2,NWN\x0c\xfd"yB|\x1f\x82\x96\xd5\xb4\xd4\x1d&\x8bM\xdb\x07'(e)ncrypt 或 (d)ecrypt: d16 位密钥:abcdabcdabcdabcd加密信息:b'\xa4?\xa9RI>\x1f\xb5*\xb2,NWN\x0c\xfd"yB|\x1f\x82\x96\xd5\xb4\xd4\x1d&\x8bM\xdb\x07'解密邮件:垃圾邮件,垃圾邮件,垃圾邮件

so I have been trying to build an AES encryption program based off of the github pycrypto guide link to github however when I go to decode an error shows up:

Traceback (most recent call last):
File "/home/pi/Desktop/aes/newAES.py", line 24, in <module>
    print(decrypt(key,msg,iv))
File "/home/pi/Desktop/aes/newAES.py", line 13, in decrypt
    cipher = AES.new(key,AES.MODE_CFB)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 94, in new
    return AESCipher(key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
    self._cipher = factory.new(key, *args, **kwargs)
ValueError: IV must be 16 bytes long

my code is:

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
def encrypt(key,msg):
    if key == 0:
        key=get_random_bytes(16)
        print("key: "+key)
    iv = get_random_bytes(16)
    print('iv: '+str(iv))
    cipher = AES.new(key,AES.MODE_CFB,iv)
    ciphertext= cipher.decrypt(msg)
    return("your encrypted message: "+str(ciphertext))
def decrypt(key,ciphertext,iv):
    cipher = AES.new(key,AES.MODE_CFB)
    msg = cipher.decrypt(ciphertext)
ed = input('(e)ncrypt or (d)ecrypt: ')
if ed=='e':
    key = input('16 digit key: ')
    msg = input('message: ')
    print(encrypt(key,msg))
elif ed =='d':
    key = input('16 digit key: ')
    iv = bytes(input('iv: '),'utf-8')
    msg = bytes(input('encrypted message:'),'utf-8')
    print(decrypt(key,msg,iv))

I would appreciate any help offered on solving this issue, hopefully it isn't some stupid error

解决方案

The problem with iv is that it consists of random bytes, but it is being read into your program as a string. Calling bytes on that string does not do what you expect.

>>> iv = b'\xba\x0eyO8\x17\xcf\x97=\xf2&l34#('
>>> siv = str(iv)
>>> siv
"b'\\xba\\x0eyO8\\x17\\xcf\\x97=\\xf2&l34#('"   # Note 'b' is part of the string
>>> biv = bytes(siv, 'utf-8')
>>> biv
b"b'\\xba\\x0eyO8\\x17\\xcf\\x97=\\xf2&l34#('"  # Now there are two 'b's!

You can resolve this by using ast.literal_eval:

>>> ast.literal_eval(siv)
b'\xba\x0eyO8\x17\xcf\x97=\xf2&l34#('

Here's a working version of your code - I removed the need to copy/paste iv, but the same observations about inputting bytes applies to the ciphertext.

import ast

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes


def encrypt(key, msg):
    iv = get_random_bytes(16)
    cipher = AES.new(key, AES.MODE_CFB, iv)
    ciphertext = cipher.encrypt(msg)    # Use the right method here
    return iv + ciphertext


def decrypt(key, ciphertext):
    iv = ciphertext[:16]
    ciphertext = ciphertext[16:]
    cipher = AES.new(key, AES.MODE_CFB, iv)
    msg = cipher.decrypt(ciphertext)
    return msg.decode("utf-8")


if __name__ == "__main__":
    ed = input("(e)ncrypt or (d)ecrypt: ")
    if ed == "e":
        key = input("16 digit key: ")
        msg = input("message: ")
        print("Encrypted message: ", encrypt(key, msg))
    elif ed == "d":
        key = input("16 digit key: ")
        smsg = input("encrypted message: ")
        msg = ast.literal_eval(smsg)
        print("Decrypted message: ", decrypt(key, msg))

The code in action:

(e)ncrypt or (d)ecrypt: e
16 digit key: abcdabcdabcdabcd
message: Spam, spam, spam
Encrypted message:  b'\xa4?\xa9RI>\x1f\xb5*\xb2,NWN\x0c\xfd"yB|\x1f\x82\x96\xd5\xb4\xd4\x1d&\x8bM\xdb\x07'

(e)ncrypt or (d)ecrypt: d
16 digit key: abcdabcdabcdabcd
encrypted message: b'\xa4?\xa9RI>\x1f\xb5*\xb2,NWN\x0c\xfd"yB|\x1f\x82\x96\xd5\xb4\xd4\x1d&\x8bM\xdb\x07'
Decrypted message:  Spam, spam, spam

这篇关于python 3 pycrypto iv 必须是 16 字节长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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