本文整理匯總了Python中Crypto.Cipher.PKCS1_v1_5.new方法的典型用法代碼示例。如果您正苦於以下問題:Python PKCS1_v1_5.new方法的具體用法?Python PKCS1_v1_5.new怎麽用?Python PKCS1_v1_5.new使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Crypto.Cipher.PKCS1_v1_5
的用法示例。
在下文中一共展示了PKCS1_v1_5.new方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: testEncrypt1
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def testEncrypt1(self):
for test in self._testData:
# Build the key
key = RSA.importKey(test[0])
# 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)
ct = cipher.encrypt(b(test[1]))
self.assertEqual(ct, t2b(test[2]))
示例2: signRSA
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 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)
示例3: __init__
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 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))
示例4: SignWith3Des
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def SignWith3Des(src):
# 首先對字符串取MD5
raw_buf = hashlib.md5(src.encode()).digest()
# 對16字節的md5做bcd2ascii
bcd_to_ascii = bytearray(32)
for i in range(len(raw_buf)):
bcd_to_ascii[2*i] = raw_buf[i]>>4
bcd_to_ascii[2*i+1] = raw_buf[i] & 0xf
# hex_to_bin轉換加密key
key = bytes.fromhex('3ECA2F6FFA6D4952ABBACA5A7B067D23')
# 對32字節的bcd_to_ascii做Triple DES加密(3DES/ECB/NoPadding)
des3 = DES3.new(key, DES3.MODE_ECB)
enc_bytes = des3.encrypt(bcd_to_ascii)
# bin_to_hex得到最終加密結果
enc_buf = ''.join(["%02X" % x for x in enc_bytes]).strip()
return enc_buf
# 根據svrid查詢client_msg_id
示例5: decrypt
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def decrypt(cls, data):
if RsaCrypt.CHIPPER is None:
rsa = SysConfig.get_init_rsa_obj()
RsaCrypt.CHIPPER = Cipher_pkcs1.new(rsa)
# 1024 bit的證書用128,2048 bit證書用256位
one_len = 128
ret_data = b''
# python2下需轉成str類型,否則異常
if IS_PY2:
data = str(data)
for i in range(0, len(data), one_len):
ret_data += RsaCrypt.CHIPPER.decrypt(data[i:i + one_len], RsaCrypt.RANDOM_GENERATOR)
return ret_data
示例6: get_client_proof
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def get_client_proof(clientnonce, servernonce, password, salt, iterations):
""" calculates server client proof (part of the SCRAM algorithm) """
msg = "%s,%s,%s" % (clientnonce, servernonce, servernonce)
salted_pass = hashlib.pbkdf2_hmac(
'sha256', password.encode('utf_8'), bytearray.fromhex(salt), iterations)
client_key = hmac.new(b'Client Key', msg=salted_pass,
digestmod=hashlib.sha256)
stored_key = hashlib.sha256()
stored_key.update(client_key.digest())
signature = hmac.new(msg.encode('utf_8'),
msg=stored_key.digest(), digestmod=hashlib.sha256)
client_key_digest = client_key.digest()
signature_digest = signature.digest()
client_proof = bytearray()
i = 0
while i < client_key.digest_size:
client_proof.append(client_key_digest[i] ^ signature_digest[i])
i = i + 1
return hexlify(client_proof)
示例7: rsa_encrypt
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def rsa_encrypt(rsae, rsan, data):
if (data is None or data == ''): return ''
N = int(rsan,16)
E = int(rsae,16)
b64data = base64.b64encode(data)
pubkey = construct((N, E))
cipher = PKCS1_v1_5.new(pubkey)
blocks = int(math.ceil(len(b64data) / 245.0))
result = []
for i in range(blocks):
block = b64data[i*245:(i+1)*245]
d = cipher.encrypt(block)
result.append(d)
result = hexlify(''.join(result))
if ((len(result) & 1) == 0):
return result
else:
return '0'+result
示例8: testEncrypt1
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def testEncrypt1(self):
for test in self._testData:
# Build the key
key = RSA.importKey(test[0])
# 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:self.idx+N]
self.idx += N
return r
# The real test
cipher = PKCS.new(key, randfunc=randGen(t2b(test[3])))
ct = cipher.encrypt(b(test[1]))
self.assertEqual(ct, t2b(test[2]))
示例9: encrypt
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def encrypt(payload, public_key):
"""
Encrypt a payload using an encrypted JSON wrapper.
See: https://diaspora.github.io/diaspora_federation/federation/encryption.html
:param payload: Payload document as a string.
:param public_key: Public key of recipient as an RSA object.
:return: Encrypted JSON wrapper as dict.
"""
iv, key, encrypter = EncryptedPayload.get_iv_key_encrypter()
aes_key_json = EncryptedPayload.get_aes_key_json(iv, key)
cipher = PKCS1_v1_5.new(public_key)
aes_key = b64encode(cipher.encrypt(aes_key_json))
padded_payload = pkcs7_pad(payload.encode("utf-8"), AES.block_size)
encrypted_me = b64encode(encrypter.encrypt(padded_payload))
return {
"aes_key": aes_key.decode("utf-8"),
"encrypted_magic_envelope": encrypted_me.decode("utf8"),
}
示例10: hash_token
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def hash_token(self):
try:
from Crypto.Hash import SHA1, HMAC
command = "{}".format(CMD_GET_KEY)
enc_command = await self.encrypt(command)
await self._ws.send(enc_command)
message = await self._ws.recv()
await self.parse_loxone_message(message)
message = await self._ws.recv()
resp_json = json.loads(message)
if 'LL' in resp_json:
if "value" in resp_json['LL']:
key = resp_json['LL']['value']
if key != "":
digester = HMAC.new(binascii.unhexlify(key),
self._token.token.encode("utf-8"), SHA1)
return digester.hexdigest()
return ERROR_VALUE
except:
return ERROR_VALUE
示例11: rws
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 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
示例12: setUp
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def setUp(self):
self.rng = Random.new().read
self.key1024 = RSA.generate(1024, self.rng)
# List of tuples with test data for PKCS#1 v1.5.
# Each tuple is made up by:
# Item #0: dictionary with RSA key component, or key to import
# Item #1: plaintext
# Item #2: ciphertext
# Item #3: random data
示例13: testEncrypt2
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def testEncrypt2(self):
# Verify that encryption fail if plaintext is too long
pt = '\x00'*(128-11+1)
cipher = PKCS.new(self.key1024)
self.assertRaises(ValueError, cipher.encrypt, pt)
示例14: testVerify1
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def testVerify1(self):
for test in self._testData:
# Build the key
key = RSA.importKey(test[0])
# The real test
cipher = PKCS.new(key)
pt = cipher.decrypt(t2b(test[2]), "---")
self.assertEqual(pt, b(test[1]))
示例15: testEncryptVerify1
# 需要導入模塊: from Crypto.Cipher import PKCS1_v1_5 [as 別名]
# 或者: from Crypto.Cipher.PKCS1_v1_5 import new [as 別名]
def testEncryptVerify1(self):
# Encrypt/Verify messages of length [0..RSAlen-11]
# and therefore padding [8..117]
for pt_len in xrange(0,128-11+1):
pt = self.rng(pt_len)
cipher = PKCS.new(self.key1024)
ct = cipher.encrypt(pt)
pt2 = cipher.decrypt(ct, "---")
self.assertEqual(pt,pt2)