Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.PublicKey import RSA
- from Crypto import Random
- def rsa_keygen():
- global key
- random_generator = Random.new().read
- key = RSA.generate(2048, random_generator)
- return key
- def encryption(config_data):
- global encrypted_config
- public_key = key.publickey()
- encrypted_config = public_key.encrypt(str(config_data), 32)
- def decryption(data):
- global decrypted_config
- decrypted_config = key.decrypt(str(data))
- return decrypted_config
- def main():
- rsa_keygen()
- #create a file first
- f=open('abc.txt','w')
- data='abcabcabc'
- f.writelines(data)
- f.close()
- f=open('abc.txt','r')
- adata=f.read()
- f.close()
- encryption(adata)
- #write the encryption binary back into abc.txt so that no one understand what abc contains.
- f=open('abc.txt','wb')
- f.write(encrypted_config[0])
- f.close()
- #open file again for decrypting
- f=open('abc.txt','rb')
- dedata=f.read()
- f.close()
- decryption(dedata)
- #write back the decrypted message into abc.txt
- f=open('abc.txt','w')
- f.write(decrypted_config)
- f.close()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement