PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Implementing the RSA algorithm in Python

Go down

Implementing the RSA algorithm in Python Empty Implementing the RSA algorithm in Python

Post by jamied_uk 3rd December 2022, 13:30

Code:
pip install rsa
Once the package is downloaded, the first thing we need to do is to import
Code:
rsa
into our program:
Code:
import rsa
We will start by implementing two helper methods to generate the private and public keys. The keys will be a tuple of public and private keys, and then write the keys into files.
To write the keys into the files, we will create a folder named
Code:
Keys
in our project folder. The
Code:
Keys
folder will have two files for holding private and public keys; one key in each file.
We will implement this using the code below:
Code:
def generateKeys():
    (publicKey, privateKey) = rsa.newkeys(1024)
    with open('keys/publcKey.pem', 'wb') as p:
        p.write(publicKey.save_pkcs1('PEM'))
    with open('keys/privateKey.pem', 'wb') as p:
        p.write(privateKey.save_pkcs1('PEM'))
Now that we have saved the keys in our files, the next thing we need to do is to load the keys.
To load the keys, we will use the code snippet below that opens the files that we created above, and return both the private and public keys:
Code:
def loadKeys():
    with open('keys/publicKey.pem', 'rb') as p:
        publicKey = rsa.PublicKey.load_pkcs1(p.read())
    with open('keys/privateKey.pem', 'rb') as p:
        privateKey = rsa.PrivateKey.load_pkcs1(p.read())
    return privateKey, publicKey
Next, create two other methods to encrypt and decrypt our message.
Start by creating the encryption method using the code below. The encrypt method will take the message and the encryption key.
After defining the encrypt method, we need to return the encrypted message. We will encode the message in
Code:
ASCII
and give it the key:
Code:
def encrypt(message, key):
    return rsa.encrypt(message.encode('ascii'), key)
Let us now create the decryption method. This method will take the ciphertext and the key to decrypt. What we will do is to try and decrypt the message and return the decrypted message.
Since we used the
Code:
ASCII
encoding, we will use
Code:
ASCII
decoding as well.
If this fails, it means that the key was not able to decrypt the message, so what we will do is return false. We will use the code below to implement the decryption method.
Code:
def decrypt(ciphertext, key):
    try:
        return rsa.decrypt(ciphertext, key).decode('ascii')
    except:
        return False
Finally, we will create two methods to sign and verify our message with a key using the
Code:
sha1 hash function
. This method will take the message and the key so that we sign our message with a key.
The message that we will encode will be given the key and our hashing algorithm. In this case,
Code:
SHA-1
.
The sign method is implemented using the code below:
Code:
def sign(message, key):
    return rsa.sign(message.encode('ascii'), key, 'SHA-1')
For the verification of the message, we will create the verify method and pass in the message, the signature to verify, and the key. So, what we need to do is to try to verify our message.
This verify method returns the hash algorithm used in the signature. So, what we do is to check that this is equal to the hash algorithm, i.e;
Code:
SHA-1
.
If the signature is authentic, then it returns true. In case there is an exception, it will return false which means that the verification has failed. This means either the message or the signature were manipulated and are not authentic.
Code:
def verify(message, signature, key):
    try:
        return rsa.verify(message.encode('ascii'), signature, key,) == 'SHA-1'
    except:
        return False
Now that we have the RSA algorithm, we will create our program. We will start by generating our keys.
We will call the generate keys method, load the public and private keys as implemented in the code below:
Code:
generateKeys()
publicKey, privateKey =load_keys()
We will then take the message input from the user, and encrypt the message using the public key. This represents the sender of the message:
Code:
message = input('Write your message here:')
ciphertext = encrypt(message, publicKey)
Now that we have the ciphertext, we will generate the signatures using the code below to sign the message with our private key. This enables the sender to verify the message with the public key and determine if the message is authentic:
Code:
signature = sign(message, privateKey)
Next, we will decrypt our encrypted message to have plain text. To implement this, we will create a decryption method and pass it in the ciphertext and the private key as shown below:
Code:
text = decrypt(ciphertext, privateKey)
After getting our plain text, the next thing to do is to print out the ciphertext and the signature.
Code:
print(f'Cipher text: {ciphertext}')
print(f'Signature: {signature}')
We will check the plain text in the next step. If it is plain text, then we indicate
Code:
message was successfully decrypted
otherwise,
Code:
unable to decrypt the message
:
Code:
if text:
    print(f'Message text: {text}')
else:
    print(f'Unable to decrypt the message.')
We verify our signature using the code below:
Code:
if verify(text, signature, publicKey):
    print(Successfully verified signature)
else:
    print('The message signature could not be verified')
With that, you can enter your message, encrypt, and then decrypt it.
jamied_uk
jamied_uk
Admin

Posts : 3020
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum