solar
solar

Reputation: 19

Encrypting strings with a predetermined key

I'm trying to make a program that fetches someone's MAC Address from their machine, encrypts it, and then copies it to their clipboard. However, all of the encryption methods I see generate a fresh key and thus can't be deciphered without knowing the specific key that was used to encrypt the address. Is there a way to use one key to encrypt everything so all addresses can be decrypted with a single key, and a fresh key is not generated every single time?

Upvotes: 0

Views: 1749

Answers (3)

Seyed_Ali_Mohtarami
Seyed_Ali_Mohtarami

Reputation: 1164

You can use Import RSA library rsa

installing :

pip install rsa

Then encrypt the byte string with the public key. Then the encrypted string can be decrypted with the private key. The public key can only be used for encryption and the private can only be used for decryption

for examle:

import rsa 
publicKey, privateKey = rsa.newkeys(512)
message = "Salio"           #this is MAC Address
encMessage = rsa.encrypt(message.encode(), publicKey)  
print("encrypted: ", encMessage)
decMessage = rsa.decrypt(encMessage, privateKey).decode() 
print("decrypted : ", decMessage)

Upvotes: 1

Finomnis
Finomnis

Reputation: 22396

You are describing asymmetric encryption here.

That exists and is a thing, yes. It works by by having a public key for encryption, and a private key for decryption.

There are multiple algorithms that implement that, like RSA. RSA is supported by the python library cryptography.

A tutorial on how to use it can be found for example here: https://nitratine.net/blog/post/asymmetric-encryption-and-decryption-in-python/


Working example

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes


# Generate keys. This only has to be done once.
# Store the keys somewhere and distribute them with the program.
def generate_keys():
    private_key = rsa.generate_private_key(
        public_exponent=65537,
        key_size=2048,
        backend=default_backend()
    )
    public_key = private_key.public_key()

    private_key_string = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption()
    )
    public_key_string = public_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo
    )

    return (public_key_string, private_key_string)


# This is just for demonstration.
# In practice, don't generate them every time.
# Only generate them once and store them in a string or a file.
(public_key_string, private_key_string) = generate_keys()


# REMOTE COMPUTER
# Only use the public key here, the private key has to stay private.
public_key = serialization.load_pem_public_key(public_key_string, backend=default_backend())

mac_address = "01:23:45:67:89:AB"
mac_address_encrypted = public_key.encrypt(
    mac_address.encode(),
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
)


# LOCAL SERVER
# Use private keys here to decrypt the MAC address
private_key = serialization.load_pem_private_key(private_key_string, password=None, backend=default_backend())
mac_address_decrypted = private_key.decrypt(
    mac_address_encrypted,
    padding.OAEP(
        mgf=padding.MGF1(algorithm=hashes.SHA256()),
        algorithm=hashes.SHA256(),
        label=None
    )
).decode()
print(mac_address_decrypted)

https://ideone.com/0eEyU6

Upvotes: 1

Breno Monteiro
Breno Monteiro

Reputation: 101

you can try it, using Fernet Lib:

from cryptography.fernet import Fernet

# IMPORTANT: The encryption key must be binary, so the prefix 'b' before the string
# To create a random binary key, use 'generate_key' method as below:
# new_key = Fernet.generate_key()

crypto_key = b'dTlQeWw2u5oMoFPHXQ7vQHPaQUEiD71SYzWeJJAQQUk='
mac = '00:33:A4:D9:F1:E1'

fernet = Fernet(crypto_key)

enc_mac = fernet.encrypt(mac.encode())
dec_mac = fernet.decrypt(enc_mac).decode()

print(f'Fixed encryption key: {crypto_key}')
print('Original MAC string: ', mac)
print('Encrypted MAC string: ', enc_mac)
print('Decrypted MAC string: ', dec_mac)

Upvotes: 1

Related Questions