本文整理匯總了Python中M2Crypto.EVP.Cipher.set_padding方法的典型用法代碼示例。如果您正苦於以下問題:Python Cipher.set_padding方法的具體用法?Python Cipher.set_padding怎麽用?Python Cipher.set_padding使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類M2Crypto.EVP.Cipher
的用法示例。
在下文中一共展示了Cipher.set_padding方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: decrypt
# 需要導入模塊: from M2Crypto.EVP import Cipher [as 別名]
# 或者: from M2Crypto.EVP.Cipher import set_padding [as 別名]
def decrypt(chunk, key):
cipher = Cipher(alg=ALG, key=key, iv=IV, op=0, key_as_bytes=0, padding=PADDING) # 0 is decrypt
cipher.set_padding(padding=m2.no_padding)
v = cipher.update(chunk)
v = v + cipher.final()
del cipher #需要刪除
return v
示例2: encrypt_sn
# 需要導入模塊: from M2Crypto.EVP import Cipher [as 別名]
# 或者: from M2Crypto.EVP.Cipher import set_padding [as 別名]
def encrypt_sn(sn):
m=Cipher(alg = "aes_128_cbc", key = config['passout'], iv = '\x00' * 16, op = 1)
m.set_padding(padding=7)
v = m.update(sn)
v = v + m.final()
del m
return v
示例3: decryptPasswd
# 需要導入模塊: from M2Crypto.EVP import Cipher [as 別名]
# 或者: from M2Crypto.EVP.Cipher import set_padding [as 別名]
def decryptPasswd(buf, passKey, iv = '\x00' * 16):
cipher = Cipher(alg='aes_128_cbc', key=passKey, iv=iv, op=0) # 0 is decrypt
cipher.set_padding(padding=7)
v = cipher.update(buf)
v = v + cipher.final()
del cipher
return v
示例4: encrypt
# 需要導入模塊: from M2Crypto.EVP import Cipher [as 別名]
# 或者: from M2Crypto.EVP.Cipher import set_padding [as 別名]
def encrypt(chunk, key):
cipher = Cipher(alg=ALG, key=key, iv=IV, op=1, key_as_bytes=0,padding=PADDING) # 1 is encrypt
# padding 有時設置為1
cipher.set_padding(padding=m2.no_padding)
v = cipher.update(chunk)
v = v + cipher.final()
del cipher #需要刪除
return v
示例5: init_key_cipher
# 需要導入模塊: from M2Crypto.EVP import Cipher [as 別名]
# 或者: from M2Crypto.EVP.Cipher import set_padding [as 別名]
class M2CryptoEngine:
K_CIPHER = None
@classmethod
def init_key_cipher(cls, prikey):
cls.K_CIPHER = RSA.load_key_string(prikey)
def __init__(self, encrypted_header=None):
if encrypted_header:
self.__enc_data = encrypted_header
header = self.K_CIPHER.private_decrypt(encrypted_header, RSA.pkcs1_padding)
secret = header[:32]
iv = header[32:]
op = DEC
else:
secret = self._get_random(32)
iv = self._get_random(16)
self.__enc_data = self.K_CIPHER.public_encrypt(secret+iv, RSA.pkcs1_padding)
op = ENC
self.__cipher = Cipher(alg='aes_128_cbc', key=secret, iv=iv, op=op)
self.__cipher.set_padding(1)
def _get_random(self, cnt):
while True:
data = Rand.rand_bytes(cnt)
if data[0] != '\x00':
return data
def encrypt(self, data, finalize=False):
end_data = self.__cipher.update(data)
if finalize:
end_data += self.__cipher.final()
return end_data
def decrypt(self, data, finalize=False):
end_data = self.__cipher.update(data)
if finalize:
end_data += self.__cipher.final()
return end_data
def get_encrypted_header(self):
return self.__enc_data
示例6: fetchPrivateKey
# 需要導入模塊: from M2Crypto.EVP import Cipher [as 別名]
# 或者: from M2Crypto.EVP.Cipher import set_padding [as 別名]
def fetchPrivateKey(self):
"""Fetch the private key for the user and storage context
provided to this object, and decrypt the private key
by using my passphrase. Store the private key in internal
storage for later use."""
# Retrieve encrypted private key from the server
logging.debug("Fetching encrypted private key from server")
privKeyObj = self.ctx.get_item("keys", "privkey")
payload = json.loads(privKeyObj['payload'])
self.privKeySalt = base64.decodestring(payload['salt'])
self.privKeyIV = base64.decodestring(payload['iv'])
self.pubKeyURI = payload['publicKeyUri']
data64 = payload['keyData']
encryptedKey = base64.decodestring(data64)
# Now decrypt it by generating a key with the passphrase
# and performing an AES-256-CBC decrypt.
logging.debug("Decrypting encrypted private key")
passKey = PBKDF2(self.passphrase, self.privKeySalt, iterations=4096).read(32)
cipher = Cipher(alg='aes_256_cbc', key=passKey, iv=self.privKeyIV, op=0) # 0 is DEC
cipher.set_padding(padding=1)
v = cipher.update(encryptedKey)
v = v + cipher.final()
del cipher
decryptedKey = v
# Result is an NSS-wrapped key.
# We have to do some manual ASN.1 parsing here, which is unfortunate.
# 1. Make sure offset 22 is an OCTET tag; if this is not right, the decrypt
# has gone off the rails.
if ord(decryptedKey[22]) != 4:
logging.debug("Binary layout of decrypted private key is incorrect; probably the passphrase was incorrect.")
raise ValueError("Unable to decrypt key: wrong passphrase?")
# 2. Get the length of the raw key, by interpreting the length bytes
offset = 23
rawKeyLength = ord(decryptedKey[offset])
det = rawKeyLength & 0x80
if det == 0: # 1-byte length
offset += 1
rawKeyLength = rawKeyLength & 0x7f
else: # multi-byte length
bytes = rawKeyLength & 0x7f
offset += 1
rawKeyLength = 0
while bytes > 0:
rawKeyLength *= 256
rawKeyLength += ord(decryptedKey[offset])
offset += 1
bytes -= 1
# 3. Sanity check
if offset + rawKeyLength > len(decryptedKey):
rawKeyLength = len(decryptedKey) - offset
# 4. Extract actual key
privateKey = decryptedKey[offset:offset+rawKeyLength]
# And we're done.
self.privateKey = privateKey
logging.debug("Successfully decrypted private key")