Lecture6_Cryptographic-Algorithms
Lecture6_Cryptographic-Algorithms
FACULTY OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Let K be a key and enc and dec a encryption function such that+
dec = enc-1
then
Symmetric Encryption
Secure communication has been an essential human need for a very long time. For example, the
ancient Greeks used a device in 400 BC to hide and reveal military messages to protect them
from enemy armies. The method of changing a message to hide or reveal its meaning is called a
cipher. Ciphers are used to encrypt (hide) a message or decrypt (reveal) a message using an
algorithm (a sequence of defined steps). It is often very hard to decrypt a message without
knowing what cipher was used.
Symmetric-key algorithms are algorithms for cryptography that use the same cryptographic keys
for both the encryption of plaintext and the decryption of ciphertext.
The two types of traditional symmetric ciphers are transposition cipher and substitution
cipher.
A transposition cipher encrypts a word by changing the order of its letters. One order could be to
remove the letters from even positions and add them to the end of the word. For example:
PINEAPPLE is encrypted as PNAPEIEPL.
In a substitution cipher, any character of plain text from the given fixed set of characters is
substituted by some other character from the same set depending on a key. For example, with a
shift of 1, A would be replaced by B, B would become C, and so on. Caesar cipher and linear
cipher are two popular substitution ciphers.
Caesar cipher
A Caesar cipher encrypts a word by replacing each letter with another letter in the alphabet,
usually in a pattern. For example, letter A is replaced by B, letter B is replaced by C, etc. (letter
Z is replaced by A). In other words, this Caesar cipher rotates each letter in the alphabet to the
right by 1 position. For example: PINEAPPLE is encrypted as QJOFBQQMF.
The table below shows mapping of letters while using a Caesar cipher rotated by 1 position.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
B C D E F G H I J K L M N O P Q R S T U V W X Y Z A
The rotation can, of course, be larger than 1. The most popular rotation value is 3. If the alphabet
is rotated right by three positions, then letter A is replaced by D, letter B is replaced by E, etc.
For example: PINEAPPLE is encrypted as SLQHDSSOH (with a rotation of 3).
The table below shows the mapping of letters when using a Caesar cipher with a rotation of 3.
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
Linear Cipher
In the encryption process, this technique uses the linear function y=ax+b, which is known as the
equation of the line in geometry. Accordingly, x denotes the message to be encrypted (plain text),
y denotes the encrypted message (cipher text), and the pair a and b form the key.
Creating the encrypted message: As b is the 2nd letter in the alphabet, it is replaced by 3×2 +2 =
8, that is, the 8th letter of the alphabet. This letter corresponds to 'h'. It is calculated similarly for
other letters. For example, the letter 'e' is replaced by 3×5 +2 = 17, that is, the letter 'q'.
Elliptic functions
⚫ based on elliptic functions and finite fields
⚫ typical key length: 160 bit
1. Compute n as the product of two very large, freely chosen prime numbers p and q:
n=p*q
2. Compute φ(n) = (p - 1) * (q - 1)
4. Compute a value for decryption key d such that (d * e) % φ(n) = 1. The % sign refers to
the modulo operation2.
1
Co-prime means that e should not multiply by factors of φ(n) and also not divide by φ(n), e.g., e = 18 and φ(n) = 9
are not coprime as 18 is dividable by 9. In contrast, 18 and 21 are coprime as their greatest common divisor is 1.
2
The modulo operation returns the remainder of a division, after one number is divided by another, e.g. 8 % 3 = 2
CEN 345 Algorithms 6 Assoc. Prof. Dr. Fatih ABUT
ÇUKUROVA UNIVERSITY
FACULTY OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
c = me % n
2. The decryption of c is
m = cd % n
1. Compute n = p * q = 3 * 11 = 33
2. Compute φ(n) = (p - 1) * (q - 1) = 2 * 10 = 20
3. Choose, e.g., the encryption key e = 7 that satisfies the following two conditions:
o 1 < e = 7 < φ(n) = 20 and
o e = 7 and φ (n) = 20 are coprime.
4. Compute a value for the decryption key d that satisfies the following condition:
o (d * e) % φ(n) = 1.
o One solution is d = 3 because (3 * 7) % 20 = 1
Let, e.g., m = 2 be the clear text to be encrypted with the public key (e, n) => (7, 33).
Hash Functions
One of the most essential objectives of cryptography is to ensure the integrity of messages.
Integrity protection is the ability to determine that a message in a communication has not been
modified or tampered with between the creator and the viewer by an unauthorized user (i.e.,
attacker). The integrity of data can be protected using hash functions. Hash functions generate a
fixed-length hash value from a data record of arbitrary length. A data record can be a word, a
sentence, a long piece of text, or an entire file. The hash value generated is often referred to as a
digital fingerprint or cryptographic checksum, which is characteristic for the input data. The hash
value is generated by a formula in such a way that it is extremely unlikely that some other text will
generate the same hash value.
Four heuristic hash functions are division hashing, mid-square hashing, digit folding hashing
and multiplicative hashing, which operate on numeric or alphanumeric keys/messages.
Division Hashing
This is the most simple and easiest method to generate a hash value. The hash function divides the
value k by M and then uses the remainder obtained.
h(K) = k mod M
Here,
k is the key value, and
M is the size of the hash table.
It is best suited that M is a prime number as that can make sure the keys are more uniformly
distributed. The hash function is dependent upon the remainder of a division.
Example #1:
k = 12345
M = 95
h(12345) = 12345 mod 95 = 90
Example #2:
k = 1276
M = 11
h(1276) = 1276 mod 11 = 0
Mid-Square Hashing
The mid-square hashing involves two steps to compute the hash value:
Formula:
h(k) = h(k * k)
Here,
k is the key value.
The value of r can be decided based on the size of the table.
CEN 345 Algorithms 9 Assoc. Prof. Dr. Fatih ABUT
ÇUKUROVA UNIVERSITY
FACULTY OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
Example:
Suppose the hash table has 100 memory locations. So, r = 2 because two digits are required to
map the key to the memory location.
k = 60
k * k = 60 * 60 = 3600
h(60) = 60
Pros:
1. The performance of this method is good as most or all digits of the key value
contribute to the result. This is because all digits in the key contribute to generating
the middle digits of the squared result.
2. The result is not dominated by the distribution of the top digit or bottom digit of the
original key value.
Cons:
1. The size of the key is one of the limitations of this method, as the key is of big size
then its square will double the number of digits.
2. Another disadvantage is that there will be collisions but we can try to reduce
collisions.
1. Divide the key-value k into a number of parts, i.e., k1, k2, k3,….,kn, where each part
has the same number of digits except for the last part that can have lesser digits than
the other parts.
2. Add the individual parts. The hash value is obtained by ignoring the last carry if
any.
Formula:
k = k1, k2, k3, k4, ….., kn
s = k1+ k2 + k3 + k4 +….+ kn
h(k)= s
Example:
k = 12345
k1 = 12, k2 = 34, k3 = 5
s = k1 + k2 + k3
= 12 + 34 + 5
= 51
h(k) = 51
Note:
The number of digits in each part varies depending upon the size of the hash table. Suppose for
example the size of the hash table is 100, then each part must have two digits except for the last
part which can have a lesser number of digits.
Multiplicative Hashing
This method involves the following steps:
Formula:
h(k) = floor (M * (k * c mod 1))
Here,
M is the size of the hash table.
k is the key value.
c is a constant value.
Example:
k = 12345
c = 0.357840
M = 100
Pros:
The advantage of the multiplication method is that it can work with any value between 0 and 1,
although there are some values that tend to give better results than the rest.
Cons:
The multiplication method is generally suitable when the table size is the power of two, then
the whole process of computing the index by the key using multiplication hashing is very fast.
The digital or electronic signature is a checksum that is generated from a message in combination
with a key. Digital signatures are used for digital messages to make their authenticity credible and
verifiable. The digital signature is attached to the message, and the authenticity of the signature
can be checked electronically.
Digital signatures are necessary for data transmission because the sender of messages can be
forged. For example, it is very easy to fake the sender of an email. That is, it is possible for someone
to impersonate someone else. Also, in real life, you can write any sender address on a letter. To
underline the credibility of the letter, we put our signature at the end of the letter. The same is done
with the digital signature.
Earlier, we learned that a hashing function is a technique that verifies the integrity of a message
(i.e., hashing code calculated by a hashing function reveals whether the originally sent message
has been manipulated by an attacker). Furthermore, we also learned the concept of asymmetric
encryption, where we use two different keys, namely a public key to encrypt a message and a
private key to decrypt the encrypted message. For signing a message, we will need to combine the
hashing function and asymmetric encryption concept.
CEN 345 Algorithms 13 Assoc. Prof. Dr. Fatih ABUT
ÇUKUROVA UNIVERSITY
FACULTY OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
To sign a message, the sender, Alice, first creates the hash code of the message (e.g., using the
division hashing). Then, Alice creates the signature by encrypting the hash code with her private
key and sends this signature to Bob. Finally, Bob verifies the signature by using the public key of
Alice. Note that, differently from the classical asymmetric encryption, we reverse the use of the
public and private keys. We’re going to encrypt with the private key and see if we can decrypt
with the public key.
Let be (e, n) and (d,n) the public and private keys, respectively. The hash code c of the message
m with h(m) = c can be signed and verified as follows.
s = cd % n
c = se % n
Example: Let, e.g., the hash code of a message be c=2; (e, n) => (7, 33) the public key; and (d, n)
=> (3, 33) the private key.
The advantage of asymmetric encryption is that the key can be exchanged without any problems
since the public key of a user is not secret and can be distributed to all participants without
hesitation. The disadvantage of asymmetric encryption is that it is slower than symmetric in terms
of performance.
Hybrid encryption was developed to combine the advantages of both encryption techniques: the
speed of symmetric encryption and the simplicity of key exchange of asymmetric encryption. More
specifically, a symmetric key is created for quick encryption of the data. Then, to securely
exchange this symmetric key with the partner, the symmetric key is asymmetrically encrypted with
CEN 345 Algorithms 16 Assoc. Prof. Dr. Fatih ABUT
ÇUKUROVA UNIVERSITY
FACULTY OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
the receiver’s public key and sent to the receiver. Upon arrival, the receiver can decrypt the
symmetric key with his/her private key and then start to use the symmetric key.
In the following, the steps of the Diffie-Hellman key exchange algorithm are listed:
1. Alice and Bob agree on a large public prime number p and a smaller number g
2. Simultaneously, Alice and Bob choose the random numbers a and b, respectively. These
are secret keys that will never be exchanged between the communication partners, i.e.,
these random numbers do not leave the local machines of Alice and Bob.
3. Using the random numbers a and b as well as the two previously agreed numbers p and g,
Alice and Bob calculate the new numbers A and B, respectively, as follows:
Alice computes A = ga % p
Bob computes B = gb % p
5. Alice and Bob calculate the common key k using their random numbers a and b as well as
the mutually exchanged public numbers A and B as follows:
Alice computes k = Ba % p
Bob computes k = Ab % p
3
The modulo operation returns the remainder of a division, after one number is divided by another, e.g. 8 % 3 = 2
CEN 345 Algorithms 19 Assoc. Prof. Dr. Fatih ABUT
ÇUKUROVA UNIVERSITY
FACULTY OF ENGINEERING
COMPUTER ENGINEERING DEPARTMENT
6. Alice and Bob use the key k to encrypt and decrypt the data
Example:
3. Using the random numbers a and b as well as the two previously agreed numbers p and g,
a. Alice computes A = ga % p = 37 % 5 = 2
b. Bob computes B = gb % p = 39 % 5 = 3
Alice computes k = Ba % p = 37 % 5 = 2
Bob computes k = Ab % p = 29 % 5 = 2
6. Alice and Bob use the key k = 2 to encrypt and decrypt the data
Homework:
You are given three single-word passwords encrypted using the ciphers presented in this lecture.
To be more secure, some passwords are encrypted using more than one cipher. Your task is to
determine which ciphers were used and decrypt the passwords.
RSA and Digital Signature: Let be p = 17 and q = 11. Also, assume that RSA chooses e = 7 and
d = 23 as encryption and decryption keys, respectively. Given these values of p, q, e, and d,
a) calculate your public and private keys using RSA.
b) Then, assume you want to exchange your clear text message m = 5 securely. Encrypt m
with your public key and decrypt the encrypted text again with your private key.
c) Sign the message m=5 with the private key and verify it with the corresponding public key.
Diffie Hellman Algorithm: Let be p = 11 and g = 5. Also, assume that Alice and Bob choose a =
7 and b = 3, respectively. Given these values of p, g, a, and b, calculate the shared session secret
for Alice and Bob.
Hash Functions: In order to calculate the numerical representation of m, we can use the table
below, which shows the mapping between letters and their numerical codes. We can just sum up
the codes of each character for a given input string to obtain its numerical representation. For
example: HELLO = 8 + 5 + 12 + 12 + 15 = 52
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
As shown in the table below, you are given four single-word messages and the corresponding
(sent) hash values (p and c values are set to 31 and 0.56, respectively). Some messages were
manipulated by an unauthorized user. Your task is to verify the sent hash values of each message
to determine which messages are manipulated.
Message
Hash Sent Hash
Message Calculated Hash Value Verified /
function Value
Manipulated
Division
MORNING 28
Hashing
Division
REGARDS 30
Hashing
Multiplicative
SECURITY 13
Hashing
Multiplicative
HASHING 29
Hashing