本文整理汇总了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())
示例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())
示例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())
示例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'))
示例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()
示例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()
示例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()
示例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