本文整理汇总了Python中Crypto.Util.Padding.pad方法的典型用法代码示例。如果您正苦于以下问题:Python Padding.pad方法的具体用法?Python Padding.pad怎么用?Python Padding.pad使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Util.Padding
的用法示例。
在下文中一共展示了Padding.pad方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encrypt
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.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 Crypto.Util import Padding [as 别名]
# 或者: from Crypto.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: auth_digital
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def auth_digital(self, title_id, title_version, device_token, ticket):
self.verify_ticket(ticket, title_id)
plain_key = get_random_bytes(16)
aes = AES.new(plain_key, AES.MODE_CBC, iv=bytes(16))
encrypted_ticket = aes.encrypt(pad(ticket, 16))
rsa_key = RSA.construct((RSA_MODULUS, RSA_EXPONENT))
rsa = PKCS1_OAEP.new(rsa_key, SHA256)
encrypted_key = rsa.encrypt(plain_key)
req = HTTPRequest.post("/v3/application_auth_token")
req.form["application_id"] = "%016x" %title_id
req.form["application_version"] = "%08x" %title_version
req.form["device_auth_token"] = device_token
req.form["media_type"] = "DIGITAL"
req.form["cert"] = b64encode(encrypted_ticket)
req.form["cert_key"] = b64encode(encrypted_key)
response = self.request(req, True)
return response.json
示例4: encrypt_item
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt_item(self, item, keys):
uuid = item['uuid']
content = json.dumps(item['content'])
logging.debug('Encrypting item {} with content: {}'.format(uuid, content))
# all this is to follow the Standard Notes spec
item_key = hex(random.getrandbits(AES_KEY_LEN * 2))
# remove '0x', pad with 0's, then split in half
item_key = item_key[2:].rjust(AES_STR_KEY_LEN * 2, '0')
item_ek = item_key[:AES_STR_KEY_LEN]
item_ak = item_key[AES_STR_KEY_LEN:]
enc_item = deepcopy(item)
enc_item['content'] = self.encrypt_string_003(
content, item_ek, item_ak, uuid)
enc_item['enc_item_key'] = self.encrypt_string_003(
item_key, keys['mk'], keys['ak'], uuid)
return enc_item
示例5: encrypt_string_003
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt_string_003(self, string_to_encrypt, encryption_key,
auth_key, uuid):
IV = hex(random.getrandbits(AES_IV_LEN))
IV = IV[2:].rjust(AES_STR_IV_LEN, '0') # remove '0x', pad with 0's
cipher = AES.new(unhexlify(encryption_key), AES.MODE_CBC, unhexlify(IV))
pt = string_to_encrypt.encode()
padded_pt = Padding.pad(pt, AES.block_size)
ciphertext = b64encode(cipher.encrypt(padded_pt)).decode()
string_to_auth = ':'.join(['003', uuid, IV, ciphertext])
auth_hash = hmac.new(
unhexlify(auth_key), string_to_auth.encode(), 'sha256').digest()
auth_hash = hexlify(auth_hash).decode()
result = ':'.join(['003', auth_hash, uuid, IV, ciphertext])
return result
示例6: encrypt
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt(self, command):
from Crypto.Util import Padding
if not self._encryption_ready:
return command
if self._salt != "" and self.new_salt_needed():
prev_salt = self._salt
self._salt = self.genarate_salt()
s = "nextSalt/{}/{}/{}\0".format(prev_salt, self._salt, command)
else:
if self._salt == "":
self._salt = self.genarate_salt()
s = "salt/{}/{}\0".format(self._salt, command)
s = Padding.pad(bytes(s, "utf-8"), 16)
aes_cipher = self.get_new_aes_chiper()
encrypted = aes_cipher.encrypt(s)
encoded = b64encode(encrypted)
encoded_url = req.pathname2url(encoded.decode("utf-8"))
return CMD_ENCRYPT_CMD + encoded_url
示例7: test1
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [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(""))
示例8: test2
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [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")))
示例9: test3
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [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")))
示例10: test4
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [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")))
示例11: encrypt_string
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt_string(self, string_to_encrypt: str) -> str:
# This is needed to remove the escaping added by Python. For example, if we find in smali the instruction
# const-string v0, "\"message\"" Android will treat it as "message" while in Python it's \"message\",
# so we need to encrypt "message" and not \"message\" (we have to remove the unnecessary escaping, otherwise
# the backslashes would by encrypted as part of the string).
string_to_encrypt = string_to_encrypt.encode(errors='replace').decode('unicode_escape')
key = PBKDF2(password=self.encryption_secret, salt=self.encryption_secret.encode(), dkLen=32, count=128)
encrypted_string = hexlify(AES.new(key=key, mode=AES.MODE_ECB)
.encrypt(pad(string_to_encrypt.encode(errors='replace'), AES.block_size))).decode()
return encrypted_string
示例12: encrypt_string
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt_string(self, string_to_encrypt: str) -> str:
# This is needed to remove the escaping added by Python. For example, if we find in string resources
# the string "\"message\"" Android will treat it as "message" while in Python it's \"message\", so we
# need to encrypt "message" and not \"message\" (we have to remove the unnecessary escaping, otherwise
# the backslashes would by encrypted as part of the string).
string_to_encrypt = string_to_encrypt.encode(errors='replace').decode('unicode_escape')
key = PBKDF2(password=self.encryption_secret, salt=self.encryption_secret.encode(), dkLen=32, count=128)
encrypted_string = hexlify(AES.new(key=key, mode=AES.MODE_ECB)
.encrypt(pad(string_to_encrypt.encode(errors='replace'), AES.block_size))).decode()
return encrypted_string
示例13: encrypt_data
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt_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)
encrypted = cipher.encrypt(pad(data, 16, 'pkcs7'))
return base64.b64encode(encrypted)
示例14: encrypt
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def encrypt(hstr, payload, encryptionKey):
paddedEncryptionKey = pad(encryptionKey, AES.block_size)
key = base64.b64encode(paddedEncryptionKey)
cipher = AES.new(key, AES.MODE_CTR)
hstrBytes = base64.b64encode(cipher.encrypt(hstr)).decode('utf-8')
payloadBytes = base64.b64encode(cipher.encrypt(payload)).decode('utf-8')
nonce = base64.b64encode(cipher.nonce).decode('utf-8')
return [hstrBytes, payloadBytes, nonce]
示例15: mk_pad_bytes
# 需要导入模块: from Crypto.Util import Padding [as 别名]
# 或者: from Crypto.Util.Padding import pad [as 别名]
def mk_pad_bytes(data_to_pad, length=AES.block_size):
"""字节串补齐"""
return Padding.pad(AESCipher.mk_bytes(data_to_pad), length)