当前位置: 首页>>代码示例>>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;未经允许,请勿转载。