Search code examples
pythoncryptographyrsapublic-key-encryptioncrypt

Convert bytes to string or store as bytes python


I'm trying to encrypt string with python by RSA but the encrypted string returns as bytes I'm trying to find a way either to convert bytes to string and store it in db or store it as bytes as it is but I couldn't find either of them i use this project in django and mysql i need some help on it and this is the full source code

import secrets
import string

import rsa


def create_token():
    alphabet = string.ascii_letters + string.digits
    token = ''.join(secrets.choice(alphabet) for i in range(64))
    return token


def generate_keys():
    (pubKey, privKey) = rsa.newkeys(2048)
    with open('keys/pubkey.pem', 'wb') as f:
        f.write(pubKey.save_pkcs1('PEM'))

    with open('keys/privkey.pem', 'wb') as f:
        f.write(privKey.save_pkcs1('PEM'))


def load_keys():
    with open('E:/workstation/projects/amon/cryptoPATH/keys/pubkey.pem', 'rb') as f:
        pubKey = rsa.PublicKey.load_pkcs1(f.read())

    with open('E:/workstation/projects/amon/cryptoPATH/keys/privkey.pem', 'rb') as f:
        privKey = rsa.PrivateKey.load_pkcs1(f.read())

    return pubKey, privKey


def encrypt_rsa(msg, key):
    return rsa.encrypt(msg.encode('utf-16'), key)


def decrypt_rsa(ciphertext, key):
    try:
        return rsa.decrypt(ciphertext, key).decode('utf-16')
    except:
        return False


def sign_sha1(msg, key):
    return rsa.sign(msg.encode('utf-16'), key, 'SHA-1')


def verify_sha1(msg, signature, key):
    try:
        return rsa.verify(msg.encode('utf-16'), signature, key) == 'SHA-1'
    except:
        return False


# generate_keys()
pubKey, privKey = load_keys()


def encrypt(msg):
    ciphertext = encrypt_rsa(msg, pubKey)
    return ciphertext


def decrypt(message):
    plaintext = decrypt_rsa(message, privKey)
    if plaintext:
        return plaintext
    else:
        return 'Could not decrypt the message.'


message = 'encrypted'
ciphertext = encrypt(message)
plaintext = decrypt(ciphertext)
print(str(ciphertext)[2:-1])

print(f'msg= {message}\n cipher= {ciphertext}\n plain= {plaintext}\n')

OUTPUT

outputof code


Solution

  • by using base64 solved my problem

    import base64
    import secrets
    import string
    
    import rsa
    
    
    def create_token():
        alphabet = string.ascii_letters + string.digits
        token = ''.join(secrets.choice(alphabet) for i in range(32))
        return token
    
    
    def generate_keys():
        (pubKey, privKey) = rsa.newkeys(2048)
        with open('keys/pubkey.pem', 'wb') as f:
            f.write(pubKey.save_pkcs1('PEM'))
    
        with open('keys/privkey.pem', 'wb') as f:
            f.write(privKey.save_pkcs1('PEM'))
    
    
    def load_keys():
        with open('keys/pubkey.pem', 'rb') as f:
            pubKey = rsa.PublicKey.load_pkcs1(f.read())
    
        with open('keys/privkey.pem', 'rb') as f:
            privKey = rsa.PrivateKey.load_pkcs1(f.read())
    
        return pubKey, privKey
    
    
    def encrypt_rsa(msg, key):
        return rsa.encrypt(msg.encode('utf-8'), key)
    
    
    def decrypt_rsa(ciphertext, key):
        try:
            return rsa.decrypt(ciphertext, key).decode('utf-8')
        except:
            return False
    
    
    def sign_sha1(msg, key):
        return rsa.sign(msg.encode('utf-8'), key, 'SHA-1')
    
    
    def verify_sha1(msg, signature, key):
        try:
            return rsa.verify(msg.encode('utf-8'), signature, key) == 'SHA-1'
        except:
            return False
    
    
    generate_keys()
    pubKey, privKey = load_keys()
    
    
    def encrypt(msg):
        ciphertext = encrypt_rsa(msg, pubKey)
        encoded = base64.b64encode(ciphertext)
        return encoded.decode()
    
    
    def decrypt(message):
        message = base64.b64decode(message)
        plaintext = decrypt_rsa(message, privKey)
        if plaintext:
            return plaintext
        else:
            return 'Could not decrypt the message.'