1

I am making a license check where I will be encrypting the string text with private key and decrypting it with the public key, I know that it is not the standard way of decryption but I am trying to build in reverse manner for my project, the standard cryptodome library checks for private key while decryption and it returns an error and saying "not a private key", so any idea to fix it up?

public_key = b64decode(pubkey)
public_key = RSA.importKey(public_key )
cipher = PKCS1_v1_5.new(public_key )
plaintext = cipher.decrypt(b64decode(encrpted_data_base64), "Error while decrypting")
print(plaintext)

File "c:\Users\Desktop\license.py", line 17, in <module>
    plaintext = cipher.decrypt(b64decode(encrpted_data_base64), "Error while decrypting")
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\Cryptodome\Cipher\PKCS1_v1_5.py", line 180, in decrypt
    m_int = self._key._decrypt(ct_int)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\Cryptodome\PublicKey\RSA.py", line 156, in _decrypt
    raise TypeError("This is not a private key")
TypeError: This is not a private key
Machavity
  • 30,841
  • 27
  • 92
  • 100

1 Answers1

0

There isn't any use in doing things in reverse because your public key is public. It gets broadcasted over the network as a part of the encryption process in response to a request made asking for the key. Anyone can make that same request to ask for the key. That means anyone would be able to decrypt a message with the public key if public keys were used for decryption which is why they are not. They are only used to encrypt.

Maybe you are looking to sign the message? The way signing works is after a signature has been attached to a message, by the holder of the private key, anyone, through the use of the public key and message, can prove, using a feature of RSA for proving signatures, that the attached signature was indeed added by the holder of the private key.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51