本文整理汇总了Python中Cryptodome.Util.Padding.pad方法的典型用法代码示例。如果您正苦于以下问题:Python Padding.pad方法的具体用法?Python Padding.pad怎么用?Python Padding.pad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cryptodome.Util.Padding
的用法示例。
在下文中一共展示了Padding.pad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def encrypt(self, plaintext, esn):
"""
Encrypt the given Plaintext with the encryption key
:param plaintext:
:return: Serialized JSON String of the encryption Envelope
"""
init_vector = get_random_bytes(16)
cipher = AES.new(self.encryption_key, AES.MODE_CBC, init_vector)
ciphertext = base64.standard_b64encode(
cipher.encrypt(Padding.pad(plaintext.encode('utf-8'), 16))).decode('utf-8')
encryption_envelope = {
'ciphertext': ciphertext,
'keyid': '_'.join((esn, str(self.sequence_number))),
'sha256': 'AA==',
'iv': base64.standard_b64encode(init_vector).decode('utf-8')
}
return json.dumps(encryption_envelope)
示例2: encrypt_credential
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def encrypt_credential(raw):
"""
Encodes data
:param data: Data to be encoded
:type data: str
:returns: string -- Encoded data
"""
# pylint: disable=invalid-name,import-error
import base64
try: # The crypto package depends on the library installed (see Wiki)
from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Util import Padding
except ImportError:
from Cryptodome import Random
from Cryptodome.Cipher import AES
from Cryptodome.Util import Padding
raw = bytes(Padding.pad(data_to_pad=raw.encode('utf-8'), block_size=__BLOCK_SIZE__))
iv = Random.new().read(AES.block_size)
cipher = AES.new(get_crypt_key(), AES.MODE_CBC, iv)
return base64.b64encode(iv + cipher.encrypt(raw)).decode('utf-8')
示例3: encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def encrypt(self, data, esn, sequence_number):
"""
Encrypt the given Plaintext with the encryption key
:param plaintext:
:return: Serialized JSON String of the encryption Envelope
"""
iv = get_random_bytes(16)
encryption_envelope = {
'ciphertext': '',
'keyid': esn + '_' + str(sequence_number),
'sha256': 'AA==',
'iv': base64.standard_b64encode(iv).decode('ascii')
}
# Padd the plaintext
plaintext = Padding.pad(data.encode('utf-8'), 16)
# Encrypt the text
cipher = AES.new(self.encryption_key, AES.MODE_CBC, iv)
citext = cipher.encrypt(plaintext)
encryption_envelope['ciphertext'] = base64.standard_b64encode(citext).decode('ascii')
return encryption_envelope;
示例4: msl_encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def msl_encrypt(msl_session, plaintext):
"""
msl_encrypt()
@param msl_session: Dict of msl_session created by the client
upon initialization
@param plaintext: Plaintext to encrypt
@return: JSON byte string of encryption envelope
"""
cbc_iv = os.urandom(16)
encryption_envelope = {
'keyid': '%s_%s' % (
msl_session['esn'],
msl_session['session_keys']['sequence_number']
),
'sha256': 'AA==',
'iv': base64.b64encode(cbc_iv).decode('utf8')
}
plaintext = Padding.pad(plaintext.encode('utf8'), 16)
cipher = AES.new(
msl_session['session_keys']['encryption_key'],
AES.MODE_CBC,
cbc_iv
)
ciphertext = cipher.encrypt(plaintext)
encryption_envelope['ciphertext'] = base64.b64encode(
ciphertext
).decode('utf8')
return json.dumps(encryption_envelope).encode('utf8')
示例5: aes_cbc_encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def aes_cbc_encrypt(plain_text: bytes, key: bytes, iv: bytes = b''):
if len(iv) == 0:
iv = AESHandler.generate_iv()
cipher = AES.new(key=key, mode=AES.MODE_CBC, iv=iv)
return cipher.IV, cipher.encrypt(pad(plain_text, AES.block_size))
示例6: encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def encrypt(key, raw):
assert type(raw) == bytes, "input data is bytes"
if isinstance(key, str):
key = b64decode(key.encode())
raw = pad(raw, AES.block_size)
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
return iv + cipher.encrypt(raw)
示例7: encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def encrypt(values, key):
# add two random bytes in front of the body
data = "AA" + json.dumps(values)
data = pad(bytearray(data, "ascii"), 16, style="pkcs7")
iv = bytes(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
data_enc = cipher.encrypt(data)
return base64.b64encode(data_enc)
示例8: _encrypt_payload
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def _encrypt_payload(self, payload):
self._update_client_key()
aes = self._handle_AES(self.client_key)
paded_message = pad(bytes(payload.encode("utf8")), 16, style="pkcs7")
encoded_message = binascii.hexlify(aes.encrypt(paded_message)).decode("utf8").upper()
digest = self._create_digest(self.client_key, encoded_message)
return self.client_key + encoded_message + digest
示例9: _encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def _encrypt(self, values, key):
data = pad(bytearray(values, "ascii"), 16, style="pkcs7")
iv = bytes(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
data_enc = cipher.encrypt(data)
return data_enc
示例10: _padding_encrypt
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def _padding_encrypt(self, values, key):
# add two random bytes in front of the body
data = "AA" + json.dumps(values)
data = pad(bytearray(data, "ascii"), 16, style="pkcs7")
iv = bytes(16)
cipher = AES.new(key, AES.MODE_CBC, iv)
data_enc = cipher.encrypt(data)
return base64.b64encode(data_enc)
示例11: _encrypt_payload
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def _encrypt_payload(self, payload):
aes = self._handle_AES(self.encryption_key)
paded_message = pad(bytes(payload.encode("utf8")), 16, style="pkcs7")
encoded_message = binascii.hexlify(aes.encrypt(paded_message)).decode("utf8").upper()
digest = self._create_digest(self.encryption_key, encoded_message)
return self.encryption_key + encoded_message + digest
示例12: final
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def final(self,style='pkcs7'):
# TODO: after calling final, reset the IV? so the cipher is as good as new?
"""Finalizes the encryption by padding the cache
padfct = padding function
import from CryptoPlus.Util.padding
For ECB, CBC: the remaining bytes in the cache will be padded and
encrypted.
For OFB,CFB, CTR: an encrypted padding will be returned, making the
total outputed bytes since construction of the cipher
a multiple of the blocksize of that cipher.
If the cipher has been used for decryption, the final function won't do
anything. You have to manually unpad if necessary.
After finalization, the chain can still be used but the IV, counter etc
aren't reset but just continue as they were after the last step (finalization step).
"""
assert self.mode not in (MODE_XTS, MODE_CMAC) # finalizing (=padding) doesn't make sense when in XTS or CMAC mode
if self.ed == b'e':
# when the chain is in encryption mode, finalizing will pad the cache and encrypt this last block
if self.mode in (MODE_OFB,MODE_CFB,MODE_CTR):
dummy = b'0'*(self.chain.totalbytes%self.blocksize) # a dummy string that will be used to get a valid padding
else: #ECB, CBC
dummy = self.chain.cache
pdata = pad(dummy,self.blocksize,style=style)[len(dummy):]
#~ pad = padfct(dummy,padding.PAD,self.blocksize)[len(dummy):] # construct the padding necessary
return self.chain.update(pdata,b'e') # supply the padding to the update function => chain cache will be "cache+padding"
else:
# final function doesn't make sense when decrypting => padding should be removed manually
pass
示例13: _encode
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [as 别名]
def _encode(self, payload_data, con, path):
payload_data = CryptoPadding.pad(payload_data, 16)
cipher = self.get_cipher(
con.master_key,
con._.header.value.dynamic_header.encryption_iv.data
)
payload_data = cipher.encrypt(payload_data)
return payload_data
示例14: test1
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [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(""))
示例15: test2
# 需要导入模块: from Cryptodome.Util import Padding [as 别名]
# 或者: from Cryptodome.Util.Padding import pad [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")))