
本文旨在解决在使用Python的`Crypto`库进行AES加密时,由于Padding不正确导致的解密失败问题。通过引入自定义的Padding和Unpadding方法,并结合示例代码,详细展示了如何正确地加密和解密密码,并将其安全地存储在文本文件中。同时,也对代码结构和潜在的安全风险提出了改进建议,确保密码管理器的安全性和可靠性。
在使用 Crypto 库进行 AES 加密时,Padding 错误是一个常见的问题,尤其是在处理需要多次加密和解密,并将结果保存到文件中的场景下。 根本原因是AES算法要求加密的数据长度必须是块大小(通常是16字节)的倍数。当数据长度不满足此要求时,需要进行 Padding 填充。如果 Padding 的方式不正确,解密时就会出现 ValueError: Padding is incorrect. 错误。
Padding 是在数据末尾添加额外字节,使其长度达到块大小的倍数的过程。Unpadding 则是解密后,移除这些额外字节,恢复原始数据的过程。Crypto.Util.Padding 模块提供了一些 Padding 和 Unpadding 的方法,但有时使用自定义的方法可能更可靠。
以下代码展示了如何使用自定义的 Padding 和 Unpadding 方法来解决这个问题。此代码基于 Stack Overflow 上的一个答案,并进行了修改以适应密码管理器的需求。
import re
import random
import string
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import hashlib
def password_generator(size=10):
if size <= 8:
print("Size must be at least 4")
return None
password = []
while len(password) < size:
password.append(random.choice(string.ascii_lowercase)) # ensure at least one lowercase letter
if len(password) < size:
password.append(random.choice(string.ascii_uppercase)) # ensure at least one uppercase letter
if len(password) < size:
password.append(random.choice(string.digits)) # ensure at least one digit
if len(password) < size:
password.append(random.choice(string.punctuation)) # ensure at least one special character
random.shuffle(password) # shuffle to remove the predictability
return ''.join(password)
def password_checker(password):
if len(password) >= 8:
if bool(re.match(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z\d])', password)):
print("The password is strong")
else:
print("The password is weak")
else:
print("You have entered a short or invalid password.")
# Generate a password
generated_password = password_generator()
print(generated_password)
# Check the generated password
password_checker(generated_password)
class AESCipher(object):
def __init__(self, key):
self.bs = AES.block_size
self.key = hashlib.sha256(key).digest()
def encrypt_data(self, iv, raw):
raw = self._pad(raw)
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return cipher.iv, iv + cipher.encrypt(raw.encode())
def decrypt_data(self, iv, enc):
cipher = AES.new(self.key, AES.MODE_CBC, iv)
return AESCipher._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
def _pad(self, s):
return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
@staticmethod
def _unpad(s):
return s[:-ord(s[len(s)-1:])]
# Generate a random 128-bit IV for AES
iv = get_random_bytes(16)
# Generate a random 256-bit key for AES
key = get_random_bytes(32) # save this maybe?
Cipher = AESCipher(key)
# Encrypt the generated password
iv, encrypted_password = Cipher.encrypt_data(iv, generated_password)
print(f"Encrypted password: {encrypted_password}")
# Decrypt the encrypted password
decrypted_password = Cipher.decrypt_data(iv, encrypted_password)
print(f"Decrypted password: {decrypted_password}")
# Save the encrypted password to a file
with open(
'Password.txt',
'a', encoding='utf-8') as f:
f.write(f"{iv.hex()}:{encrypted_password.hex()}\n")
# Read the encrypted password from the file
with open(
'Password.txt',
'r', encoding='utf-8') as f:
for line in f.readlines():
line = line.strip() # Remove the trailing newline character
iv, encrypted_password = line.split(':')
decrypted_password = Cipher.decrypt_data(bytes.fromhex(iv), bytes.fromhex(encrypted_password))
print(f"Decrypted password from file: {decrypted_password}")代码解释:
通过使用自定义的 Padding 和 Unpadding 方法,可以有效地解决 Crypto 库中由于 Padding 不正确导致的解密失败问题。同时,注意密钥的安全存储和管理,以及代码的健壮性和安全性,可以构建一个安全可靠的密码管理器。
以上就是解决密码管理器中的Padding问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号