Skip to content

Commit

Permalink
Fix Encryption in Actuator Python Library (#23)
Browse files Browse the repository at this point in the history
* Fix Encryption in Actuator Python Library
* Update submodules.
  • Loading branch information
rfrsilva authored and nmsa committed Dec 26, 2019
1 parent 054d891 commit 23132a2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion common/tmalibrary/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='tmalibrary',
version='9.12',
version='10.0',
author='Rui Silva',
author_email='rfsilva@student.dei.uc.pt',
description='Probe and Actuator Library',
Expand Down
8 changes: 4 additions & 4 deletions common/tmalibrary/tmalibrary/actuator/HandleRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import base64
import json
from .ActuatorPayload import ActuatorPayload
import os
from flask import Response

class HandleRequest:

Expand All @@ -19,8 +17,10 @@ def generateResponse(self, plainResponse):
publicKeyExecutorPath = "keys/pub-key-executor"
publicKeyExecutor = keymanager.getPublicKey(publicKeyExecutorPath)
encryptedMessage = keymanager.encrypt(plainResponse,publicKeyExecutor)
response = base64.b64encode(encryptedMessage)
return Response("{}\n{}".format(response, signedResponseEncoded), mimetype='application/octet-stream')
encryptedMessageBase = base64.b64encode(encryptedMessage)
response = str(encryptedMessageBase) + '\n' + str(signedResponseEncoded)
responseBytes = response.encode()
return responseBytes

def processRequest(self, request):

Expand Down
22 changes: 12 additions & 10 deletions common/tmalibrary/tmalibrary/actuator/KeyManager.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
from Crypto.PublicKey import RSA
import Crypto.Cipher.PKCS1_v1_5
from Crypto.Cipher import PKCS1_v1_5
import Crypto.Signature.PKCS1_v1_5
from Crypto.Hash import SHA
from base64 import b64decode
from base64 import b64encode

import random
import os
import errno

class KeyManager:

def decrypt(self, text, private_key_string):

# import private key to an RSA object
private_key = RSA.importKey(private_key_string)
cipher = PKCS1_v1_5.new(private_key)

message = cipher.decrypt(b64decode(text),"Error while decrypting")
return message

# decrypt the text with the private key
cypher = Crypto.Cipher.PKCS1_v1_5.new(private_key)
decryptedText = cypher.decrypt(text,15)
return decryptedText

def encrypt(self, text, public_key_string):

# import public key to an RSA object
public_key = RSA.importKey(public_key_string)

# encrypt the plain text using the public key
encryptedText = public_key.encrypt(text.encode('utf-8'), 32)[0]
return encryptedText
cipher = PKCS1_v1_5.new(public_key)
ciphertext = b64encode(cipher.encrypt(bytes(text,"utf-8")))
return ciphertext

def getPrivateKey(self, filenameprivatekey):
# read private key from file
Expand Down

0 comments on commit 23132a2

Please sign in to comment.