Securing your apps with Public Key Cryptography & Digital Signature
Need for Digital Signature
If hacker manages to change the message before Bob sends the encrypted message to Mary, there is no way for Mary to know if the message originally came from Bob. Since Mary’s public key is known to anyone, anyone could send the encrypted message to Mary, claiming that he/she is Bob.
With his private key and the right software, Bob can put digital signatures on documents and other data. A digital signature is a "stamp" Bob places on the data which is unique to Bob, and is very difficult to forge. In addition, the signature assures that any changes made to the data that has been signed cannot go undetected.
Digital Signature works in the following way:
Step #1: Bob calculates Hash of his message. Hash functions are well-suited for ensuring data integrity because any change made to the contents of a message will result in the receiver calculating a different hash value than the one placed in the transmission by the sender. Since it is highly unlikely that two different messages will yield the same hash value, data integrity is ensured to a high degree of confidence. Note that there is no way to re-generate the message back from the Hash.
Step #2: Bob then encrypts the Hash with his Private key (Figure 2 below). The result becomes the Digital Signature of Bob.
Step #3: Upon receipt of the digital signature, Mary recovers the hash value calculated by Bob by decrypting the digital signature with Bob’s public key.
Step #4: Mary can then apply the Hash function to Bob’s original message, which she has already decrypted.
If the resultant hash value is not the same as the value supplied by Bob, then Mary knows that the message has been altered; if the hash values are the same, Mary should believe that the message she received is identical to the one that Bob sent.
Figure 2: Public Key Cryptography with Digital Signature providing non-repudiation
This scheme also provides non-repudiation since it proves that Bob sent the message; if the hash value recovered by Mary using Bob’s public key proves that the message has not been altered, then only Bob could have created the digital signature. Mary also has proof that she is the intended receiver; if she can correctly decrypt the message, then she must have correctly decrypted the session key meaning that her is the correct private key.
Pretty Good Privacy (PGP) and Hybrid Cryptography
The above scheme also form the basis of more popular PGP encryption and decryption program widely used to encrypt and decrypt data communication like emails, that used both conventional (aka Symmetric) Cryptography as well as Public Key cryptography.
When a user encrypts plaintext with PGP, PGP first compresses the plaintext. Apart from data compression to save disk space, this also strengthens cryptographic security as it reduces the data patterns found in plain text. Based on random mouse movements and keystroke, PGP creates random session key, to encrypt plain text.
Once the data is encrypted, the session key is then encrypted to the recipient's public key. This public key-encrypted session key is transmitted along with the ciphertext to the recipient.
Decryption works in the reverse. The recipient's copy of PGP uses his or her private key to recover the temporary session key, which PGP then uses to decrypt the conventionally-encrypted ciphertext.
The combination of the two encryption methods combines the convenience of public key encryption with the speed of conventional encryption and works exactly the way explained in previous section.