Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
2 Corrected typo
source | link

To clarify one point from the question not covered in the otherwise excellent answer by @PTW-105 (and asked in the comment there by @JVE999):

I thought public key is to encrypt data, not to decrypt data...

The keys work both ways - what is encrypted with the public key can only be decrypted with the private and vice versa. We just decide one is private and one is public, there's no conceptual difference.

So if I encrypt data to send to you I use your public key to encrypt it and only you can decrypt it with your private key.

HoweverHowever, if I want to sign something, to prove it came from me, then I generate a hash of the message and encrypt that hash with my private key. Then anyone can decrypt it with my public key and compare to the actual message hash, but they know that only I could have encrypted it, since only I have my private key. So they know the message hash hasn't changed since I signed it, and therefore that it came from me.

As per the comments, if I want tothe above is not quite true. See the sign something, to prove it camelink from methe comment by @dave_thompson_085. However, then I generatethis isn't a hash of"how to sign properly" tutorial, just clarifying the messageroles of private and encryptpublic keys in encryption verses signing. The basic point in that hash with myregard is this: private key. Then anyone can decrypt it

  • To encrypt data the external party uses a public key and only the private key holder can decrypt it.
  • To sign, the private key holder uses a hash function and their private key (plus appropriate padding, etc.). The external party can then verify the signature using the public key. This ensures the message came from the private key holder (assuming no-one else has access to the private key).

Signing may sometimes (depending on the implementation) be done with my publicthe same key and compare topair as encryption, just used the actual message hashother way round, but they know that only I could have encryptedor it, since only I have my private may use a distinct key. So they know the message hash hasn't changed since I signed it pair (see another link, and therefore that it camealso from me.@dave_thompson_085's comment)

To clarify one point from the question not covered in the otherwise excellent answer by @PTW-105 (and asked in the comment there by @JVE999):

I thought public key is to encrypt data, not to decrypt data...

The keys work both ways - what is encrypted with the public key can only be decrypted with the private and vice versa. We just decide one is private and one is public, there's no conceptual difference.

So if I encrypt data to send to you I use your public key to encrypt it and only you can decrypt it with your private key.

However, if I want to sign something, to prove it came from me, then I generate a hash of the message and encrypt that hash with my private key. Then anyone can decrypt it with my public key and compare to the actual message hash, but they know that only I could have encrypted it, since only I have my private key. So they know the message hash hasn't changed since I signed it, and therefore that it came from me.

To clarify one point from the question not covered in the otherwise excellent answer by @PTW-105 (and asked in the comment there by @JVE999):

I thought public key is to encrypt data, not to decrypt data...

The keys work both ways - what is encrypted with the public key can only be decrypted with the private and vice versa. We just decide one is private and one is public, there's no conceptual difference.

So if I encrypt data to send to you I use your public key to encrypt it and only you can decrypt it with your private key.

However, if I want to sign something, to prove it came from me, then I generate a hash of the message and encrypt that hash with my private key. Then anyone can decrypt it with my public key and compare to the actual message hash, but they know that only I could have encrypted it, since only I have my private key. So they know the message hash hasn't changed since I signed it, and therefore that it came from me.

As per the comments, the above is not quite true. See the link from the comment by @dave_thompson_085. However, this isn't a "how to sign properly" tutorial, just clarifying the roles of private and public keys in encryption verses signing. The basic point in that regard is this:

  • To encrypt data the external party uses a public key and only the private key holder can decrypt it.
  • To sign, the private key holder uses a hash function and their private key (plus appropriate padding, etc.). The external party can then verify the signature using the public key. This ensures the message came from the private key holder (assuming no-one else has access to the private key).

Signing may sometimes (depending on the implementation) be done with the same key pair as encryption, just used the other way round, or it may use a distinct key pair (see another link, also from @dave_thompson_085's comment)

1
source | link

To clarify one point from the question not covered in the otherwise excellent answer by @PTW-105 (and asked in the comment there by @JVE999):

I thought public key is to encrypt data, not to decrypt data...

The keys work both ways - what is encrypted with the public key can only be decrypted with the private and vice versa. We just decide one is private and one is public, there's no conceptual difference.

So if I encrypt data to send to you I use your public key to encrypt it and only you can decrypt it with your private key.

However, if I want to sign something, to prove it came from me, then I generate a hash of the message and encrypt that hash with my private key. Then anyone can decrypt it with my public key and compare to the actual message hash, but they know that only I could have encrypted it, since only I have my private key. So they know the message hash hasn't changed since I signed it, and therefore that it came from me.