如何使用已提供的密钥和iv解密AES加密数据

时间:2017-07-28 10:30:59

标签: python python-2.7 python-3.x aes pycrypto

我正在尝试解密AES加密数据。 我正在使用密钥和iv,但我认为密钥也是编码的。

以下是我使用的代码:

import binascii
from Crypto.Cipher import AES
enckey = '5f35604280b44dd1073f7ee83e346d81'
key = binascii.unhexlify(enckey)
key32 = "{: <32}".format(key).encode("utf-8")
data='692fa1deafad8ad80b98cd6f077899e9be457ac5364c3822aae9457d4912e4829d71cb
8702bd10e1d54f7a0461edba193517b353835480bd174804f586776e623473022548ff098a95
45b608282bf498a36968dd6b858ad631f6eaa79ea1a87c984f4a8da5a9d1cee1b11b32d46c0d
2a670d4e634ecc47c7105387a0a38853c91e10747170de69ebf6f0e1a99f0134ddb0af0cec2c
fc70f53c9eab7227460cf1153ef686a5dc5014bd286fb0efdec571327f5a4874bec5fd5c65f0
9f0ed10e906e4199dd8c3cb8340aca1904f486a70b02554581f0e723d22854188e933ed9fce6
0172099bc675b89eba39651bbc0658ae264213217f14ff4f0824494585d8856dfd44e4ce9505
e43762a9f1ea48f9c736603e83c3e10c5740cdf279dc3a914e19eee089160ffa91180d1b4299
38ab1b6a4272d1779f7702f760cbac3f35fc35c16fcf21c7e00183f306e7a18f71ffb3b62b91
250dca7dd627876a6cedbfe83f0f18abbbb7c7650566a7f761844243fe1271cef22b1026a3f1
d37b8e7bd7c068331897680ec101e269ce66c3f129de33d3277c2cc120feb88f77f1bb851d41
b83468128366b7ed92ae07f37675cee07355ebcdfcba90a690e3d4817cd18123a0c9de175ea6
c5049c51170ee73facd5148f6525024116991b0601598a3501e770493dab0653e146981e91d2
ea9c50fbd1e6b8bb38407655c518f30852ce43ed62d1c578e642c4fa92f00bbf102c3418ed52
ed23138c86d327bbc4718ec44440f289e3af6c096c7ad69af5d941768b0f4b2e3decfad5dcfe
6dc491ce4f2f9d86d226b87f19dfb56dc44f6d66820773e6fcfa4fcd7958da2d639037627057
99a414baf93081242c2b594981c93b892f4f28883203875a4010ace9a5eafea51ee406'
cipher_text = binascii.unhexlify(data)
# Decryption
decryption_suite = AES.new('key32', AES.MODE_CBC, 'heF9BATUfWuISyO8')
plain_text = decryption_suite.decrypt(cipher_text)
print plain_text

这是我收到的错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 4: 
ordinal not in range(128)

我无法弄清楚我的密钥和数据的问题...... 如果我必须对我的代码进行任何更改,请告诉我。

2 个答案:

答案 0 :(得分:2)

以下代码有效:

from Crypto.Cipher import AES

keyAscii = '5f35604280b44dd1073f7ee83e346d81'
keyBinary = bytes(keyAscii, 'ascii')
ciphertextHex='692fa1deafad8ad80b98cd6f077899e9be457ac5364c3822aae9457d4912e4829d71cb8702bd10e1d54f7a0461edba193517b353835480bd174804f586776e623473022548ff098a9545b608282bf498a36968dd6b858ad631f6eaa79ea1a87c984f4a8da5a9d1cee1b11b32d46c0d2a670d4e634ecc47c7105387a0a38853c91e10747170de69ebf6f0e1a99f0134ddb0af0cec2cfc70f53c9eab7227460cf1153ef686a5dc5014bd286fb0efdec571327f5a4874bec5fd5c65f09f0ed10e906e4199dd8c3cb8340aca1904f486a70b02554581f0e723d22854188e933ed9fce60172099bc675b89eba39651bbc0658ae264213217f14ff4f0824494585d8856dfd44e4ce9505e43762a9f1ea48f9c736603e83c3e10c5740cdf279dc3a914e19eee089160ffa91180d1b429938ab1b6a4272d1779f7702f760cbac3f35fc35c16fcf21c7e00183f306e7a18f71ffb3b62b91250dca7dd627876a6cedbfe83f0f18abbbb7c7650566a7f761844243fe1271cef22b1026a3f1d37b8e7bd7c068331897680ec101e269ce66c3f129de33d3277c2cc120feb88f77f1bb851d41b83468128366b7ed92ae07f37675cee07355ebcdfcba90a690e3d4817cd18123a0c9de175ea6c5049c51170ee73facd5148f6525024116991b0601598a3501e770493dab0653e146981e91d2ea9c50fbd1e6b8bb38407655c518f30852ce43ed62d1c578e642c4fa92f00bbf102c3418ed52ed23138c86d327bbc4718ec44440f289e3af6c096c7ad69af5d941768b0f4b2e3decfad5dcfe6dc491ce4f2f9d86d226b87f19dfb56dc44f6d66820773e6fcfa4fcd7958da2d63903762705799a414baf93081242c2b594981c93b892f4f28883203875a4010ace9a5eafea51ee406'
ciphertextBinary = bytes.fromhex(ciphertextHex)
ivAscii = 'heF9BATUfWuISyO8'
ivBinary = bytes(ivAscii, 'ascii')
# Decryption
decrypter = AES.new(keyBinary, AES.MODE_CBC, ivBinary)
plaintextBinary = decrypter.decrypt(ciphertextBinary)
plaintext = plaintextBinary.decode('utf-8')
print(plaintext)

和输出

connection_type=wifi&android_id=863e87fea9a09533&app_name=AstroNest&app_version=53&app_version_name=2.7.1&device_brand=motorola&device_cpu_type=armv7l&device_model=XT1562&google_aid=ab95a01a-242b-4ac2-ad12-b6189e983a56&google_ad_tracking_disabled=0&insdate=1494826343&installer=com.android.vending&language=en&mac_address=02%3A00%3A00%3A00%3A00%3A00&mat_id=45c0a743-a948-434b-a20d-fe66e870d285&os_version=6.0.1&screen_density=3.0&screen_layout_size=1920x1080&sdk_version=3.11.4&conversion_user_agent=Dalvik%2F2.1.0+%28Linux%3B+U%3B+Android+6.0.1%3B+XT1562+Build%2FMPDS24.107-70-1-5%29&currency_code=USD&revenue=0.0&system_date=1494826532

但它在许多方面的加密错误:

  • 该键看起来像32个字符的十六进制字符串(它将编码128位密钥),但实际上您需要将其视为ASCII编码32字节密钥。这是错误的,因为密钥应该是统一分布的二进制字符串
  • 同样,IV应该是一个统一的二进制文件,但它实际上是ASCII
  • IV是固定的,但是对于每条消息,IV的整个点是不同的(CBC模式是不可预测的随机)。
  • CBC模式容易受到填充oracle攻击,允许主动攻击者恢复明文,如果他们可以执行选择的密文攻击,他们会知道哪个密文成功解密。

您应该使用经过身份验证的加密,而不是使用唯一的IV。

答案 1 :(得分:0)

这个可以帮到你

import binascii
from Crypto.Cipher import AES
import re

enckey = '5f35604280b44dd1073f7ee83e346d81'
key32 = "{: <32}".format(enckey).encode("utf-8")
cipher = AES.new(key32, AES.MODE_ECB)
data='692fa1deafad8ad80b98cd6f077899e9be457ac5364c3822aae9457d4912e4829d71cb8702bd10e1d54f7a0461edba193517b353835480bd174804f586776e623473022548ff098a9545b608282bf498a36968dd6b858ad631f6eaa79ea1a87c984f4a8da5a9d1cee1b11b32d46c0d2a670d4e634ecc47c7105387a0a38853c91e10747170de69ebf6f0e1a99f0134ddb0af0cec2cfc70f53c9eab7227460cf1153ef686a5dc5014bd286fb0efdec571327f5a4874bec5fd5c65f09f0ed10e906e4199dd8c3cb8340aca1904f486a70b02554581f0e723d22854188e933ed9fce60172099bc675b89eba39651bbc0658ae264213217f14ff4f0824494585d8856dfd44e4ce9505e43762a9f1ea48f9c736603e83c3e10c5740cdf279dc3a914e19eee089160ffa91180d1b429938ab1b6a4272d1779f7702f760cbac3f35fc35c16fcf21c7e00183f306e7a18f71ffb3b62b91250dca7dd627876a6cedbfe83f0f18abbbb7c7650566a7f761844243fe1271cef22b1026a3f1d37b8e7bd7c068331897680ec101e269ce66c3f129de33d3277c2cc120feb88f77f1bb851d41b83468128366b7ed92ae07f37675cee07355ebcdfcba90a690e3d4817cd18123a0c9de175ea6c5049c51170ee73facd5148f6525024116991b0601598a3501e770493dab0653e146981e91d2ea9c50fbd1e6b8bb38407655c518f30852ce43ed62d1c578e642c4fa92f00bbf102c3418ed52ed23138c86d327bbc4718ec44440f289e3af6c096c7ad69af5d941768b0f4b2e3decfad5dcfe6dc491ce4f2f9d86d226b87f19dfb56dc44f6d66820773e6fcfa4fcd7958da2d63903762705799a414baf93081242c2b594981c93b892f4f28883203875a4010ace9a5eafea51ee406'
cipher_text = binascii.unhexlify(data)
# Decryption


plain_text = re.sub('\0*$','', cipher.decrypt( data[16:]))
print plain_text

或试试这个

import binascii
from Crypto.Cipher import AES


enckey = '5f35604280b44dd1073f7ee83e346d81'
key32 = "{: <32}".format(enckey).encode("utf-8")
cipher = AES.new(key32, AES.MODE_ECB)
data='692fa1deafad8ad80b98cd6f077899e9be457ac5364c3822aae9457d4912e4829d71cb8702bd10e1d54f7a0461edba193517b353835480bd174804f586776e623473022548ff098a9545b608282bf498a36968dd6b858ad631f6eaa79ea1a87c984f4a8da5a9d1cee1b11b32d46c0d2a670d4e634ecc47c7105387a0a38853c91e10747170de69ebf6f0e1a99f0134ddb0af0cec2cfc70f53c9eab7227460cf1153ef686a5dc5014bd286fb0efdec571327f5a4874bec5fd5c65f09f0ed10e906e4199dd8c3cb8340aca1904f486a70b02554581f0e723d22854188e933ed9fce60172099bc675b89eba39651bbc0658ae264213217f14ff4f0824494585d8856dfd44e4ce9505e43762a9f1ea48f9c736603e83c3e10c5740cdf279dc3a914e19eee089160ffa91180d1b429938ab1b6a4272d1779f7702f760cbac3f35fc35c16fcf21c7e00183f306e7a18f71ffb3b62b91250dca7dd627876a6cedbfe83f0f18abbbb7c7650566a7f761844243fe1271cef22b1026a3f1d37b8e7bd7c068331897680ec101e269ce66c3f129de33d3277c2cc120feb88f77f1bb851d41b83468128366b7ed92ae07f37675cee07355ebcdfcba90a690e3d4817cd18123a0c9de175ea6c5049c51170ee73facd5148f6525024116991b0601598a3501e770493dab0653e146981e91d2ea9c50fbd1e6b8bb38407655c518f30852ce43ed62d1c578e642c4fa92f00bbf102c3418ed52ed23138c86d327bbc4718ec44440f289e3af6c096c7ad69af5d941768b0f4b2e3decfad5dcfe6dc491ce4f2f9d86d226b87f19dfb56dc44f6d66820773e6fcfa4fcd7958da2d63903762705799a414baf93081242c2b594981c93b892f4f28883203875a4010ace9a5eafea51ee406'
cipher_text = binascii.unhexlify(data)
# Decryption


plain_text = cipher.decrypt(cipher_text)
print plain_text