Password Encryptor Using Python


Password Encryptor using Python - Using Crypto.Cipher:

import sys
from base64 import b64encode, b64decode 
from Crypto.Cipher import AES

# AES key must be either 16, 24, or 32 bytes long. Right now set to "This is a key123".
# Do not make "obj" global. Generates garbage.

def encrypt(message):
    obj = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
    return b64encode(obj.encrypt(message))
    
def decrypt(message):
    obj = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
    return obj.decrypt(b64decode(message))

def main():
    print("Usage: 'python PasswordEncryptor.py -e message' for encryption. 'python PasswordEncryptor.py -d message' for decryption.")

    if sys.argv[1] == "-e":
        encrypt(sys.argv[2])
    elif sys.argv[1] == "-d":
        decrypt(sys.argv[2])

if __name__ == "__main__":
    main()
Password Encryptor using Python - Using cryptography.fernet:

# Fernet guarantees that a message encrypted using it cannot be manipulated or read without the key. Fernet is an implementation of symmetric (also known as “secret key”) authenticated cryptography.

import sys
from base64 import b64encode, b64decode 
from cryptography.fernet import Fernet
from joblib import dump, load

def encrypt(message):
    key = Fernet.generate_key() #this is your "password"
    dump(key, "key.joblib")
    cipher_suite = Fernet(key)
    return cipher_suite.encrypt(bytes(message, 'utf-8'))
    
def decrypt(message):
    cipher_suite = Fernet(load("key.joblib"))
    return cipher_suite.decrypt(bytes(message, 'utf-8'))

def main():
    print("Usage: 'python PasswordEncryptor.py -e message' for encryption. 'python PasswordEncryptor.py -d message' for decryption.")

    if sys.argv[1] == "-e":
        print(encrypt(sys.argv[2]))
    elif sys.argv[1] == "-d":
        print(decrypt(sys.argv[2]))

if __name__ == "__main__":
    main()

No comments:

Post a Comment