Advertisement
Guest User

Python Encryption Code

a guest
Apr 22nd, 2016
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from Crypto.PublicKey import RSA
  2. from Crypto import Random
  3.  
  4. def rsa_keygen():
  5.     global key
  6.     random_generator = Random.new().read
  7.     key = RSA.generate(2048, random_generator)
  8.     return key
  9.  
  10. def encryption(config_data):
  11.     global encrypted_config
  12.     public_key = key.publickey()
  13.     encrypted_config = public_key.encrypt(str(config_data), 32)
  14.  
  15. def decryption(data):
  16.     global decrypted_config
  17.     decrypted_config = key.decrypt(str(data))
  18.     return decrypted_config
  19.  
  20. def main():
  21.  
  22.  
  23.     rsa_keygen()
  24.  
  25.  
  26.     #create a file first
  27.     f=open('abc.txt','w')
  28.     data='abcabcabc'
  29.     f.writelines(data)
  30.     f.close()
  31.  
  32.     f=open('abc.txt','r')
  33.     adata=f.read()
  34.     f.close()
  35.  
  36.  
  37.     encryption(adata)
  38.  
  39.     #write the encryption binary back into abc.txt so that no one understand what abc contains.
  40.     f=open('abc.txt','wb')        
  41.     f.write(encrypted_config[0])
  42.     f.close()
  43.  
  44.     #open file again for decrypting
  45.     f=open('abc.txt','rb')
  46.     dedata=f.read()
  47.     f.close()
  48.  
  49.     decryption(dedata)    
  50.  
  51.     #write back the decrypted message into abc.txt
  52.     f=open('abc.txt','w')
  53.     f.write(decrypted_config)
  54.     f.close()
  55.  
  56.  
  57.  
  58. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement