Skip to content

Instantly share code, notes, and snippets.

@XSAM
Last active July 10, 2021 12:44
Show Gist options
  • Save XSAM/bfaf7cb579f1440f5c1cc0d83f2f11f2 to your computer and use it in GitHub Desktop.
Save XSAM/bfaf7cb579f1440f5c1cc0d83f2f11f2 to your computer and use it in GitHub Desktop.
1Key
import base64
import getpass
from cryptography.fernet import Fernet
BLOCK_SIZE = 32
# the character used for padding--with a block cipher such as AES, the value
# you encrypt must be a multiple of BLOCK_SIZE in length. This character is
# used to ensure that your value is always a multiple of BLOCK_SIZE
PADDING = b'{'
# one-liner to sufficiently pad the text to be encrypted
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
class Cipher(object):
def __init__(self, key):
self.bs = 32
self.key = pad(key)
self.cipher = Fernet(base64.urlsafe_b64encode(self.key))
def encrypt(self, raw):
return self.cipher.encrypt(raw)
def decrypt(self, raw):
return self.cipher.decrypt(raw)
def test(self, raw):
print("result: ", self.encrypt(raw))
assert self.encrypt(raw), self.decrypt(raw)
if __name__ == '__main__':
#key = getpass.getpass("Key:")
key = "This is a key"
cipher = Cipher(bytes(key, "utf-8"))
cipher.test(b"This is a test")
cryptography==2.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment