Tuesday, May 26, 2020

DEMONSTRATION OF CRYPTOGRAPHY FOR DUMMIES


Sender – User ‘A’
Receiver – User ‘B’
Message to be sent - plaintext.txt

The commands are to be run in Linux Environment. This is a complex process for beginners.  Many online editors are available. This demonstration uses  https://repl.it/ which is a user friendly editor for many languages. It even welcomes you once you open and gives suggestions !! For details of usage refer to the blog " How to use Repl.it to create and run a Linux Program "
To create Linux Environment choose Bash from the options.



 
The representative diagram of the whole process of encryption, decryption, creating a digital signature and verification of digital signature is shown.



 
The following table shows the execution of the above process.
S.No
Action
Command
Output

Step 1 : Initialisation
1
To create a message file plaintext.txt
gedit plaintext.txt

The command when used in Linux a file plaintext.txt is created. In repl.it create file plaintext.txt manually using the Add File option
2.
Make entry into the plaintext.txt file
“ Welcome to the World of BlockChain !”


3
To see the contents of the plaintext.txt file
cat plaintext.txt 
Welcome to the World of BlockChain !
4
OpenSSL contains an implementation of SSL and TLS protocols, meaning that most servers and HTTPS websites use its resources. To check the version of openssl
openssl version

OpenSSL 1.1.1  11 Sep 2018

Step 2: User  A generates Private Key and Public Key
5
To create Private key for user A
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:3 -out Private-Key-A.pem
......................+++++
.................+++++
6
To see the contents of Private-Key-A.pem
cat Private-Key-A.pem
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDiE86YCHZPh3vC
6zOih9hNfS1dyn0pdsL1nRS9PBBQRtGYW9W6bt4oGXgKKeQBa6dFM83xz5x2ZnEC
………………………………………………….
erFHPDeTWBRop5ftndNnAQ7suct8DOPNBL3xPo+J1teklxLYIyCyzdg9LPWJ/4kH
sZ7ndNTmaKg/009llBLTcho=
-----END PRIVATE KEY-----
7
Private-Key-A is encoded with Base 64. To view the actual contents as text
openssl pkey –in Private-Key-A.pem -text
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDiE86YCHZPh3vC/6zOih9hNfS1dyn0pdsL1nRS9PBBQRtGYW9W6bt4oGXgKKeQBa6dFM83xz5x2ZnEC/erFHPDeTWBRop5ftndNnAQ7suct8DOPNBL3xPo+J1teklxLYIyCyzdg9LPWJ/4kH
sZ7ndNTmaKg/009llBLTcho=
-----END PRIVATE KEY-----
8
To generate Public Key of User A from Private Key of User A
openssl pkey -in Private-Key-A.pem -out Public-Key-A.pem -pubout

9
To see the contents of Public-Key-A.pem
cat Public-Key-A.pem
-----BEGIN PUBLIC KEY-----
MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAxsNckiN2sz9Ok4X8P2sD
aT801yjd7jzVLeiZnTnzztEtQCSj8KNqNfblnp7aa85e445JEEJxQ5whBxAC+ExJ
Ib1yuNGGH3+BGlL8rLeXxAXkNunJqvMxi3AA3rSESp/dtutKvuhIsT8tQFEwoihS
Sjn0kYQY/n9EexpfcbZUXWZqN2XEw5cemqcQSjv5iZvkeA9gUAqP7m6MMmYSZJ5c
lTfV4PFpd8wEZ7pSs/EMAiHBI0HACHNkYChhxWDt3pECMforxrTN3hsAPE0RQNwW
RSA8Nd0m/OpKw1OspFktfnCjQdhbabiOrKQqyFhKSPU1luQJe70ODqtWercvivGx
cwIBAw==
-----END PUBLIC KEY-----
10
To view the contents of the Public-Key-A as text
openssl pkey -in Public-Key-A.pem -pubin -text

RSA Public-Key: (2048 bit)
Modulus:
    00:b8:fb:18:92:40:ec:48:5a:6e:c5:36:af:f4:33:
    8c:15:21:0a:df:49:41:44:70:1a:25:b6:e1:29:07:
    de:5b:ef:46:c7:e4:dd:ac:a2:ca:19:ca:ba:f7:32:
    62:a2:bf:1f:e0:2f:01:dd:27:9c:6e:cd:10:d2:2d:
…………………………………………………
    e1:3f:c8:79:f1:90:1d:2b:17:e7:a0:56:8e:05:6f:
    9d:46:b8:dc:33:85:3a:64:38:7b:81:29:9b:24:af:
    59:50:00:13:f2:31:c9:ea:a9:80:0a:8c:33:80:c2:
    97:97
Exponent: 3 (0x3)

Step 3: User  B generates Private Key and Public Key
5
To create Private key for user B
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -pkeyopt rsa_keygen_pubexp:3 -out Private-Key-B.pem
......................+++++
.................+++++
6
To see the contents of Private-Key-B.pem
cat Private-Key-B.pem
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCneC6rqk
XZPCMp
KxDJR0MOHrtyoZyQr2Cw9JVxQ55Kd2lylAM51gD8flTRGAnEzBK2PMiUrz
Zv4zwq
………………………………………………
XkjPQ2MAKLvheObkRQzu8k14sp4xS1oCiE368I/KmHrfKjRhPKeOPJlvNGUHAonS
VHIOtxIqGir6xgOQqyWzig==
-----END PRIVATE KEY-----
7
Private-Key-B is encoded with Base 64. To view the actual contents as text
openssl pkey –in Private-Key-B.pem -text
-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDiE86YCHZPh3vC/6zOih9hNfS1dyn0pdsL1nRS9PBBQRtGYW9W6bt4oGXgKKeQBa6dFM83xz5x2ZnEC/erFHPDeTWBRop5ftndNnAQ7suct8DOPNBL3xPo+J1teklxLYIyCyzdg9LPWJ/4kH
sZ7ndNTmaKg/009llBLTcho=
-----END PRIVATE KEY-----
8
To generate Public Key of User B from Private Key of User B
openssl pkey -in Private-Key-B.pem -out Public-Key-A.pem -pubout

9
To see the contents of Public-Key-B.pem
cat Public-Key-B.pem
-----BEGIN PUBLIC KEY-----
MIIBIDANBgkqhkiG9w0BAQEFAAOCAQ0AMIIBCAKCAQEAp3guq6pF2TwjKSsQyUdD
Dh67cqGckK9gsPSVcUOeSndpcpQDOdYA/H5U0RgJxMwStjzIlK82b+M8KtlA5NnZ
DuSBNe/ZBVDmAW/abKI3dX+uxMRHOpxCXeKqcNBZuo1z/axnnxH6hK1fVcJ2jKXQ
CnZF/Y2yszpQPC2ATOvBKtNRwtYm6Y1YRJWOWvIkNPOX8OH5Q76qwKy1L3UJC3fo
+lH+dYcnmVXYr0wxT3hBNDw2Eq9uH+09d1yGs/Co2ZTw/uJ0YMTR0cUQ0r4UJNhu
LSiS/WenQvu6Jdaz5Q/uT4fnvq2iJwiZ5wZw41V95maJTbQQd3nA6TWM1nxYCEJF
KwIBAw==
-----END PUBLIC KEY-----
10
To view the contents of the Public-Key-B as text
openssl pkey -in Public-Key-B.pem -pubin -text

RSA Public-Key: (2048 bit)
Modulus:
    00:a7:78:2e:ab:aa:45:d9:3c:23:29:2b:10:c9:47:
    43:0e:1e:bb:72:a1:9c:90:af:60:b0:f4:95:71:43:
    9e:4a:77:69:72:94:03:39:d6:00:fc:7e:54:d1:18:
    09:c4:cc:12:b6:3c:c8:94:af:36:6f:e3:3c:2a:d9:
    …………………………………………..
    92:fd:67:a7:42:fb:ba:25:d6:b3:e5:0f:ee:4f:87:
    e7:be:ad:a2:27:08:99:e7:06:70:e3:55:7d:e6:66:
    89:4d:b4:10:77:79:c0:e9:35:8c:d6:7c:58:08:42:
    45:2b
Exponent: 3 (0x3)
11
To see the list of files created
ls -l
total 24
-rw-r--r-- 1 runner runner  508 May 26 17:40 main.sh
-rw-r--r-- 1 runner runner   36 May 26 15:22 plaintext.txt
-rw------- 1 runner runner 1704 May 26 17:40 Private-Key-A.pem
-rw------- 1 runner runner 1704 May 26 17:40 Private-Key-B.pem
-rw-r--r-- 1 runner runner  451 May 26 17:40 Public-Key-A.pem
-rw-r--r-- 1 runner runner  451 May 26 17:40 Public-Key-B.pem

Step 4: To Calculate the hash
12
To generate the hash or message digest of plaintext.txt using sha1
openssl dgst -sha1 plaintext.txt
SHA1(plaintext.txt)= c0a752f24f855f119eb04cf2c7b849ff9d57141a
13
To generate the hash or message digest of plaintext.txt using sha256

** 512 can be used in a similar manner
openssl dgst –sha256 plaintext.txt
SHA256(plaintext.txt)= 2ffdd7ce23516ef4b58fc5ccf66500d715b9741e0449096b53df85ec6fc81000

Step 5: To Calculate the hash and sign the message
14
To generate the hash and digital signature "Signature.bin"of the document "plaintext.txt" with private key of A
openssl dgst -sha1 -sign Private-Key-A.pem -out Signature.bin plaintext.txt

 A file Signature.bin is created
15
To check the list of files
ls -l
-rw-r--r-- 1 runner runner  656 May 26 18:45 main.sh
-rw-r--r-- 1 runner runner   36 May 26 15:22 plaintext.txt
-rw------- 1 runner runner 1704 May 26 18:45 Private-Key-A.pem
-rw------- 1 runner runner 1704 May 26 18:45 Private-Key-B.pem
-rw-r--r-- 1 runner runner  451 May 26 18:45 Public-Key-A.pem
-rw-r--r-- 1 runner runner  451 May 26 18:45 Public-Key-B.pem
-rw-r--r-- 1 runner runner  256 May 26 18:45 Signature.bin

Step 6: To encrypt the message using RSA, Using the Public Key of B
16
Encrypt "plaintext.txt" to get ciphertext "ciphertext.bin" using public key of A
openssl pkeyutl -encrypt -in plaintext.txt -pubin -inkey Public-Key-B.pem -out ciphertext.bin
A file ciphertext.bin is created
17
To check the contents of ciphertext.bin
cat ciphertext.bin
x��FzS.SpniXFK_}]Ψ~e0H0ѬӤ47ɦL3gi9A@B0df# y-))Doo])ܬS9vlKN]K_/tcQvO29'/>-?6cqe^I>VfvMgKt@g4V'+TSXӨzG
What we have done so far !!


Step 7: To decrypt the received ciphertext, Using the Private Key of B
18
To decrypt the ciphertext "ciphertext.bin" using private key of B(receiver)
openssl pkeyutl -decrypt -in ciphertext.bin -inkey Private-Key-B.pem -out receivedtext.bin

The decrypted file receivedtext.bin is created
19
To check the contents of the received file
cat receivedtext.bin

Welcome to the World of BlockChain !

Step 7: To verify the Digital Signature
20
To verify the digital signature of receivedtext.bin using the public Key of A

openssl dgst -sha1 -verify Public-Key-A.pem -signature Signature.bin receivedtext.bin
Verified OK !

 The overall appearance in  https://repl.it

Wow !! Cryptography Simplified !!

3 comments:

  1. I will try this and let you know if I face any issues. I hard a gard time reading the slides. is it just me or the font is too small.

    ReplyDelete
    Replies
    1. Thank You Tim for the feedback. I have increased the size of the images to actual size now. But the appearance in the preview is not good. Please bear with me as I am still struggling with formatting in Blogger.

      Delete

The Winning Combo – Cryptocurrency and Block Chain

  The Winning Combo – Cryptocurrency and Block Chain ABSTRACT: Commercial transactions on the Internet have come to rely exclusively on ...