Python RSA Encryption And Decryption Example

The RSA algorithm (Rivest-Shamir-Adleman) is a cryptographic algorithm that is used for specific security services or purposes, which enables public-key encryption and is widely used to secure sensitive data, particularly when it is being sent over an insecure network such as the HTTP. A public key is shared publicly, while a private key is secret and must not be shared with anyone.
The following illustration highlights how asymmetric cryptography works:


Example ,

RSA Encryption and Decryption with RSA-OAEP Padding,

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64

# 2048 is the number of bits for RSA
keys = RSA.generate(2048)

publicKey = keys.publickey()
publicKeyPEM = publicKey.exportKey()
print("\n",publicKeyPEM.decode('ascii'))


privateKeyPEM = keys.exportKey()
print("\n",privateKeyPEM.decode('ascii'))

# Your secret text
secretMessage = 'This is your secret'

#encrypt the message with the RSA public key
encryptor = PKCS1_OAEP.new(publicKey)
encrypted = encryptor.encrypt(secretMessage.encode())
print("\nEncrypted:", base64.b64encode(encrypted).decode('ascii'))

#decrypt the message with the RSA private key
decryptor = PKCS1_OAEP.new(keys)
decrypted = decryptor.decrypt(encrypted)
print('\nDecrypted Message:', decrypted)

Run the application:

python3 rsademo.py

Output:

user@user-desktop:~/Downloads/golang$ python3 rsademo.py

 -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3+v1KRkLSMyKO1Zp3bQW
aXxMbZu7zCjMnOj7VUYtTdLqMbZ89JgTWPPUoYPkydHSkSoBCP67/CsNczeHWW1R
g6Vr5hYqp3NSsEJdLhf2s3OPe4jU/0OpnH+YzPwZILe+uPwsSzsFRWae3kTbVhtW
XuZc/Lfx3KOxeR2FZWs4oJIjN7EiJzGuvvDwndk4WgRD++XtEMfSkVl2PB2CU8hc
G2yKcaVQDSLBwMKHqwsgy+K/wPmclLtkKZvgtsu23iL2iNfTyOvV3ewGrBZ841hl
UjyO5I+oOXz0KzPkKdFyGYRSQk6WlJdoQ8tLYmLUCkIiChUz2w+7iAL9qMK3iTmU
UwIDAQAB
-----END PUB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PYV
sfYC10y4wHYBiAUUbmYeJJBL3hyVp7NFLlSMz5MCgYEAkrkfNs6UcpT41ZnLgf7f
vE4Qk8JW9aNAj8+uduJ4VMZjKInSeT/5lnBEy0DxDypNykywuk6h3O++ZaWBCKkk
JK0dGhPAqLHZ8OppzSm3/oLEAvmOET3KZhC5SHHKsN75Z9rP5Tw8H9KNwNVG4cdr
O42YMZURMmQJUxWOjKVd2wECgYBBBimIuAcSoKg+6Vfux38HslEalzhjMRbatdSa
Ls4oB7ub9PFUMssA3JFk5cN7a3k32/bonb4y6jOTJx83Lqoiv2qLZhP7x83cwGLd
lG1oN/nXlpPGmBJuL07XQIEKBQNPkcNpeVZng4J1xPrBH+7ZupXp+4wtP7tZunG9
CnFaNQKBgC/WWLEhbthbJYtnNJosJ7umgmeMYgaDHnj9R38gAAn9meN0xeKspW97
jVonUpJeslYJZn1iVlBVPAjegO6HU/enAETfgenLwdcvT0yJ2Goyg11u586r2LJT
WjppD9IO0dk6hC+qFK1X12Gmjfi2M7Mp4hVMYHL86RqTt+ZtPRDT
-----END RSA PRIVATE KEY-----

Encrypted: kB1lr7vx1ggVHJ8dCl3NrM9wCJq6SN5gwBP1ZRLEf+HvB8Tdojh3oKOPt781l5oZr9ESZOUZNBZ2eoml6TQ0v06rdvT1VZC7JMnpCMbizJ2gtflUDTugaOU2QNub9M1ypltdDkORZy5F2hJrSSUX1Z1AaFUPQa1GCtTyamFMDT1JkvMmCX+AOogX/bE7N/eMnivXHAv+FfmC8tyKiTzKX1ZhrKH2s/zCLKCxrbK/x625UgPvqn3RsH3OcEtGwTilpebMIDlHEmE7I1t1OUl1G8364YjcqvuFigbqlbpgKv9V50kL5hGGHf/JUBujw2BXO9gSb5RImmZfnNgSMqA2WQ==

Decrypted Message: b'This is your secret'

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example