def encrypt(self, private_key, plaintext): try: Logger.printMessage( message='{methodName}'.format(methodName='encrypt'), description='{private_key} - {msg}'.format( private_key=private_key, msg=plaintext[0:10]), debug_module=True) #Unpack the key into it's components key, n = private_key ba64 = base64.b64encode(plaintext) ashex = Utils.asciiToHex(ba64) hexba64 = Utils.hexToBase64(ashex) ba64un = Utils.joinBase64(hexba64) decasc = Utils.decimalToAscii(ba64un) mensaje = Utils.textToAscii(decasc) Logger.printMessage( message='{methodName}'.format(methodName='encrypt'), description='{msg} - Length: {l}'.format(msg=mensaje[0:10], l=len(mensaje)), debug_module=True) mensaje1 = [(ord(chr(char))**key) % n for char in mensaje] mensajeHex = Utils.asciiToHex(mensaje1) mensajeBase64 = Utils.hexToBase64(mensajeHex) mensajeFinalBase64 = Utils.joinBase64(mensajeBase64) return mensajeFinalBase64.decode("utf-8") except Exception as e: Logger.printMessage( message='{methodName}'.format(methodName='encrypt'), description='{msg}'.format(msg=e)) return
def decrypt(self, public_key, ciphertext): Logger.printMessage( message='{methodName}'.format(methodName='decrypt'), description='{public_key}'.format(public_key=public_key), debug_module=True) #Unpack the key into its components key, n = public_key menRec = Utils.asciiToBase64(ciphertext.encode('utf-8')) menHex = Utils.base64ToHex(menRec) menDec = Utils.hexToDecimal(menHex) Logger.printMessage( message='{methodName}'.format(methodName='decrypt'), description='{msg}'.format(msg=menDec[0:10]), debug_module=True) menDesc = [((char**key) % n) for char in menDec] menAscii = Utils.decimalToAscii(menDesc) decasc = Utils.asciiToBase64(''.join(menAscii).encode()) hexba64 = Utils.base64ToHex(decasc) ashex = Utils.hexToDecimal(hexba64) deasc = Utils.decimalToAscii(ashex) ba64 = base64.b64decode(deasc.encode()) return ba64