-2

I have been given a public key by my client, and I want to send him a text message which would be encrypted with his public key. The public key is with .pub extension.

I am trying to do this in bash via openssl command and via python using pycrypto module with no luck. I am a novice with no experience in cryptography.

How can I go about this.Thanks in advance

public_key

Vedant Aggrawal
  • 400
  • 4
  • 17
  • You should use gnupg. Look here: https://superuser.com/questions/919188/how-to-encrypt-a-file-using-gnupg-in-linux – Pumpkin Oct 31 '18 at 08:55
  • Possible duplicate of https://stackoverflow.com/questions/21327491/using-pycrypto-how-to-import-a-rsa-public-key-and-use-it-to-encrypt-a-string – Zohar Meir Oct 31 '18 at 09:11
  • 1
    Possible duplicate of [Using pycrypto, how to import a RSA public key and use it to encrypt a string?](https://stackoverflow.com/questions/21327491/using-pycrypto-how-to-import-a-rsa-public-key-and-use-it-to-encrypt-a-string) – stovfl Oct 31 '18 at 11:34

1 Answers1

2

Suppositions:

  • The public key given by your client is in "key.pub" file
  • Taking the input from the user at run time for the string or text to be encrypted in a variable named, "msg".
  • Already installed Crypto.PublicKey library using command "sudo pip install Crypto.PublicKey"

Code:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5

with open("key.pub", 'r') as f1:
    pubkey = f1.read()

msg = raw_input("Enter String to be encrypted: ")
print("raw string->", msg)

keyPub = RSA.importKey(pubkey) # import the public key
cipher = Cipher_PKCS1_v1_5.new(keyPub)
cipher_text = cipher.encrypt(msg.encode()) # now we have the cipher
print("cipher text->", cipher_text)

Format for the Key in the file:

The format of key in the file should be like this,

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAybVqRvfYvWbLsB98BqkD
lWd0/5y6SyhHt6/r6M0l7JXBweqMvxVt7XmI2yqPL56YxzcgQ8ycDkoqHJ+XozgP
iRnLNpYRlCzsiaOElbmQcnrI8iOb9Ahm6j0cbBB1S8VNvD+u9RQJt53zPxPj8/Dq
f1oNGFXOM8udNYWZaRCukLs/TumsAn0a+BF4639WtFiUvTWdVhlyvCQTs49ytRkH
rXH30RkB528RIvTGeW8xBTV4NaiTIzAEKCVSPagLr4Hzbb9b5+bODic/zkLGQazy
/NKOFgiB7kD2+WEMcuhTr5noeXau0PDAhgmrBhzzWOjUwwaO+ACvJLkPXZfjhy7P
+wIDAQAB
-----END PUBLIC KEY-----
  • thanks for the answer. I am getting "ValueError: RSA key format is not supported". I edited the question with the link to public key – Vedant Aggrawal Oct 31 '18 at 12:21
  • I have edited the answer. I checked your uploaded file. The format was not correct. Please check the format in the answer above. Edit your Public key like the format above. Thanks – Talha Ghaffar Oct 31 '18 at 14:19
  • how can i change the format of the public key provided to me. – Vedant Aggrawal Nov 02 '18 at 05:43
  • You can either provide the format I provided, to your client, asking him to convert it in the similar format. Or can try yourself adding the "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----" phrases. – Talha Ghaffar Nov 02 '18 at 13:39