I am trying to implement a Symmetric-key agreement scheme using public-key cryptography between multiple clients via socket communication and I have been testing the encryption and decryption functionality.
import rsa
def generateAKeys():
(publicKey, privateKey) = rsa.newkeys(1024)
with open('keys/APubKey.pem', 'wb') as p:
p.write(publicKey.save_pkcs1('PEM'))
with open('keys/APrivKey.pem', 'wb') as p:
p.write(privateKey.save_pkcs1('PEM'))
def generateBKeys():
(publicKey, privateKey) = rsa.newkeys(1024)
with open('keys/BPubKey.pem', 'wb') as p:
p.write(publicKey.save_pkcs1('PEM'))
with open('keys/BPrivKey.pem', 'wb') as p:
p.write(privateKey.save_pkcs1('PEM'))
def loadKeys():
with open('keys/APubKey.pem', 'rb') as p:
APubKey = rsa.PublicKey.load_pkcs1(p.read())
with open('keys/APrivKey.pem', 'rb') as p:
APrivKey = rsa.PrivateKey.load_pkcs1(p.read())
with open('keys/BPubKey.pem', 'rb') as p:
BPubKey = rsa.PublicKey.load_pkcs1(p.read())
with open('keys/BPrivKey.pem', 'rb') as p:
BPrivKey = rsa.PrivateKey.load_pkcs1(p.read())
return APubKey, APrivKey, BPubKey, BPrivKey
def encrypt(message, key):
return rsa.encrypt(message.encode('utf8'), key)
def decrypt(ciphertext, key):
try:
return rsa.decrypt(ciphertext, key).decode('utf8')
except:
return False
def sign(message, key):
return rsa.sign(message.encode('utf8'), key, 'SHA-1')
def verify(message, signature, key):
try:
return rsa.verify(message.encode('utf8'), signature, key,) == 'SHA-1'
except:
return False
APubKey, APrivKey, BPubKey, BPrivKey = loadKeys()
message = 'Hello'
ciphertext = encrypt(message, BPubKey)
print(ciphertext)
print(type(ciphertext))
signature = sign(message, APrivKey)
message1 = "{}|{}".format(ciphertext, signature)
#Simulating message transfer over socket
message2 = message1 #Recived Messgae
message2 = message2.split("|")
ciphertext1, signature1 = message2[0], message2[1]
print(ciphertext1)
print(type(ciphertext1))
print(ciphertext == ciphertext1)
plaintext = decrypt(ciphertext1, BPrivKey)
if plaintext:
print(f'Message text: {plaintext}')
else:
print(f'Unable to decrypt the message.')
if verify(plaintext, signature1, APubKey):
print('Successfully verified signature')
else:
print('The message signature could not be verified')
The information being sent back and forth between clients is being packaged up in the following format message1 = "{}|{}".format(ciphertext, signature) which allows the message to be split up and read in individual pieces. In this file, I am just packaging the message in and out of this format to simulate the transmission between clients.
My issue is that once the encrypted message is created (which is in bytes) and packaged into the format shown above, it converts in as a string. When I try to extract that encrypted message and decrypt it, I am unable as it is no longer in bytes format. I have tried converting the string into bytes but to no avail. Any help would be appreciated.
Below is a copy of the output whereby the encrypted bytes are converted to the exact same string
b'\x8d\x19b\xbbE\xc1\xbf/K\x8b_}\xae\x0c\xb3\x8b\x94\x19\xfb\x8e\x01q6\xf5\xdd2O\\\xd2\xbf\xe3\xca\xcf\xac\x03\x84\xe9\xd7\xce\x13\xaaB\x16x\x13\xb4x26\xfc\x1c\xfe6\x82\xf6\x89i\x8aT\x87\xa0\xe9\x85p\xea\x03\x0fK\xb1/\xe0\x1b\x10a\x83\xa2\x0b}b\x0b\xc3\xe1"\xc1\x94\xfa\x95\xb0iQ\xa8%sqs\xc9\x98`gd,\xdc;\xa3\x08\xb6\xc3T:2N\xede-\x16\xe6i\xdc?3\x1d\x8c\x12^\x10\xde*\xc5'
<class 'bytes'>
b'\x8d\x19b\xbbE\xc1\xbf/K\x8b_}\xae\x0c\xb3\x8b\x94\x19\xfb\x8e\x01q6\xf5\xdd2O\\\xd2\xbf\xe3\xca\xcf\xac\x03\x84\xe9\xd7\xce\x13\xaaB\x16x\x13\xb4x26\xfc\x1c\xfe6\x82\xf6\x89i\x8aT\x87\xa0\xe9\x85p\xea\x03\x0fK\xb1/\xe0\x1b\x10a\x83\xa2\x0b}b\x0b\xc3\xe1"\xc1\x94\xfa\x95\xb0iQ\xa8%sqs\xc9\x98`gd,\xdc;\xa3\x08\xb6\xc3T:2N\xede-\x16\xe6i\xdc?3\x1d\x8c\x12^\x10\xde*\xc5'
<class 'str'>
False
Unable to decrypt the message.
The message signature could not be verified