Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from Crypto import Random
- from Crypto.Cipher import AES
- import base64
- class Toolbox():
- def __init__(self):
- """
- Provides peripheral & reusable functions for the app
- """
- self.ENCRYPTION_KEY = '40c8c33c-6733-4244-bdd8-e118e0e8'.encode()
- def encrypt( self, raw_text ):
- """
- Encrypts a string using bcrypt algorithm
- @raw_text is data to be encrypted
- returns bcrypt encrypted text
- """
- # - AES requires its bnlocks to be of size mod 16
- # - However we don't want unusable characters in a password
- # - First Pipe "|" delimits the end of the password
- if len(raw_text)%16 != 0:
- raw_text = raw_text+"|"
- # - Then Tilde "~" acts as padding.
- while len(raw_text)%16 != 0:
- raw_text = raw_text+"~"
- raw_text = raw_text.encode()
- # Generate a new random block
- random_block = Random.new().read( AES.block_size )
- # Encrypt plain text with a key and hash
- cipher = AES.new( self.ENCRYPTION_KEY, AES.MODE_CBC, random_block )
- text_cipher = cipher.encrypt( raw_text )
- block_and_cipher = random_block + text_cipher
- b64_cipher = base64.b64encode( block_and_cipher )
- return b64_cipher
- def decrypt( self, encrypted_text ):
- """
- Decrypts into text using bcrypt algorithm
- @encrypted_text:Input is a bcrypt encrypted block
- returns raw text
- """
- encrypted_text = base64.b64decode(encrypted_text)
- iv = encrypted_text[:16]
- try:
- cipher = AES.new(self.ENCRYPTION_KEY, AES.MODE_CBC, iv )
- except:
- self.cleanPrint("Decryption error","error")
- return
- # - To compensate for delimeter/padding above, we rstrip
- # - the excess & remove the padding
- decrypt_msg = cipher.decrypt(encrypted_text[16:])
- decrypt_msg = decrypt_msg.decode()
- decrypt_msg = decrypt_msg.rstrip("~")
- decrypt_msg = decrypt_msg.rstrip("|")
- return decrypt_msg
- def main():
- tlbx = Toolbox()
- raw_String = "how now brown cow"
- print(raw_String)
- enc = tlbx.encrypt(raw_String)
- print(enc)
- dec = tlbx.decrypt(enc)
- print(dec)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment