本文整理汇总了Python中Crypto.Util.Padding.unpad方法的典型用法代码示例。如果您正苦于以下问题:Python Padding.unpad方法的具体用法?Python Padding.unpad怎么用?Python Padding.unpad使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.Padding
的用法示例。
在下文中一共展示了Padding.unpad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decrypt_credential
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.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: get_cookie_data
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def get_cookie_data(self, cookie):
try:
cookie = base64.b64decode(urllib.parse.unquote(cookie).split("--")[0])
encrypted_data, iv = map(base64.b64decode, cookie.split("--".encode()))
cipher = AES.new(self.secret, AES.MODE_CBC, iv)
pt = unpad(cipher.decrypt(encrypted_data), AES.block_size)
try:
return json.loads(pt.decode())
except:
return json.loads(pt)
except Exception as e:
return {"err": "Cookie decryption failed.", "code": str(e)}
示例3: decrypt
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def decrypt(cls, encrypted_text):
"""
DES 解密
:param encrypted_text: 待解密的字符串
:return: 解密后的字符串
"""
# "url safe"格式base64转bytes
base64_decrypted = base64.urlsafe_b64decode(encrypted_text)
# AES初始化
aes = AES.new(cls.secret_key, AES.MODE_ECB)
# AES解密
decrypted_text = aes.decrypt(base64_decrypted)
# 去掉填充
decrypted_text = unpad(decrypted_text, AES.block_size)
# bytes转字符串
decrypted_text = decrypted_text.decode(encoding='utf-8')
return decrypted_text
示例4: test1
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.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(""))
示例5: test2
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def test2(self):
padded = pad(uh(b("12345678")), 4)
self.failUnless(padded == uh(b("1234567804040404")))
back = unpad(padded, 4)
self.failUnless(back == uh(b("12345678")))
示例6: test3
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def test3(self):
padded = pad(uh(b("123456")), 4)
self.failUnless(padded == uh(b("12345601")))
back = unpad(padded, 4)
self.failUnless(back == uh(b("123456")))
示例7: test4
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def test4(self):
padded = pad(uh(b("1234567890")), 4)
self.failUnless(padded == uh(b("1234567890030303")))
back = unpad(padded, 4)
self.failUnless(back == uh(b("1234567890")))
示例8: testn3
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.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)
示例9: testn1
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def testn1(self):
self.assertRaises(ValueError, unpad, b("123456\x81"), 4, 'iso7816')
示例10: decrypt
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.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)
示例11: decrypt_data
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def decrypt_data(cls, data, key, iv):
rkey = "".join(reversed(key)).encode('utf8')
riv = "".join(reversed(iv)).encode('utf8')
fkey = SHA256.new(rkey).hexdigest()[:32].encode("utf8")
cipher = AES.new(fkey, AES.MODE_CBC, riv)
decrypted = cipher.decrypt(base64.b64decode(data))
if decrypted:
return unpad(decrypted, 16, 'pkcs7')
else:
return decrypted
示例12: decrypt_aes_cbc
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def decrypt_aes_cbc(self, encrypted, key, iv):
key = AESCipher.mk_bytes(key)
iv = AESCipher.mk_bytes(iv)
res = AES.new(key, AES.MODE_CBC, iv=iv).decrypt(encrypted)
res = Padding.unpad(res, AES.block_size)
return res
示例13: __DecryptContents
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def __DecryptContents( self, contents: bytes, contentKeyBase64: str ) -> bytes:
contentKey = base64.b64decode( contentKeyBase64 )
keyAes = AES.new( self.DeviceIdUserIdKey, AES.MODE_ECB )
decryptedContentKey = keyAes.decrypt( contentKey )
contentAes = AES.new( decryptedContentKey, AES.MODE_ECB )
decryptedContents = contentAes.decrypt( contents )
return Padding.unpad( decryptedContents, AES.block_size, "pkcs7" )
示例14: decrypt_AES256
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def decrypt_AES256(data: bytes, key: bytes):
# hmac should include IV
mac = data[-32:] # sha256 hmac at the end
iv = data[:16] # 16 Bytes for IV at the beginning
message = data[16:-32] # the rest is the message
h = HMAC.new(key=key, msg=iv + message, digestmod=SHA256)
h.verify(mac)
decryption_cipher = AES.new(key, AES.MODE_CBC, iv=iv)
decrypted_message = decryption_cipher.decrypt(message)
# print(decrypted_message)
# now to remove any padding that was added on to make it the right block size of 16
return unpad(decrypted_message, 16)
示例15: decrypt_string_003
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import unpad [as 别名]
def decrypt_string_003(self, string_to_decrypt, encryption_key,
auth_key, uuid):
components = string_to_decrypt.split(':')
if len(components) == 6:
version, auth_hash, local_uuid, IV, ciphertext, auth_params = components
else:
version, auth_hash, local_uuid, IV, ciphertext = components
if local_uuid != uuid:
print('Note UUID does not match.')
print('This could be caused by a conflicted copy of a note.')
print('Rename or delete the conflicted copy to fix.')
print('Could also be a sign of tampering. Exiting for security...')
logging.debug('UUID: {}, Local UUID: {}'.format(uuid, local_uuid))
sys.exit(1)
string_to_auth = ':'.join([version, uuid, IV, ciphertext])
local_auth_hash = hmac.new(
unhexlify(auth_key), string_to_auth.encode(), 'sha256').digest()
auth_hash = unhexlify(auth_hash)
if not hmac.compare_digest(local_auth_hash, auth_hash):
print('Auth hash does not match. This could indicate tampering or '
'that something is wrong with the server. Exiting.')
logging.debug('Auth Hash: {}, Local Auth Hash: {}'.format(auth_hash, local_auth_hash))
sys.exit(1)
cipher = AES.new(unhexlify(encryption_key), AES.MODE_CBC, unhexlify(IV))
result = cipher.decrypt(b64decode(ciphertext))
result = Padding.unpad(result, AES.block_size).decode()
return result