當前位置: 首頁>>代碼示例>>Python>>正文


Python Random.get_random_bytes方法代碼示例

本文整理匯總了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) 
開發者ID:CastagnaIT,項目名稱:plugin.video.netflix,代碼行數:19,代碼來源:default_crypto.py

示例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() 
開發者ID:Marten4n6,項目名稱:EvilOSX,代碼行數:23,代碼來源:model.py

示例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 
開發者ID:mlperf,項目名稱:training,代碼行數:24,代碼來源:crypto.py

示例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") 
開發者ID:haynieresearch,項目名稱:jarvis,代碼行數:21,代碼來源:BLAKE2b.py

示例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") 
開發者ID:haynieresearch,項目名稱:jarvis,代碼行數:21,代碼來源:HMAC.py

示例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) 
開發者ID:haynieresearch,項目名稱:jarvis,代碼行數:23,代碼來源:ECC.py

示例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) 
開發者ID:haynieresearch,項目名稱:jarvis,代碼行數:26,代碼來源:Salsa20.py

示例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; 
開發者ID:asciidisco,項目名稱:plugin.video.netflix,代碼行數:23,代碼來源:MSLCrypto.py

示例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) 
開發者ID:ontio,項目名稱:ontology-python-sdk,代碼行數:10,代碼來源:utils.py

示例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] 
開發者ID:ontio,項目名稱:ontology-python-sdk,代碼行數:9,代碼來源:utils.py

示例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 
開發者ID:ontio,項目名稱:ontology-python-sdk,代碼行數:5,代碼來源:aes_handler.py

示例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] 
開發者ID:Azure,項目名稱:aztk,代碼行數:16,代碼來源:secure_utils.py

示例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) 
開發者ID:keylime,項目名稱:keylime,代碼行數:4,代碼來源:cryptodome.py

示例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) 
開發者ID:keylime,項目名稱:keylime,代碼行數:11,代碼來源:cryptodome.py

示例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)) 
開發者ID:r4sas,項目名稱:PBinCLI,代碼行數:16,代碼來源:format.py


注:本文中的Cryptodome.Random.get_random_bytes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。