pycrypto加密/解密,解密时丢失部分加密字符串

2024-09-30 14:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试用pycrypto在python中加密/解密。大多数情况下一切都很顺利,但是我在解密数据时遇到了一个奇怪的问题。我试着对一些jpg进行加密/解密以进行测试,尽管它们加密/解密没有问题,但解密的文件无法打开/损坏。为了找到这个问题,我保存了一个文本文件,其中有一个类似于“testthisfileforintegrityblah-blah”的随机语句,它只在“。。。。诚信之前的一切都是乱七八糟的。我对AES不是很了解,但我认为这是一个编码/解码或填充错误。在

这是我的代码:

#encryption
iv = Random.new().read( AES.block_size)

filePath = input("Path to file for encryption: ")
selFile = open(filePath, 'rb')
getBytes = bytes(selFile.read())

encPW = input("Enter password: ")
hashpw = hashlib.sha256(encPW.encode('UTF-8').digest())

destination = input("Destination path for encrypted file: ")

aes = AES.new(hashpw, AES.Mode_CFB, iv)
encFile = base65.b64encode(aes.encrypt(getBytes))

writetofile = open(destination, 'wb')
writetofile.write(encFile)
writetofile.close()
print("Encryption successful")

#Decryption
iv = Random.new().read( AES.block_size)

filePath = input("Path to file for decryption: ")
selFile = open(filePath, 'rb')
getBytes = bytes(selFile.read())

decPW = input("Enter password: ")
hashdecpw = hashlib.sha256(encPW.encode('UTF-8').digest())

destination = input("Destination path for decrypted file: ")

aes = AES.new(hashdecpw, AES.Mode_CFB, iv)
decFile = aes.decrypt(getBytes)

writetofile = open(destination, 'wb')
writetofile.write(decFile)
writetofile.close()
print("Decryption successful")

有什么想法可以导致第一个字符丢失,并阻止我正确加密/解密文件吗?在


Tags: newforreadinputopendestinationfileaes
2条回答

您正在生成一个单独用于加密和解密的新IV,这就产生了这样的问题。我建议您这样做:

def encrypt(inpath, outpath, password):
    iv = Random.new().read(AES.block_size)
    with open(inpath, "rb") as f:
        contents = f.read()
    # A context manager automatically calls f.close()
    key = pbkdf2.crypt(password, "")
    # See notes

    aes = AES.new(key, AES.Mode_CFB, iv)
    encrypted = aes.encrypt(contents)
    with open(outpath, "wb") as f:
        f.write(iv + b":")
        f.write(encrypted)
    print("Encryption successful")


def decrypt(inpath, outpath, password):
    with open(inpath, "rb") as f:
        contents = f.read()

    iv, encrypted = contents.split(b":")
    key = pbkdf2.crypt(password, "")
    aes = AES.new(key, AES.Mode_CFB, iv)

    decrypted = aes.decrypt(contents)
    with open(outpath, "wb") as f:
        f.write(decrypted)
    print("Decryption successful")

注意事项:

  • 一个IV并不意味着是秘密的,所以它可以随机生成一次,然后写入一个文件,以便以后用于解密(如本例所示)

  • 哈希算法对于派生密钥来说不够强大,这就是为什么有称为密钥派生算法(如python中的PBKDF2)的特殊工具。用那些代替!

我自己没有测试过这段代码,所以它可能无法正常工作。

你至少有三个问题:

  • 你可能是说hashlib.sha256(encPW.encode('UTF-8')).digest()而不是hashlib.sha256(encPW.encode('UTF-8').digest())(右大括号在错误的位置)

  • 在将密文写入文件之前,先用Base64对密文进行编码。你忘记了在解密之前从文件中读回它后解码它。例如:

    getBytes = base64.b64decode(bytes(selFile.read()))
    
  • 这是一个大问题:在解密过程中,你需要与加密时使用的完全相同的IV。IV不是秘密的,但是它必须是唯一的,你用同一个密钥进行的每一次加密。通常情况下,IV写在密文前面,然后读回解密。在

    #encryption
    encFile = base64.b64encode(iv + aes.encrypt(getBytes))
    
    #decryption
    getBytes = base64.b64decode(bytes(selFile.read()))
    iv = getBytes[:16]
    aes = AES.new(hashdecpw, AES.Mode_CFB, iv)
    decFile = aes.decrypt(getBytes[16:])
    

相关问题 更多 >