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


Python modes.CFB屬性代碼示例

本文整理匯總了Python中cryptography.hazmat.primitives.ciphers.modes.CFB屬性的典型用法代碼示例。如果您正苦於以下問題:Python modes.CFB屬性的具體用法?Python modes.CFB怎麽用?Python modes.CFB使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在cryptography.hazmat.primitives.ciphers.modes的用法示例。


在下文中一共展示了modes.CFB屬性的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: aes_encrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def aes_encrypt(key, message):

    #key = sys.argv[1].encode()
    #plain = sys.argv[2].encode()
    iv = os.urandom(16)

    digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
    digest.update(key.encode())
    key_digest = digest.finalize()


    cipher = Cipher(algorithms.AES(key_digest), modes.CFB(iv), backend=default_backend())
    encryptor = cipher.encryptor()
    encrypted = encryptor.update(message.encode()) + encryptor.finalize()

    print(hexlify(iv).decode(), hexlify(encrypted).decode()) 
開發者ID:fportantier,項目名稱:vulpy,代碼行數:18,代碼來源:aes-encrypt.py

示例2: _encrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def _encrypt(pt, key, alg, iv=None):
    if iv is None:
        iv = b'\x00' * (alg.block_size // 8)

    if alg.is_insecure:
        raise PGPInsecureCipher("{:s} is not secure. Do not use it for encryption!".format(alg.name))

    if not alg.is_supported:
        raise PGPEncryptionError("Cipher {:s} not supported".format(alg.name))

    try:
        encryptor = Cipher(alg.cipher(key), modes.CFB(iv), default_backend()).encryptor()

    except UnsupportedAlgorithm as ex:  # pragma: no cover
        six.raise_from(PGPEncryptionError, ex)

    else:
        return bytearray(encryptor.update(pt) + encryptor.finalize()) 
開發者ID:SecurityInnovation,項目名稱:PGPy,代碼行數:20,代碼來源:symenc.py

示例3: _decrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def _decrypt(ct, key, alg, iv=None):
    if iv is None:
        """
        Instead of using an IV, OpenPGP prefixes a string of length
        equal to the block size of the cipher plus two to the data before it
        is encrypted. The first block-size octets (for example, 8 octets for
        a 64-bit block length) are random, and the following two octets are
        copies of the last two octets of the IV.
        """
        iv = b'\x00' * (alg.block_size // 8)

    try:
        decryptor = Cipher(alg.cipher(key), modes.CFB(iv), default_backend()).decryptor()

    except UnsupportedAlgorithm as ex:  # pragma: no cover
        six.raise_from(PGPDecryptionError, ex)

    else:
        return bytearray(decryptor.update(ct) + decryptor.finalize()) 
開發者ID:SecurityInnovation,項目名稱:PGPy,代碼行數:21,代碼來源:symenc.py

示例4: aes_decrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def aes_decrypt(key, iv, message):

    digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
    digest.update(key.encode())
    key_digest = digest.finalize()

    cipher = Cipher(algorithms.AES(key_digest), modes.CFB(unhexlify(iv)), backend=default_backend())
    decryptor = cipher.decryptor()
    plain = decryptor.update(unhexlify(message)) + decryptor.finalize()

    print(plain.decode(errors='ignore')) 
開發者ID:fportantier,項目名稱:vulpy,代碼行數:13,代碼來源:aes-decrypt.py

示例5: encrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def encrypt(key, iv, byte_data):
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
    encryptor = cipher.encryptor()

    return encryptor.update(byte_data) + encryptor.finalize() 
開發者ID:fdslight,項目名稱:fdslight,代碼行數:7,代碼來源:_aes_cfb.py

示例6: decrypt

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def decrypt(key, iv, byte_data):
    cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())
    decryptor = cipher.decryptor()

    return decryptor.update(byte_data) + decryptor.finalize() 
開發者ID:fdslight,項目名稱:fdslight,代碼行數:7,代碼來源:_aes_cfb.py

示例7: __init__

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def __init__(
      self, algorithm=None, cipher_mode=None, initialization_vector=None,
      **kwargs):
    """Initializes a decrypter.

    Args:
      algorithm (Optional[Cryptography.CipherAlgorithm]): cipher algorithm.
      cipher_mode (Optional[str]): cipher mode.
      initialization_vector (Optional[bytes]): initialization vector.
      kwargs (dict): keyword arguments depending on the decrypter.

    Raises:
      ValueError: if the initialization_vector is required and not set.
    """
    if (cipher_mode != definitions.ENCRYPTION_MODE_ECB and
        not initialization_vector):
      raise ValueError('Missing initialization vector.')

    if cipher_mode == definitions.ENCRYPTION_MODE_CBC:
      mode = modes.CBC(initialization_vector)
    elif cipher_mode == definitions.ENCRYPTION_MODE_CFB:
      mode = modes.CFB(initialization_vector)
    elif cipher_mode == definitions.ENCRYPTION_MODE_ECB:
      mode = modes.ECB()
    elif cipher_mode == definitions.ENCRYPTION_MODE_OFB:
      mode = modes.OFB(initialization_vector)

    backend = backends.default_backend()
    cipher = ciphers.Cipher(algorithm, mode=mode, backend=backend)

    super(CryptographyBlockCipherDecrypter, self).__init__()
    self._algorithm = algorithm
    self._cipher_context = cipher.decryptor() 
開發者ID:log2timeline,項目名稱:dfvfs,代碼行數:35,代碼來源:decrypter.py

示例8: get_encryptor

# 需要導入模塊: from cryptography.hazmat.primitives.ciphers import modes [as 別名]
# 或者: from cryptography.hazmat.primitives.ciphers.modes import CFB [as 別名]
def get_encryptor(algoname, key, iv=None, mode='ecb', padd='pkcs7'):
    backend  = default_backend()
    if algoname == 'ChaCha20':
        algoer = algorithms.ChaCha20(key, iv)
        cipher = Cipher(algoer, None, backend=backend)
    elif algoname == 'ARC4':
        algoer = algorithms.ARC4(key)
        cipher = Cipher(algoer, None, backend=backend)
    else:
        algoer = algos[algoname](key)
        if mode == 'ecb': mode = modes.ECB()
        if mode == 'cfb': mode = modes.CFB(iv)
        if mode == 'ofb': mode = modes.OFB(iv)
        if mode == 'cbc': mode = modes.CBC(iv)
        if mode == 'ctr': mode = modes.CTR(iv)
        cipher = Cipher(algoer, mode, backend=backend)

    def enc(bitstring):
        if algoname not in ['ARC4', 'ChaCha20']:
            if padd.upper() == 'PKCS7':
                padder    = padding.PKCS7(algoer.block_size).padder()
                bitstring = padder.update(bitstring) + padder.finalize()
            elif padd.upper() == 'ANSIX923':
                padder    = padding.ANSIX923(algoer.block_size).padder()
                bitstring = padder.update(bitstring) + padder.finalize()
        encryptor = cipher.encryptor()
        return encryptor.update(bitstring) + encryptor.finalize()

    def dec(bitstring):
        decryptor = cipher.decryptor()
        ddata = decryptor.update(bitstring) + decryptor.finalize()
        if algoname not in ['ARC4', 'ChaCha20']:
            if padd.upper() == 'PKCS7':
                unpadder  = padding.PKCS7(algoer.block_size).unpadder()
                ddata = unpadder.update(ddata) + unpadder.finalize()
            elif padd.upper() == 'ANSIX923':
                unpadder  = padding.ANSIX923(algoer.block_size).unpadder()
                ddata = unpadder.update(ddata) + unpadder.finalize()
        return ddata
    class f:pass
    f.encrypt = enc
    f.decrypt = dec
    return f 
開發者ID:cilame,項目名稱:vrequest,代碼行數:45,代碼來源:pymultialgo.py


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