本文整理汇总了Python中Cryptodome.Random.get_random_bytes方法的典型用法代码示例。如果您正苦于以下问题:Python Random.get_random_bytes方法的具体用法?Python Random.get_random_bytes怎么用?Python Random.get_random_bytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cryptodome.Random
的用法示例。
在下文中一共展示了Random.get_random_bytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encrypt
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [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: _openssl_encrypt
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def _openssl_encrypt(password, plaintext):
"""
:type password: str
:type plaintext: str
:rtype: str
"""
# Thanks to Joe Linoff, taken from https://stackoverflow.com/a/42773185
salt = get_random_bytes(8)
key, iv = PayloadFactory._get_key_and_iv(password, salt)
# PKCS#7 padding
padding_len = 16 - (len(plaintext) % 16)
padded_plaintext = plaintext + (chr(padding_len) * padding_len)
# Encrypt
cipher = AES.new(key, AES.MODE_CBC, iv)
cipher_text = cipher.encrypt(padded_plaintext.encode())
# Make OpenSSL compatible
openssl_cipher_text = b"Salted__" + salt + cipher_text
return b64encode(openssl_cipher_text).decode()
示例3: encrypt_file
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def encrypt_file(public_key, src_file, dest_file):
try:
with open(src_file) as f:
rsa_key = RSA.import_key(open(public_key).read())
session_key = get_random_bytes(16)
# Encrypt session key
cipher_rsa = PKCS1_OAEP.new(rsa_key)
encrypted_session_key = cipher_rsa.encrypt(session_key)
# Encrypt data
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(f.read().encode("utf-8"))
except Exception as e:
print("Unable to encrypt file: {}".format(src_file))
raise e
try:
with open(dest_file, "wb") as f:
for x in (encrypted_session_key, cipher_aes.nonce, tag, ciphertext):
f.write(x)
except Exception as e:
print("Unable to write output file {}".format(dest_file))
raise e
示例4: verify
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def verify(self, mac_tag):
"""Verify that a given **binary** MAC (computed by another party)
is valid.
:Parameters:
mac_tag : byte string
The expected MAC of the message.
:Raises ValueError:
if the MAC does not match. It means that the message
has been tampered with or that the MAC key is incorrect.
"""
secret = get_random_bytes(16)
mac1 = new(digest_bits=160, key=secret, data=mac_tag)
mac2 = new(digest_bits=160, key=secret, data=self.digest())
if mac1.digest() != mac2.digest():
raise ValueError("MAC check failed")
示例5: verify
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def verify(self, mac_tag):
"""Verify that a given **binary** MAC (computed by another party)
is valid.
:Parameters:
mac_tag : byte string
The expected MAC of the message.
:Raises ValueError:
if the MAC does not match. It means that the message
has been tampered with or that the MAC key is incorrect.
"""
secret = get_random_bytes(16)
mac1 = BLAKE2s.new(digest_bits=160, key=secret, data=mac_tag)
mac2 = BLAKE2s.new(digest_bits=160, key=secret, data=self.digest())
if mac1.digest() != mac2.digest():
raise ValueError("MAC check failed")
示例6: generate
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def generate(**kwargs):
"""Generate a new private key on the given curve.
:Keywords:
curve : string
Mandatory. It must be "P-256", "prime256v1" or "secp256r1".
randfunc : callable
Optional. The RNG to read randomness from.
If ``None``, the system source is used.
"""
curve = kwargs.pop("curve")
randfunc = kwargs.pop("randfunc", get_random_bytes)
if kwargs:
raise TypeError("Unknown parameters: " + str(kwargs))
d = Integer.random_range(min_inclusive=1,
max_exclusive=_curve.order,
randfunc=randfunc)
return EccKey(curve=curve, d=d)
示例7: new
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def new(key, nonce=None):
"""Create a new Salsa20 cipher
:Parameters:
key : byte string
The secret key to use in the symmetric cipher.
It must be 16 or 32 bytes long.
nonce : byte string
A value that must never be reused for any other encryption.
It must be 8 bytes long.
If not provided, a random byte string will be generated (you can
read it back via the ``nonce`` attribute).
:Return: an `Salsa20Cipher` object
"""
if nonce is None:
nonce = get_random_bytes(8)
return Salsa20Cipher(key, nonce)
#: Size of a data block (in bytes)
示例8: encrypt
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [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;
示例9: get_random_bytes
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def get_random_bytes(length: int) -> bytes:
"""
This interface is used to get a random byte string of the desired length.
:param length: the desired length of a random byte string.
:return: a random byte string of the desired length.
"""
return Random.get_random_bytes(length)
示例10: get_random_hex_str
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def get_random_hex_str(length: int) -> str:
"""
:param length:
:return: a random hexadecimal string of the desired length.
"""
return Random.get_random_bytes(length).hex()[:length]
示例11: generate_key
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def generate_key():
key = Random.get_random_bytes(32)
return key
示例12: encrypt_password
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def encrypt_password(ssh_pub_key, password):
if not password:
return [None, None, None, None]
recipient_key = RSA.import_key(ssh_pub_key)
session_key = get_random_bytes(16)
# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
encrypted_aes_session_key = cipher_rsa.encrypt(session_key)
# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(password.encode())
return [encrypted_aes_session_key, cipher_aes.nonce, tag, ciphertext]
示例13: generate_random_key
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def generate_random_key(size=32):
return get_random_bytes(size)
示例14: encrypt
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def encrypt(plaintext, key):
#Deal with the case when field is empty
if plaintext is None:
plaintext = b''
nonce = get_random_bytes(AES.block_size)
cipher = AES.new(key, AES.MODE_GCM, nonce = nonce)
(cipher_text, digest) = cipher.encrypt_and_digest(_pad(plaintext))
return base64.b64encode(nonce + cipher_text + digest)
示例15: __init__
# 需要导入模块: from Cryptodome import Random [as 别名]
# 或者: from Cryptodome.Random import get_random_bytes [as 别名]
def __init__(self, debug=False):
self._version = 2
self._compression = 'zlib'
self._data = ''
self._text = ''
self._attachment = ''
self._attachment_name = ''
self._password = ''
self._debug = debug
self._iteration_count = CIPHER_ITERATION_COUNT
self._salt_bytes = CIPHER_SALT_BYTES
self._block_bits = CIPHER_BLOCK_BITS
self._tag_bits = CIPHER_TAG_BITS
self._key = get_random_bytes(int(self._block_bits / 8))