本文整理汇总了Python中Cryptodome.Cipher.PKCS1_OAEP.new方法的典型用法代码示例。如果您正苦于以下问题:Python PKCS1_OAEP.new方法的具体用法?Python PKCS1_OAEP.new怎么用?Python PKCS1_OAEP.new使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cryptodome.Cipher.PKCS1_OAEP
的用法示例。
在下文中一共展示了PKCS1_OAEP.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decrypt
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def decrypt(self, ciphertext, key, padding="pkcs1_padding"):
if padding == "pkcs1_padding":
cipher = PKCS1_v1_5.new(key)
if self.with_digest:
dsize = SHA.digest_size
else:
dsize = 0
sentinel = Random.new().read(32 + dsize)
text = cipher.decrypt(ciphertext, sentinel)
if dsize:
_digest = text[-dsize:]
_msg = text[:-dsize]
digest = SHA.new(_msg).digest()
if digest == _digest:
text = _msg
else:
raise DecryptionFailed()
else:
if text == sentinel:
raise DecryptionFailed()
elif padding == "pkcs1_oaep_padding":
cipher = PKCS1_OAEP.new(key)
text = cipher.decrypt(ciphertext)
else:
raise Exception("Unsupported padding")
return text
示例2: encryptDataWithPubKey
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def encryptDataWithPubKey(data, pubKeyFile=RSA_pubKeyFile, outFile=None):
from Cryptodome.PublicKey import RSA
from Cryptodome.Random import get_random_bytes
from Cryptodome.Cipher import AES, PKCS1_OAEP
recipient_key = RSA.import_key(open(pubKeyFile).read())
session_key = get_random_bytes(16)
# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
print ()
print (ciphertext)
print ()
print (tag)
print ()
if outFile:
file_out = open(outFile, "wb")
file_out.write(cipher_rsa.encrypt(session_key))
[ file_out.write(x) for x in (ciphertext.nonce, tag, ciphertext) ]
示例3: encrypt
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def encrypt(self, message):
if isinstance(message, unicode):
message = message.encode('utf8')
cipher = PKCS1_OAEP.new(self.key)
encryptedMessage = cipher.encrypt(message)
return base64.b64encode(encryptedMessage)
示例4: testEncryptDecrypt3
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def testEncryptDecrypt3(self):
# Verify that OAEP supports labels
pt = self.rng(35)
xlabel = self.rng(22)
cipher = PKCS.new(self.key1024, label=xlabel)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
示例5: testEncryptDecrypt1
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def testEncryptDecrypt1(self):
# Encrypt/Decrypt messages of length [0..128-2*20-2]
for pt_len in xrange(0,128-2*20-2):
pt = self.rng(pt_len)
cipher = PKCS.new(self.key1024)
ct = cipher.encrypt(pt)
pt2 = cipher.decrypt(ct)
self.assertEqual(pt,pt2)
示例6: testDecrypt1
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def testDecrypt1(self):
# Verify decryption using all test vectors
for test in self._testData:
# Build the key
comps = [ long(rws(test[0][x]),16) for x in ('n','e','d') ]
key = RSA.construct(comps)
# The real test
cipher = PKCS.new(key, test[4])
pt = cipher.decrypt(t2b(test[2]))
self.assertEqual(pt, t2b(test[1]))
示例7: encrypt
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def encrypt(self, msg, key, padding="pkcs1_padding"):
if padding == "pkcs1_padding":
cipher = PKCS1_v1_5.new(key)
if self.with_digest: # add a SHA digest to the message
h = SHA.new(msg)
msg += h.digest()
elif padding == "pkcs1_oaep_padding":
cipher = PKCS1_OAEP.new(key)
else:
raise Exception("Unsupported padding")
return cipher.encrypt(msg)
示例8: signature
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def signature(email, password, key):
signature = bytearray(b'\x00')
struct = key_to_struct(key)
signature.extend(hashlib.sha1(struct).digest()[:4])
cipher = PKCS1_OAEP.new(key)
encrypted_login = cipher.encrypt((email + u'\x00' + password).encode('utf-8'))
signature.extend(encrypted_login)
return base64.urlsafe_b64encode(signature)
示例9: testEncryptDecrypt4
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def testEncryptDecrypt4(self):
# Verify that encrypt() uses the custom MGF
global mgfcalls
# Helper function to monitor what's requested from MGF
def newMGF(seed,maskLen):
global mgfcalls
mgfcalls += 1
return bchr(0x00)*maskLen
mgfcalls = 0
pt = self.rng(32)
cipher = PKCS.new(self.key1024, mgfunc=newMGF)
ct = cipher.encrypt(pt)
self.assertEqual(mgfcalls, 2)
self.assertEqual(cipher.decrypt(ct), pt)
示例10: add_issuer_key
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def add_issuer_key(self, private_key):
"""
Adds a private key to the list of keys available for decryption
and signatures
:return: Boolean - Whether the key is already in the list
"""
new_key = RSAKey(key=import_rsa_key(private_key),
kid=self.__generate_key_id(private_key))
for key in self.issuer_private_keys:
if new_key.kid == key.kid:
return False
self.issuer_private_keys.append(new_key)
self.loaded_issuer_private_keys[new_key.kid] = PKCS1_OAEP.new(
RSA.importKey(private_key))
return True
示例11: testEncryptDecrypt2
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def testEncryptDecrypt2(self):
# Helper function to monitor what's requested from RNG
global asked
def localRng(N):
global asked
asked += N
return self.rng(N)
# Verify that OAEP is friendly to all hashes
for hashmod in (MD2,MD5,SHA1,SHA256,RIPEMD160):
# Verify that encrypt() asks for as many random bytes
# as the hash output size
asked = 0
pt = self.rng(40)
cipher = PKCS.new(self.key1024, hashmod, randfunc=localRng)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
self.assertEqual(asked, hashmod.digest_size)
示例12: parse_key_response
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def parse_key_response(self, headerdata):
# Init Decryption
enc_key = headerdata['keyresponsedata']['keydata']['encryptionkey']
hmac_key = headerdata['keyresponsedata']['keydata']['hmackey']
encrypted_encryption_key = base64.standard_b64decode(enc_key)
encrypted_sign_key = base64.standard_b64decode(hmac_key)
cipher_rsa = PKCS1_OAEP.new(self.rsa_key)
# Decrypt encryption key
cipher_raw = cipher_rsa.decrypt(encrypted_encryption_key)
encryption_key_data = json.JSONDecoder().decode(cipher_raw)
self.encryption_key = self.__base64key_decode(encryption_key_data['k'])
# Decrypt sign key
sign_key_raw = cipher_rsa.decrypt(encrypted_sign_key)
sign_key_data = json.JSONDecoder().decode(sign_key_raw)
self.sign_key = self.__base64key_decode(sign_key_data['k'])
示例13: PKCS1_OAEP_AES_decrypt
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def PKCS1_OAEP_AES_decrypt(self, data, inpFile):
file_in = open(inpFile, "rb")
private_key = self._privateKey
enc_session_key, nonce, tag, ciphertext = [ file_in.read(x) for x in (private_key.size_in_bytes(), 16, 16, -1) ]
# Decrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(private_key)
session_key = cipher_rsa.decrypt(enc_session_key)
# Decrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX, nonce)
data = cipher_aes.decrypt_and_verify(ciphertext, tag)
return data
示例14: testEncrypt1
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def testEncrypt1(self):
# Verify encryption using all test vectors
for test in self._testData:
# Build the key
comps = [ long(rws(test[0][x]),16) for x in ('n','e') ]
key = RSA.construct(comps)
# RNG that takes its random numbers from a pool given
# at initialization
class randGen:
def __init__(self, data):
self.data = data
self.idx = 0
def __call__(self, N):
r = self.data[self.idx:N]
self.idx += N
return r
# The real test
cipher = PKCS.new(key, test[4], randfunc=randGen(t2b(test[3])))
ct = cipher.encrypt(t2b(test[1]))
self.assertEqual(ct, t2b(test[2]))
示例15: PKCS1_OAEP_AES_encrypt
# 需要导入模块: from Cryptodome.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Cryptodome.Cipher.PKCS1_OAEP import new [as 别名]
def PKCS1_OAEP_AES_encrypt(self, data, outFile):
# recipient_key = Crypto.PublicKey.RSA.import_key(open("receiver.pem").read())
recipient_key = self._privateKey
session_key = Random.get_random_bytes(32)
# Encrypt the session key with the public RSA key
cipher_rsa = PKCS1_OAEP.new(recipient_key)
# Encrypt the data with the AES session key
cipher_aes = AES.new(session_key, AES.MODE_EAX)
ciphertext, tag = cipher_aes.encrypt_and_digest(data)
# creazione del file con i dati crypted
print ('file {FILE} has been created with encrypted data.'.format(FILE=outFile))
file_out = open(outFile, "wb")
file_out.write(cipher_rsa.encrypt(session_key))
[ file_out.write(x) for x in (cipher_aes.nonce, tag, ciphertext) ]
return ciphertext