Reputation: 35
I was trying to make secure socket connection but when i create private key and create pubkey out of private key and create another private key then encrypt another private key with pubkey and decrypt it with first private key gives me garbage
priv_key=RSA.generate(1024)
pub_key=priv_key.publickey()
another_priv_key=RSA.generate(1024)
priv_key.decrypt(pub_key.encrypt(another_priv_key.exportKey('PEM'),32))
output:b')O\xa5\xf9\xa5k%z\x10S.\x13\x06@\x83i\x1eI\xc6R\x1a\xa5}{h\x9aW0R\t\xa9\xf1\xdb\xe3\xdc\x1b\xd0\xe54\xb1\xf0^\x93 \xb6\x93\x16\xed\x98d8\xec6\xe2B\xde\x87(\xf7\xce\x9a)\x99ZR\xae$T\x1b\x19<\x12e\xfc\xd4\x92\xf3\x82l\x81ed,\xf4{CH\xe0\xd2\xdb\x0b#\xde9: ?\xea\x99\xe9\xd8\xe6\xca\xadFs\xa6\xc6\xc9x#\x96h\xf0\x83\x7f\xd25\xa4)Iv\xa6]\x15%5\x96'
Upvotes: 1
Views: 53
Reputation: 24567
You're trying to encrypt a plaintext that is far too long. RSA with a 1024-bit modulus should just about manage a string of 128 characters, but your ASCII-armoured private key will probably contain something closer to 1000 characters.
You can encrypt the key in separate pieces if you like.
>>> priv_key.decrypt(pub_key.encrypt(another_priv_key.exportKey('PEM')[:128],32))
'-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQCgoIPQspwO6DdHc9nGN6r9+qlMpFP+0PCik4QjbRzktX2U98sy\n2hZKXTjEj02+/bhJGL8dP63JDJQBWNL'
Upvotes: 1