本文整理汇总了Python中Crypto.Cipher.PKCS1_OAEP.new方法的典型用法代码示例。如果您正苦于以下问题:Python PKCS1_OAEP.new方法的具体用法?Python PKCS1_OAEP.new怎么用?Python PKCS1_OAEP.new使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Crypto.Cipher.PKCS1_OAEP
的用法示例。
在下文中一共展示了PKCS1_OAEP.new方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def create(vek, keySizeBytes, certificatePath):
#print("VEK: " + str(binascii.hexlify(vek)))
publicKeyPem = open(certificatePath).read()
publicKey = RSA.importKey(publicKeyPem)
# Convert from PEM to DER
lines = publicKeyPem.replace(" ", '').split()
publicKeyDer = binascii.a2b_base64(''.join(lines[1:-1]))
cert = x509.load_pem_x509_certificate(SmartStr(publicKeyPem), default_backend())
subjectName = cert.subject.rfc4514_string()
serial = cert.serial_number
cipher = PKCS1_OAEP.new(key=publicKey, hashAlgo=SHA256, mgfunc=lambda x, y: pss.MGF1(x, y, SHA1))
wrapped_key = cipher.encrypt(vek)
#print("WrappedKey: " + str(binascii.hexlify(wrapped_key)))
return CertEncryptedKeyBag(subjectName, serial, keySizeBytes, wrapped_key)
示例2: testDecrypt
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def testDecrypt(self):
g = rdflib.Graph()
g.parse(data=keybagturtle, format="turtle")
kb = keybag.PasswordWrappedKeyBag.load(g)
key = "password"
kek = digest.pbkdf2_hmac("sha256", key, kb.salt, kb.iterations, kb.keySizeBytes);
vek = aes_unwrap_key(kek, kb.wrappedKey)
key1 = vek[0:16]
key2 = vek[16:]
tweak = codecs.decode('00', 'hex')
cipher = python_AES.new((key1, key2), python_AES.MODE_XTS)
text = cipher.decrypt(target_ciphertext, tweak)
self.assertEqual(src[0:len(src)], text[0:len(src)])
示例3: testEncrypt1
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.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
key._randfunc = randGen(t2b(test[3]))
cipher = PKCS.new(key, test[4])
ct = cipher.encrypt(t2b(test[1]))
self.assertEqual(ct, t2b(test[2]))
示例4: testEncryptDecrypt1
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def testEncryptDecrypt1(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,RIPEMD):
# Verify that encrypt() asks for as many random bytes
# as the hash output size
asked = 0
pt = self.rng(40)
self.key1024._randfunc = localRng
cipher = PKCS.new(self.key1024, hashmod)
ct = cipher.encrypt(pt)
self.assertEqual(cipher.decrypt(ct), pt)
self.failUnless(asked > hashmod.digest_size)
示例5: encrypt
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def encrypt(self, plaintext, esn):
"""
Encrypt the given Plaintext with the encryption key
:param plaintext:
:return: Serialized JSON String of the encryption Envelope
"""
init_vector = get_random_bytes(16)
cipher = AES.new(self.encryption_key, AES.MODE_CBC, init_vector)
ciphertext = base64.standard_b64encode(
cipher.encrypt(Padding.pad(plaintext.encode('utf-8'), 16))).decode('utf-8')
encryption_envelope = {
'ciphertext': ciphertext,
'keyid': '_'.join((esn, str(self.sequence_number))),
'sha256': 'AA==',
'iv': base64.standard_b64encode(init_vector).decode('utf-8')
}
return json.dumps(encryption_envelope)
示例6: encryptPassword
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def encryptPassword(email, password):
gdpk = "AAAAgMom/1a/v0lblO2Ubrt60J2gcuXSljGFQXgcyZWveWLEwo6prwgi3iJIZdodyhKZQrNWp5nKJ3srRXcUW+F1BD3baEVGcmEgqaLZUNBjm057pKRI16kB0YppeGx5qIQ5QjKzsR8ETQbKLNWgRY0QRNVz34kMJR3P/LgHax/6rmf5AAAAAwEAAQ=="
binaryKey = b64decode(gdpk).encode('hex')
half = binaryKey[8:264]
modulus = long(half, 16)
half = binaryKey[272:278]
exponent = long(half, 16)
sha1hash = sha1(b64decode(gdpk)).digest()
signature = "00" + sha1hash[:4].encode('hex')
key = RSA.construct((modulus, exponent))
cipher = PKCS1_OAEP.new(key)
plain = email + "\x00" + password
encrypted = cipher.encrypt(plain).encode('hex')
ste = signature + encrypted
output = unhexlify(ste)
encryptedPassword = b64encode(output).encode('ascii').replace("+","-").replace("/","_")
return encryptedPassword
示例7: auth_digital
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def auth_digital(self, title_id, title_version, device_token, ticket):
self.verify_ticket(ticket, title_id)
plain_key = get_random_bytes(16)
aes = AES.new(plain_key, AES.MODE_CBC, iv=bytes(16))
encrypted_ticket = aes.encrypt(pad(ticket, 16))
rsa_key = RSA.construct((RSA_MODULUS, RSA_EXPONENT))
rsa = PKCS1_OAEP.new(rsa_key, SHA256)
encrypted_key = rsa.encrypt(plain_key)
req = HTTPRequest.post("/v3/application_auth_token")
req.form["application_id"] = "%016x" %title_id
req.form["application_version"] = "%08x" %title_version
req.form["device_auth_token"] = device_token
req.form["media_type"] = "DIGITAL"
req.form["cert"] = b64encode(encrypted_ticket)
req.form["cert_key"] = b64encode(encrypted_key)
response = self.request(req, True)
return response.json
示例8: test_encrypt
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def test_encrypt(self, sign_message, encrypt_message, encrypt_key, get_key, get_iv):
message = Random.new().read(2000)
encryption = Encryption(self.sender_key, self.recipient_key)
sender_key_bytes = int(self.sender_key.publickey().n.bit_length() / 8)
encrypt_message.return_value = Random.new().read(len(message))
encrypt_key.return_value = Random.new().read(sender_key_bytes)
get_iv.return_value = Random.new().read(16)
encrypted_message = encryption.encrypt(message)
data = base64.b64decode(encrypted_message)
iv = data[0:16]
encrypted_key = data[16:(16 + sender_key_bytes)]
encrypted_message = data[(16 + sender_key_bytes):]
self.assertEqual(encrypt_message.return_value, encrypted_message)
self.assertEqual(encrypt_key.return_value, encrypted_key)
self.assertEqual(get_iv.return_value, iv)
encrypt_key.assert_called_once_with(get_key.return_value)
encrypt_message.assert_called_once_with(
sign_message.return_value, message, get_key.return_value, get_iv.return_value
)
sign_message.assert_called_once_with(message)
示例9: test_decrypt
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def test_decrypt(self, verify, get_message, get_signature, get_decrypted_message, get_key, get_iv, hexlify):
decryption = Decryption(self.recipient_key, self.sender_key)
raw = Random.new().read(2000)
encrypted = base64.b64encode(raw)
message = get_message.return_value
signature = get_signature.return_value
hexlify_signature = hexlify.return_value
decrypted_message = get_decrypted_message.return_value
iv = get_iv.return_value
key = get_key.return_value
verify.return_value = True
returned_message, returned_signature = decryption.decrypt(encrypted)
self.assertEqual(message, returned_message)
self.assertEqual(hexlify_signature, returned_signature)
get_message.assert_called_once_with(decrypted_message)
get_decrypted_message.assert_called_once_with(iv, key, raw)
get_iv.assert_called_once_with(raw)
get_key.assert_called_once_with(raw)
get_signature.assert_called_once_with(decrypted_message)
verify.assert_called_once_with(signature, message)
hexlify.assert_called_once_with(signature)
示例10: signRSA
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def signRSA(message, privatekey, hashAlg):
global hash1
hash1 = hashAlg
signer = PKCS1_v1_5.new(privatekey)
if (hash1 == "SHA-512"):
digest = SHA512.new()
elif (hash1 == "SHA-384"):
digest = SHA384.new()
elif (hash1 == "SHA-256"):
digest = SHA256.new()
elif (hash1 == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()
digest.update(message)
return signer.sign(digest)
示例11: __init__
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def __init__(self, key_pair, passphrase=None, cipher=utils.AESCTRCipher(), mat_desc=None):
super(RsaProvider, self).__init__(cipher=cipher, mat_desc=mat_desc)
self.wrap_alg = headers.RSA_NONE_PKCS1Padding_WRAP_ALGORITHM
if key_pair and not isinstance(key_pair, dict):
raise ClientError('Invalid type, the type of key_pair must be dict!')
try:
if 'public_key' in key_pair:
self.__encrypt_obj = PKCS1_v1_5.new(RSA.importKey(key_pair['public_key'], passphrase=passphrase))
if 'private_key' in key_pair:
self.__decrypt_obj = PKCS1_v1_5.new(RSA.importKey(key_pair['private_key'], passphrase=passphrase))
except (ValueError, TypeError) as e:
raise ClientError(str(e))
示例12: unwrap_key
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def unwrap_key(self, privateKey):
key = RSA.importKey(open(privateKey).read())
cipher = PKCS1_OAEP.new(key=key, hashAlgo=SHA256, mgfunc=lambda x, y: pss.MGF1(x, y, SHA1))
vek = cipher.decrypt(self.wrappedKey)
#print("VEK: " + str(binascii.hexlify(vek)))
return vek
示例13: testEncrypt
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def testEncrypt(self):
vek = binascii.unhexlify("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
plaintext = src + b'\x00' * (512-len(src))
key1 = vek[0:16]
key2 = vek[16:]
tweak = codecs.decode('00', 'hex')
cipher = python_AES.new((key1,key2), python_AES.MODE_XTS)
ciphertext = cipher.encrypt(plaintext, tweak)
self.assertEqual(target_ciphertext, ciphertext)
示例14: testWrap
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def testWrap(self):
keysize = 0x20 # in bytes
key = "password"
iterations = 147256
saltSize = 16
salt = binascii.unhexlify("000102030405060708090a0b0c0d0e0f")
#hhh = hashlib.pbkdf2_hmac("sha256", key.encode(), salt, iterations, keysize);
#print(len(hhh))
#print(binascii.hexlify(hhh))
kek = digest.pbkdf2_hmac("sha256", key, salt, iterations, keysize);
print(binascii.hexlify(kek))
#h = pbkdf2_sha256.encrypt(key, rounds=iterations, salt_size=saltSize)
# print(h)
vek = binascii.unhexlify("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
print(len(vek))
wrapped_key= aes_wrap_key(kek, vek)
print(binascii.hexlify(wrapped_key))
plaintext = src + b'\x00' * (512-len(src))
#msg = dict_xts_aes['msg%i' % i].decode('hex')
#key = (dict_xts_aes['key1_%i' % i].decode('hex'), dict_xts_aes['key2_%i' % i].decode('hex'))
key1 = vek[0:16]
key2 = vek[16:]
#cip = dict_xts_aes['cip%i' % i].decode('hex')
#n = dict_xts_aes['n%i' % i].decode('hex')
tweak = codecs.decode('00', 'hex')
print(len(tweak))
cipher = python_AES.new((key1,key2), python_AES.MODE_XTS)
ciphertext = cipher.encrypt(plaintext, tweak)
print(len(ciphertext))
print(binascii.hexlify(ciphertext))
示例15: rws
# 需要导入模块: from Crypto.Cipher import PKCS1_OAEP [as 别名]
# 或者: from Crypto.Cipher.PKCS1_OAEP import new [as 别名]
def rws(t):
"""Remove white spaces, tabs, and new lines from a string"""
for c in ['\n', '\t', ' ']:
t = t.replace(c,'')
return t