本文整理汇总了Python中Cryptodome.PublicKey.RSA.construct方法的典型用法代码示例。如果您正苦于以下问题:Python RSA.construct方法的具体用法?Python RSA.construct怎么用?Python RSA.construct使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cryptodome.PublicKey.RSA
的用法示例。
在下文中一共展示了RSA.construct方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: privatekeyblob_to_pkcs1
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def privatekeyblob_to_pkcs1(key):
'''
parse private key into pkcs#1 format
:param key:
:return:
'''
modulus = bytes_to_long(key['modulus'][::-1]) # n
prime1 = bytes_to_long(key['prime1'][::-1]) # p
prime2 = bytes_to_long(key['prime2'][::-1]) # q
exp1 = bytes_to_long(key['exponent1'][::-1])
exp2 = bytes_to_long(key['exponent2'][::-1])
coefficient = bytes_to_long(key['coefficient'][::-1])
privateExp = bytes_to_long(key['privateExponent'][::-1]) # d
if PY3:
long = int
pubExp = long(key['rsapubkey']['pubexp']) # e
# RSA.Integer(prime2).inverse(prime1) # u
r = RSA.construct((modulus, pubExp, privateExp, prime1, prime2))
return r
示例2: testEncrypt1
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testEncrypt1(self):
# Verify encryption using all test vectors
for test in self._testData:
# Build the key
comps = [ int(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]))
示例3: testEncrypt1
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [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]))
示例4: decryptKey
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def decryptKey(encryptedKey):
"""Decrypts an RSA-encrypted key"""
rsa = RSA.construct((long(constants.rsaModulus), long(constants.rsaExponent)))
try:
return rsa.encrypt(encryptedKey, 0)[0]
except NotImplementedError:
# pycryptodome
return long_to_bytes(rsa._encrypt(bytes_to_long(encryptedKey)))
示例5: rsa_publickey
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def rsa_publickey(mod, exp):
return rsa_construct((mod, exp))
示例6: key_from_b64
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def key_from_b64(b64_key):
binaryKey = base64.b64decode(b64_key)
i = bytes_to_long(binaryKey[:4])
mod = bytes_to_long(binaryKey[4:4+i])
j = bytes_to_long(binaryKey[i+4:i+4+4])
exponent = bytes_to_long(binaryKey[i+8:i+8+j])
key = RSA.construct((mod, exponent))
return key
示例7: testExportKey1
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey1(self):
key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
derKey = key.exportKey("DER")
self.assertEqual(derKey, self.rsaKeyDER)
示例8: testExportKey2
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey2(self):
key = RSA.construct([self.n, self.e])
derKey = key.exportKey("DER")
self.assertEqual(derKey, self.rsaPublicKeyDER)
示例9: testExportKey3
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey3(self):
key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
pemKey = key.exportKey("PEM")
self.assertEqual(pemKey, b(self.rsaKeyPEM))
示例10: testExportKey4
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey4(self):
key = RSA.construct([self.n, self.e])
pemKey = key.exportKey("PEM")
self.assertEqual(pemKey, b(self.rsaPublicKeyPEM))
示例11: testExportKey5
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey5(self):
key = RSA.construct([self.n, self.e])
openssh_1 = key.exportKey("OpenSSH").split()
openssh_2 = self.rsaPublicKeyOpenSSH.split()
self.assertEqual(openssh_1[0], openssh_2[0])
self.assertEqual(openssh_1[1], openssh_2[1])
示例12: testExportKey8
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey8(self):
key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
pemKey = key.exportKey("PEM", pkcs=8)
self.assertEqual(pemKey, b(self.rsaKeyPEM8))
示例13: testExportKey9
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey9(self):
key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
self.assertRaises(ValueError, key.exportKey, "invalid-format")
示例14: testExportKey10
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey10(self):
# Export and re-import the encrypted key. It must match.
# PEM envelope, PKCS#1, old PEM encryption
key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
outkey = key.exportKey('PEM', 'test')
self.assertTrue(tostr(outkey).find('4,ENCRYPTED')!=-1)
self.assertTrue(tostr(outkey).find('BEGIN RSA PRIVATE KEY')!=-1)
inkey = RSA.importKey(outkey, 'test')
self.assertEqual(key.n, inkey.n)
self.assertEqual(key.e, inkey.e)
self.assertEqual(key.d, inkey.d)
示例15: testExportKey11
# 需要导入模块: from Cryptodome.PublicKey import RSA [as 别名]
# 或者: from Cryptodome.PublicKey.RSA import construct [as 别名]
def testExportKey11(self):
# Export and re-import the encrypted key. It must match.
# PEM envelope, PKCS#1, old PEM encryption
key = RSA.construct([self.n, self.e, self.d, self.p, self.q, self.pInv])
outkey = key.exportKey('PEM', 'test', pkcs=1)
self.assertTrue(tostr(outkey).find('4,ENCRYPTED')!=-1)
self.assertTrue(tostr(outkey).find('BEGIN RSA PRIVATE KEY')!=-1)
inkey = RSA.importKey(outkey, 'test')
self.assertEqual(key.n, inkey.n)
self.assertEqual(key.e, inkey.e)
self.assertEqual(key.d, inkey.d)