本文整理匯總了Python中pycryptopp.cipher.aes.AES.new方法的典型用法代碼示例。如果您正苦於以下問題:Python AES.new方法的具體用法?Python AES.new怎麽用?Python AES.new使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pycryptopp.cipher.aes.AES
的用法示例。
在下文中一共展示了AES.new方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: aes
# 需要導入模塊: from pycryptopp.cipher.aes import AES [as 別名]
# 或者: from pycryptopp.cipher.aes.AES import new [as 別名]
def aes(key, data, counter=False):
""" encrypt data with aes, using either pycryptopp or PyCrypto.
Args
key: The encryption key
data: plain text data
counter: a callable, usually not needed
"""
# using either pycryptopp...
if hasattr(AES, "process"):
a = AES(key)
return a.process(data)
# ... or PyCrypto
counter = counter or Counter()
a = AES.new(key, AES.MODE_CTR, counter=counter)
rest = len(data) % 16
if not rest:
return a.encrypt(data)
# Data length must be a multiple of 16
# Pad with bytes all of the same value as the number of padding bytes
pad = (16 - rest)
data += chr(pad) * pad
return a.encrypt(data)[:-pad]