-1

msg = string + rsa_encryped_result rsa_encrypt(msg) failed to decrypt when we connect string with rsaResult include code

we tried to change to base64 instead of String, also use PAD ,

class Crypto():


    def __init__(self):
        random_generator = Random.new().read
        self.private_key = RSA.generate(1024, random_generator)
        self.public_key = self.private_key.publickey()

    def get_string_public_key(self):
        return self.public_key.exportKey()

    def decrypt_message(self, message):
        decrypted_message = self.private_key.decrypt(message)
        return decrypted_message

def encrypt_message(message, public_key):
    encrypted_message = public_key.encrypt(message, 32)[0]
    return encrypted_message


def make_public_key(string_key):
    return RSA.importKey(string_key)

bob = Crypto()


first_msg = "Hello World"
print first_msg + ' - the original'
msg1 = encrypt_message(first_msg, make_public_key(bob.get_string_public_key()))
msg2 = msg1 + "Hello again"
msg3 = encrypt_message(msg2, make_public_key(bob.get_string_public_key()))
# work ok
print 'simple one \r\n', bob.decrypt_message(msg1)  

print msg2 + ' the second one'
msg3 = bob.decrypt_message(msg2)
# does not work
print 'second one \r\n', msg3  

print msg3.find("again")

we expected to find the string 'again' inside msg3 but we didnt

second one 
w%z�� ����bV�W�T    ?�q
H?�)�9��)0�Ӕk��Ӯ4dK-�CH��!�b�5��d�_������Ð�a2g
����K��_E�n�Wܧlz]y�~�jBY�̐&}\��h���$��WZ��
-1
M Reza
  • 18,350
  • 14
  • 66
  • 71
Merry
  • 1

2 Answers2

1

You tried to decrypt msg2 and not msg3 so it fails, msg2 is not a valid rsa encrypted so the decrypt doesnt understand

0

after reading more about RSA i understand that there is no way to do what i want (to wrap messages one on one with different RSA-keys and them unwrap them again). so i choose another algorithm: i used RAC4 - a symmetric algorithm to encrypt the messages i used RSA - to send the RAC4-key over the network thanks to all

Merry
  • 1