def __init__(self, type="client"): if type == "client": rng = Random().read key = RSA.generate(2048, rng) self.__publicKey = key.publickey().exportKey('PEM') self.__privateKey = key.exportKey('PEM')
def generate_keys(): # generate keys modlen = 2560 pvKey = RSA.generate(modlen, Random().read) pbKey = pvKey.publickey() # export keys as unicode strings pbKey = pbKey.exportKey("PEM") pvKey = pvKey.exportKey("PEM") pbKey = pbKey.decode('utf-8') pvKey = pvKey.decode('utf-8') return pvKey, pbKey
def encrypt(self, data): plain_text = self.pad(data) iv = Random().read(AES.block_size) cipher = AES.new(self.key, AES.MODE_OFB, iv) return b64encode(iv + cipher.encrypt(plain_text.encode())).decode()
from flask import Flask, render_template, request from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_v1_5 from Crypto.Random import new as Random from base64 import b64encode, b64decode app = Flask(__name__) #Set up the RSA keys randon_generator = Random().read set_of_keys = RSA.generate(1024, randon_generator) public_key = set_of_keys.publickey() # Defining Routes @app.route('/') def index(): return render_template('index.html') @app.route('/submit', methods=['POST']) def submit(): if request.method == 'POST': message = request.form['message'] isEncry = request.form['isEncry'] # Validating input if message == '': return render_template('index.html', message='Please enter required fields') elif len(message) > 200 and isEncry == 'true': return render_template('index.html', message='Please insert an string with less than 200 characters') # Encrypting Message if isEncry == 'true':
def generate_key(self, key_length): assert key_length in [1024, 2048, 4096] rng = Random().read self.key = RSA.generate(key_length, rng)
def generate_key(self, key_length): rng = Random().read self.key = RSA.generate(key_length, rng)
def encrypt(self): plain_text = self.readfile() iv = Random().read(AES.block_size) cipher = AES.new(self.key, AES.MODE_OFB, iv) a = base64.b64encode(iv + cipher.encrypt(bytes(plain_text, 'utf8'))).decode() self.writefile(a)