Monday, 2 December 2013

Pretty Good Privacy (Encryption Decryption and Digital Signatures)

In 1991 Phil Zimmerman created Pretty Good Privacy or PGP to support privacy in online communication. Pretty Good Privacy (PGP) is most widely used mechanism in public key cryptography. PGP software is used nowadays in accord with OpenPGP standard.OpenPGP signatures certify:

  • files
  • text blocks
  • user IDs

PGP signature is hash of such data using hashing algorithm like SHA-1,encrypted by public key using PK algorithm. OpenPGP allows uses of multiple PK algorithms including:

  • DSA US standard for digital signatures from 1994
  • ElGamal discrete logarithm PK algorithm of Taher ElGamal from 1994
  • RSA Rivest, Shamir and Adelman algorithm from 1978
  • DSA/ElGamal can be termed DH/DSS (Diffie Hellman, DS Standard).

GNUPG is GNU project's implementation of OpenPGP standard.
Use it to generate new key pair as follows:

user@USER:$gpg --gen-key


Fill all the details for generating key.
Enter pass phrase: Any secret text you want to enter!
Repeat pass phrase: Any secret text you want to enter!


Public key created in this key pair
  • is 2048 bit RSA key
  • never expires
  • has key ID A5A35B36
  • has self-signed user ID Blogger (How to work with PGP.) <Blogger@blogger.com>
  • has fingerprint 6429 7328 8C2C 181D 41CA  9558 0ADF EA9D A5A3 5B36
  • is stored in public keyring ~/.gnupg/pubring.gpg
Private key is encrypted in private keyring ~/.gnupg/secring.gpg.
In case you forget encryption passphrase, create revocation certificate.

user@USER:$ gpg --output revoke.asc --gen-revoke "Blogger"

To list keys on your public keyring

user@USER:$ gpg --list-keys

To export this public key in binary format to "humbert.gpg"

user@USER:$ gpg --output humbert.gpg --export "Blogger"
Add argument "--armor" to output key in Radix-64 (Base64) format

user@USER:$ gpg --armor --export "Blogger"

Public/private key is added/deleted to one's public/private keyring by

user@USER:$ gpg --import harry_lime.gpg
user@USER:$ gpg --delete-secret-and-public-key Harry

Signing another person's public key requires you to
  • import it from public key server to your public keyring
  • verify its fingerprint and ID with him (e.g. by phone)
  • verify ID(s) you are signing designate him
  • verify he can sign message corresponding to that public key
Consider whether to put your reputation on line for him

Keys on your public keyring can be signed as followed:
user@USER:$ gpg --edit-key "Harry Lime"
...
[ unknown] (1). Harry Lime (Cuckoo clocks) <H.Lime@cccp.wien.at>
[ unknown] (2) [jpeg image of size 3100]
Command> sign
Really sign all user IDs? (y/N) y
pub 1024D/D8C0D322 created: 2011-06-08 expires: 2011-09-06 usage: SC
trust: unknown validity: unknown
Primary key fingerprint: 4B31 1C50 4331 2B16 05BC C3E9 EF84 7D38 D8C0 D322
Harry Lime (Cuckoo clocks) <H.Lime@cccp.wien.at>
[jpeg image of size 3100]
This key is due to expire on 2011-09-06.
Are you sure that you want to sign this key with your
key "Blogger (How to work with PGP.) <Blogger@blogger.com>" (A5A35B36)
Really sign? (y/N) y
...
Enter passphrase:
...
Command> quit
Save changes? (y/N) y
Individual IDs can be picked with command "uid n". n=0 deselects all.

Once signed
  • export their public key from your keyring
  • upload updated public key back to public key server
Updated public key will be merged with their public key on server.
Display details of other person's public key including signatures with:

user@USER:$ gpg --list-sigs "Harry Lime"

Verify their signatures with public keys in your public key ring by

user@USER:$ gpg --check-sigs "Harry Lime"

Encryption and Decryption

Encrypt doc.pdf to send to Harry Lime as follows:

user@USER:$ gpg --output doc.gpg --encrypt --recipient "Harry Lime" doc.pdf

Harry Lime decrypts doc.gpg sent to him as follows:

user@USER:$ gpg --output doc.pdf --decrypt doc.gpg

Enter passphrase:
GNUPG also supports symmetric encryption using a passphrase.

user@USER:$ gpg --output doc.gpg --symmetric doc.pdf

Enter passphrase:
Passphrase is hashed into symmetric key of the right size.
Symmetrically encrypted message can be decrypted as follows:

user@USER:$ gpg --output doc.pdf --decrypt doc.gpg

Document doc.pdf can be signed and compressed as follows:

user@USER:$ gpg --output doc.sig --sign doc.pdf

Document is signature checked and decompressed by

user@USER:$ gpg --output doc.pdf --decrypt doc.sig

Documents can be signed without compression by

user@USER:$ gpg --clearsign doc.txt
user@USER:$ gpg --output doc.sig --detach-sig doc.txt

Detached signature doc.sig can be verified over doc.txt

user@USER:$ gpg --verify doc.sig doc.txt

Subkeys and signatures on OpenPGP keys can be individually revoked.
Example shows revocation of subkey and self-signature of photo-id:

user@USER:$ gpg --edit-key "Blogger"

pub 1024D/D8E1C459 created: 2011-06-27 expires: never usage: SC
trust: ultimate validity: ultimate
sub 2048g/933254DE created: 2011-06-27 expires: never usage: E
[ultimate] (1). Blogger (How to work with PGP.) <Blogger@blogger.com>
[ultimate] (2) [jpeg image of size 25817]
Command> key 1
...
Command> revkey
...
Command> uid 2
...
Command> revsig
...
Create a revocation certificate for this signature? (y/N) y
...
pub 1024D/D8E1C459 created: 2011-06-27 expires: never usage: SC
trust: ultimate validity: ultimate
This key was revoked on 2011-06-30 by DSA key D8E1C459
Blogger (How to work with PGP.) <Blogger@blogger.com>
sub 2048g/933254DE created: 2011-06-27 revoked: 2011-06-30 usage: E
[ultimate] (1). Blogger (How to work with PGP.) <Blogger@blogger.com>
[ revoked] (2) [jpeg image of size 25817]
New subkey can be added as follows:
Command> addkey
...
Please select what kind of key you want:
...
(4) Elgamal (encrypt only)
...
Your selection? 4
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048) 4096
...
pub 1024D/D8E1C459 created: 2011-06-27 expires: never usage: SCtrust: ultimate validity: ultimate
...
sub 4096g/11E26C5B created: 2011-06-30 expires: never usage: E
Revoked components can be trimmed by delkey and deluid.

However, once own key is published on servers, trimming it is inept.
Adjustments can be made in a key's trust level

user@USER:$ gpg --edit-key Harry

pub 1024D/D8C0D322 created: 2011-06-08 expires: 2011-09-06 usage: SC
trust: unknown validity: full
sub 2048g/9EED759F created: 2011-06-08 expires: 2011-09-06 usage: E
[ full ] (1). Harry Lime (Cuckoo clocks) <H.Lime@cccp.wien.at>
[ full ] (2) [jpeg image of size 3100]
Command> trust
...
Please decide how far you trust this user to verify other users' keys
(by looking at passports, checking fingerprints from various sources etc.)
1 = I don't know or won't say
2 = I do NOT trust
3 = I trust marginally
4 = I trust fully
5 = I trust ultimately
m = back to the main menu
Your decision? 3
pub 1024D/D8C0D322 created: 2011-06-08 expires: 2011-09-06 usage: SC
trust: marginal validity: full
Web of trust parameters can also be altered in .gnupg/gpg.conf to
  • completes-needed 2
  • marginals-needed 5
  • max-cert-depth 3

GNUPG supports interactive maintenance of trust database

user@USER:$ gpg --update-trustdb

This indicates web of trust is built over two keys
  • one is marginally trusted (Harry Lime's)
  • other is ultimately trusted (own public key)
Query pgp.mit.edu or keyserver.pgp.com for public key.Use key ID to import it into your public keyring

user@USER:$ gpg --keyserver pgp.mit.edu --recv-key 4B2700B9

Check out its 22 signatures with

user@USER:$ gpg --list-sigs Ross

Added signatures to it can be republished back to keyserver with

user@USER:$ gpg --keyserver pgp.mit.edu --send-key rja14@cl.cam.ac.uk


Friday, 29 November 2013

Public Key Cryptography

In computer science, public key cryptography is used for following purposes:

  • Encrypt valuable information so that no one can read it.
  • Identify source of publisher i.e. Digital Signatures.
  • Cryptography private keys exchange.
How it works
To do this a key pair is generated i.e. a public key and a private key. Let suppose a user A generated his key pair then A will broadcast his public key. Now any one, let suppose B, who wants to communicate with A can get public key of A. Now they can communicate securely.

Encryption and Decryption Process
Suppose A and B wants to communicate securely. Then A and B will exchange their public keys to each let me explain it with an example:

Lets suppose

  • A's key pair is Pa (Public Key) and Sa (Secret Key)
  • B's key pair is Pb (Public Key) and Sb (Secret Key)
Now A wants to send a message to B such that no one can read it then

A will encrypt the message with public key of B and send it over network. The message payload will be received by B then B decrypt it by his private key. i.e if M is message, Pb is public key of B, Sb is secret or private key of B and C is cipher text which will be send over network then:

  • A:   Encrypt  (M, Pb) = C
  • B:   Decrypt  (C, Sb) = M

Do you know about man in the middle attack? this Encryption and Decryption process is vulnerable to man in middle Attack. For Example:


  • A encrypt message M with public key of B which produce cipher text C and send C to network.
  • An attacker capture the C which was send to B. Attacker know the public key of B then attacker can change the message and encrypt it with public key of B and send it to B. i.e Encrypt (FakeMessage, Pb) = FakeCipher  and send FakeCipher to B.
  • Now when B decrypt it i.e Decrypt  (FakeCipher, Sb) = FakeMessage
What is solution of this such that no one can see message and also source of message is identified too. Answer is use of Digital Signatures.

Digital Signatures 
In public key cryptography digital signatures are widely used for source identification. who they works let me explain it by example:

Suppose A want to send A message to B and Also wants to identify him self then.

  • A will encrypt the message with public key of B. i.e Encrypt  (M, Pb) = C
  • Now A will use any hashing function SHA or MD5 and generate hash of message. i.e SHA (M) = hash. 
  • Now A will encrypt whole package, cipher C and hash, with its own secret key Sa. i.e. Encrypt  (C+hash, Sa) = SignedCipher
  • Now A send SignedCipher over the network.
  • B recive the Signed Cipher and then decrypt it with public key of A this will give B orignal cipher and i.e. Decrypt  (SignedCipher, Pa) = C + hash
  • Then B will decrypt the cipher i.e. Decrypt  (C, Sb) = M1
  • Now B will generate a hash of M1 i.e. SHA (M1)
  • If hash of M1 is equal to hash which was send previously then message is same which was send by A, also source of A is identified.
Please give us your feedback.