本文整理汇总了Python中Cryptodome.Util.Padding.unpad方法的典型用法代码示例。如果您正苦于以下问题:Python Padding.unpad方法的具体用法?Python Padding.unpad怎么用?Python Padding.unpad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cryptodome.Util.Padding
的用法示例。
在下文中一共展示了Padding.unpad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decrypt_credential
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def decrypt_credential(enc, secret=None):
"""
Decodes data
:param data: Data to be decoded
:type data: str
:returns: string -- Decoded data
"""
# pylint: disable=invalid-name,import-error
import base64
try: # The crypto package depends on the library installed (see Wiki)
from Crypto.Cipher import AES
from Crypto.Util import Padding
except ImportError:
from Cryptodome.Cipher import AES
from Cryptodome.Util import Padding
enc = base64.b64decode(enc)
iv = enc[:AES.block_size]
cipher = AES.new(secret or get_crypt_key(), AES.MODE_CBC, iv)
decoded = Padding.unpad(
padded_data=cipher.decrypt(enc[AES.block_size:]),
block_size=__BLOCK_SIZE__)
return decoded
示例2: test2
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def test2(self):
padded = pad(uh(b("12345678")), 4)
self.assertTrue(padded == uh(b("1234567804040404")))
back = unpad(padded, 4)
self.assertTrue(back == uh(b("12345678")))
示例3: aes_cbc_decrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def aes_cbc_decrypt(cipher_text: bytes, iv: bytes, key: bytes):
cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
return unpad(cipher.decrypt(cipher_text), AES.block_size, style='pkcs7')
示例4: decrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def decrypt(key, enc):
assert type(enc) == bytes, 'Encrypt data is bytes'
if isinstance(key, str):
key = b64decode(key.encode())
iv = enc[:AES.block_size]
cipher = AES.new(key, AES.MODE_CBC, iv)
raw = cipher.decrypt(enc[AES.block_size:])
raw = unpad(raw, AES.block_size)
if len(raw) == 0:
raise ValueError("AES decryption error, not correct key.")
return raw
示例5: decrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def decrypt(self, init_vector, ciphertext):
"""Decrypt a ciphertext"""
cipher = AES.new(self.encryption_key, AES.MODE_CBC, init_vector)
return Padding.unpad(cipher.decrypt(ciphertext), 16)
示例6: decrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def decrypt(data, key):
payload = base64.b64decode(data)
data = aes_decrypt(payload, key)
# response starts with 2 random bytes, exclude them
response = unpad(data, 16, style="pkcs7")[2:]
return response.decode("ascii")
示例7: _decrypt_payload
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def _decrypt_payload(self, encrypted_payload):
encoded_counter = encrypted_payload[0:8]
aes = self._handle_AES(encoded_counter)
encoded_message = encrypted_payload[8:-64].upper()
digest = encrypted_payload[-64:]
calculated_digest = self._create_digest(encoded_counter, encoded_message)
if digest != calculated_digest:
raise WrongDigestException
decoded_message = aes.decrypt(bytes.fromhex(encoded_message))
unpaded_message = unpad(decoded_message, 16, style="pkcs7")
return unpaded_message.decode("utf8")
示例8: _decrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def _decrypt(self, data, key):
payload = base64.b64decode(data)
data = self._aes_decrypt(payload, key)
# response starts with 2 random bytes, exclude them
response = unpad(data, 16, style="pkcs7")[2:]
return response.decode("ascii")
示例9: _decrypt_payload
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def _decrypt_payload(self, encrypted_payload):
self.encoded_counter = encrypted_payload[0:8]
aes = self._handle_AES(self.encoded_counter)
encoded_message = encrypted_payload[8:-64].upper()
digest = encrypted_payload[-64:]
calculated_digest = self._create_digest(self.encoded_counter, encoded_message)
if digest != calculated_digest:
raise Exception
decoded_message = aes.decrypt(bytes.fromhex(encoded_message))
unpaded_message = unpad(decoded_message, 16, style="pkcs7")
return unpaded_message.decode("utf8")
示例10: test1
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def test1(self):
padded = pad(b(""), 4)
self.assertTrue(padded == uh(b("04040404")))
padded = pad(b(""), 4, 'pkcs7')
self.assertTrue(padded == uh(b("04040404")))
back = unpad(padded, 4)
self.assertTrue(back == b(""))
示例11: test3
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def test3(self):
padded = pad(uh(b("123456")), 4)
self.assertTrue(padded == uh(b("12345601")))
back = unpad(padded, 4)
self.assertTrue(back == uh(b("123456")))
示例12: test4
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def test4(self):
padded = pad(uh(b("1234567890")), 4)
self.assertTrue(padded == uh(b("1234567890030303")))
back = unpad(padded, 4)
self.assertTrue(back == uh(b("1234567890")))
示例13: testn3
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def testn3(self):
self.assertRaises(ValueError, unpad, b("123456\x02"), 4)
self.assertRaises(ValueError, unpad, b("123456\x00"), 4)
self.assertRaises(ValueError, unpad, b("123456\x05\x05\x05\x05\x05"), 4)
示例14: testn1
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def testn1(self):
self.assertRaises(ValueError, unpad, b("123456\x81"), 4, 'iso7816')
示例15: test1
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import unpad [as 别名]
def test1(self):
padded = pad(b(""), 4)
self.failUnless(padded == uh(b("04040404")))
padded = pad(b(""), 4, 'pkcs7')
self.failUnless(padded == uh(b("04040404")))
back = unpad(padded, 4)
self.failUnless(back == b(""))