You are on page 1of 80

Module2- Cryptography and Smart Contracts

• Symmetric cryptography (DES, AES), Asymmetric


cryptography, Public and Private keys, Algorithms - RSA,
Hash functions, SHA, SHA-256, ECDSA

• Smart contracts - Benefits of Smart contracts, Solidity


programming-Types, Literals, Enums, Function types,
Reference types, mappings, Global variables, Control
structures (Events, Inheritance, Libraries, Functions),
Compile, verify and Deploy.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Cryptography
• Cryptography is the science of secret, or hidden writing
• It has two main Components:
1. Encryption
– Practice of hiding messages so that they can not be read by anyone
other than the intended recipient
2. Authentication
– Ensuring that users of data/resources are the persons they claim to be
and that a message has not been surreptitiously altered

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Symmetric-key cryptography

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Note

In symmetric-key cryptography, the same key is used


by the sender(for encryption) and the receiver (for
decryption).
The key is shared.
Algorithm: DES,3DES, AES

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
How DES works and Key Generation

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed
ledger & 19CS3618
Department of Computer Science and
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed
ledger & 19CS3618
Department of Computer Science and
Round Function of DES

Introduction to Blockchain and distributed


ledger & 19CS3618
Department of Computer Science and
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
DES: Data Encryption Standard
• Adopted in 1977 by National Bureau of
Standards (now NIST)
• Divides message into blocks of 64 bits, and
uses a key of 56 bits
• Key idea for this: XOR the data with the key
– (Remember XOR? How did it work?)

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
DES
• In July 1998, DES was officially cracked by a machine
built by the EFF
– Total cost: under $250,000
– Total time: 6-8 months
• They then published the details of their approach,
which essentially was a brute force attack
• Note: 56 bits means 256 keys to try.
• Also, not as easy as just trying. What do you always
do to files before sending them somewhere?

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Modes of Operation

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Cipher Block chaining

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Cipher Feedback(CFB)

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Live Example

• https://www.tools4noobs.com.
• https://www.tools4noobs.com/online_tools/e
ncrypt/

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
3DES
• Effort to salvage DES
• Main algorithm: repeat DES 3 times with
different keys (so key size is now 168 bits)
• Still very secure - brute force attacks would
take too long, and that is the only way to
attack this algorithm
• Main problem: SLOW

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Advanced Encryption Standard
(AES)
• Designed in response to a call by NIST in 1998, and
officially adopted in 2001
• Block length is 128 bits, and keys can be 128, 192, or
256 bits.
• Essentially, proceeds in 4 rounds (which are
repeated):
– Substitute bytes
– Permute
– Mix columns
– Add round key

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Advanced Encryption Standard
• Block size –128 bit.

• Number of rounds 10 Rounds

• Key Size – 128 bits( Key will be processed in terms of words).

• Number of Sub keys → 44

• Each sub keys size is → 32bits

• Each Round → 4 keys.

• Before starting rounds we should do 4 more rounds.

• Pre-round calculation → 4 sub keys.

• Cipher text →128 bits.


Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
AES Block diagram

Substitute Byte:- S- Box


Shift Rows:- Circular Right Shift Operation.
Mix Columns:- Multiple the pre-defined matrix.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Stage 1: substitute bytes
• AES computes a matrix which maps every 8-bit value
to a different 8-bit value.
• Computed using properties of finite fields (go take
some math classes to learn more about this).

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Stage 2: permute
• AES then shifts each row, where each row is shifted a
different amount.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Stage 3: Mix columns
• Here, the 4 bytes in each column are
combined using a linear transformation
• Essentially, the output of any byte depends on
all the input bytes, so this “mixes” them
together

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Stage 4: Add round key
• Use XOR to combine the key with the message

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Expansion Keys

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Symmetric-key cryptography
• Advantages:
– Simple
– Faster
• Disadvantages:
– Key must exchanges in secure way
– Easy for hacker to get a key as it is passed in
unsecure way.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Figure 30.4 Asymmetric-key cryptography

Introduction to Blockchain and distributed


ledger & 19CS3618
Department of Computer Science and
Note

An asymmetric-key (or public-key) cipher uses two


keys: one private (To encrypt data) and one public(To
decrypt data).

Introduction to Blockchain and distributed


ledger & 19CS3618
Department of Computer Science and
• Asymmetric Key Cryptography (Public Key Cryptography)
– 2 different keys are used
– Users get the Key from an Certificate Authority

Advantages
1. More Secured
2. Authentication

Disadvantages
1. Relatively Complex

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Figure 30.6 Comparison between two categories of cryptography

Introduction to Blockchain and distributed


ledger & 19CS3618
Department of Computer Science and
• Asymmetric encryption use two keys, one to encrypt the
data, and another key to decrypt the data.
• These keys are generated together
• One is named as Public key and is distributed freely. The other
is named as Private Key and it is kept hidden.
• Both Sender & Recipient has to share their Public Keys for
Encryption and has to use their Private Keys for Decryption.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
How it WORKS…….?

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Key Points in Asymmetric Encryption

❖ Asymmetric encryption use two keys:

Public Key - to encrypt the data

Private Key - to decrypt the data

❖ These keys are generated together.

❖ The Public key(s) is distributed freely between the sender and receiver.

❖ The other is named as Private Key and it is kept hidden.

❖ The Private Key is only used for Decryption and will not be shared
between the sender and receiver.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
What is Asymmetric Encryption

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Asymmetric Encryption Algorithms

❖RSA:
❖Digital Signature Algorithm:
❖Diffie-Helman:
❖ SHA, SHA-256:

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
RSA Algorithm
• Invented in 1978 by Ron Rivest, Adi Shamir and Leonard Adleman.

– Published as R L Rivest, A Shamir, L Adleman, "On Digital


Signatures and Public Key Cryptosystems", Communications of
the ACM, vol 21 no 2, pp120-126, Feb 1978.

• Security relies on the difficulty of factoring large composite


numbers.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
RSA Public Key Crypto System
Key generation:

1. Select 2 large prime numbers of about the same size, p and q

Typically each p, q has between 512 and 2048 bits

2. Compute n = pq, and (n) = (q-1)(p-1)

3. Select e, 1<e< (n), s.t. gcd(e, (n)) = 1

Typically e=3 or e=65537

4. Compute d, 1< d< (n) s.t. ed  1 mod (n)

Knowing (n), d easy to compute.

Public key: (e, n)


Private key: d
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
RSA Description (cont.)
Encryption
Given a message M, 0 < M < n M  Zn− {0}
use public key (e, n)
compute C = Me mod n C  Zn− {0}

Decryption
Given a ciphertext C, use private key (d)
Compute Cd mod n = (Me mod n)d mod n = Med mod n = M

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
C = Me mod (n=pq)

Plaintext: M Ciphertext: C
Cd mod n

From n, difficult to figure out p,q


From (n,e), difficult to figure d.
From (n,e) and C, difficult to figure out M s.t. C = Me

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
RSA Example
• p = 11, q = 7, n = 77, (n) = 60
• d = 13, e = 37 (ed = 481; ed mod 60 = 1)
• Let M = 15. Then C  Me mod n
– C  1537 (mod 77) = 71
• M  Cd mod n
– M  7113 (mod 77) = 15

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
RSA Example 2
• Parameters:
– p = 3, q = 5, pq = 15
– (n) = ?
• Let e = 3, what is d?
• Given M=2, what is C?
• How to decrypt?

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
RSA Example
• p=11, q=19.
• p=7, q=13.
• p=17 q=11.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Hash Functions
• Hashing algorithm is a hash function that creates unique finger print of
digital data from arbitrary size to a fixed size value.

• The values returned by a hash function are called hash values, hash codes,
digests or simply hashes.

• Its designed to be one-way function infeasible to invert.

• It is used for data comparing to check the integrity.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Hash Functions
Cryptographic hash functions are hash functions with additional
security requirements

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Usages of Cryptographic Hash Functions
• Software integrity
– E.g., tripwire

• Time stamping
– How to prove that you have discovered a secret on an
earlier date without disclosing it?

• Covered later
– Message authentication
– One-time passwords
– Digital signature
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Well Known Hash Functions
• MD5
– output 128 bits
– collision resistance completely broken by researchers in China in 2004

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Secure Hash Algorithm(SHA)
• Developed by NIST, Specified in the secure hash standard,
originally 1993.

• Revised as SHA1 in 1995- 160 bit hash.

• NIST Specified SHA2 algorithm in 2002.

1. Hash value lengths of 256, 384 and 512.

2. Similar to SHA1.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Merkle-Tree Construction for Hash Functions
• Message is divided into fixed-size blocks and padded.
• Uses a compression function f, which takes a chaining variable (of size of
hash output) and a message block, and outputs the next chaining variable.
• Final chaining variable is the hash value.

M=m1m2…mn; C0=IV, Ci+1=f(Ci,mi); H(M)=Cn


Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
MERITS & DE-MERITS

Merits:
❖ Two parties don't need to have their private keys already shared in order to
communicate using encryption.
❖ Authentication and Non-Repudiation are possible. (Authentication means that you can
encrypt the message with my public key and only I can decrypt it with my private key.
Non-repudiation means that you can "sign" the message with your private key and I can
verify that it came from you with your public key.)

De-Merits:
❖Asymmetric Encryption algorithms are comparatively complex.
❖Time consuming process for Encryption and Decryption.

Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering
Introduction to Blockchain and distributed ledger & 19CS3618 Department of Computer Science and Engineering

You might also like