Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import os
  2. import base64
  3. import json
  4. import binascii
  5. from Crypto.Cipher import AES
  6.  
  7. from config import modulus, nonce, pub_key
  8.  
  9.  
  10. modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
  11. nonce = '0CoJUm6Qyw8W8jud'
  12. pub_key = '010001'
  13.  
  14.  
  15. def encrypted_request(text):
  16. text = json.dumps(text)
  17. secKey = createSecretKey(16)
  18. encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
  19. encSecKey = rsaEncrypt(secKey, pub_key, modulus)
  20. data = {'params': encText, 'encSecKey': encSecKey}
  21. return data
  22.  
  23.  
  24. def aesEncrypt(text, secKey):
  25. pad = 16 - len(text) % 16
  26. text = text + chr(pad) * pad
  27. encryptor = AES.new(secKey, 2, '0102030405060708')
  28. ciphertext = encryptor.encrypt(text)
  29. ciphertext = base64.b64encode(ciphertext).decode('utf-8')
  30. return ciphertext
  31.  
  32.  
  33. def rsaEncrypt(text, pubKey, modulus):
  34. text = text[::-1]
  35. rs = pow(int(binascii.hexlify(text), 16), int(pubKey, 16), int(modulus, 16))
  36. return format(rs, 'x').zfill(256)
  37.  
  38.  
  39. def createSecretKey(size):
  40. return binascii.hexlify(os.urandom(size))[:16]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement