You are on page 1of 48

Advanced cryptography

RSA algorithm
RSA Algorithm is used to encrypt and decrypt data in modern computer
systems and other electronic devices. RSA algorithm is an asymmetric
cryptographic algorithm as it creates 2 different keys for the purpose of
encryption and decryption. It is public key cryptography as one of the keys
involved is made public.
RSA makes use of prime numbers (arbitrary large numbers) to function. The
public key is made available publicly (means to everyone) and only the person
having the private key with them can decrypt the original message.
Working with RSA algorithm
RSA involves use of public and private key for its operation. The keys are
generated using the following steps:-
• Two prime numbers are selected as p and q
• n = pq which is the modulus of both the keys.
• Calculate totient = (p-1)(q-1)
• Choose e such that e > 1 and coprime to totient which means gcd (e,
totient) must be equal to 1, e is the public key
• Choose d such that it satisfies the equation de = 1 + k (totient), d is the
private key not known to everyone.
• Cipher text is calculated using the equation c = m^e mod n where m is the
message.
• With the help of c and d we decrypt message using equation m = c^d mod
n where d is the private key.
Coprime numbers
When two numbers have no common factors other than 1.
In other words there is no whole number that you could divide them both
by exactly (without any remainder).
21 and 22 are coprime:
• The factors of 21 are 1, 3, 7 and 21
• The factors of 22 are 1, 2, 11 and 22
(the only common factor is 1)
Program for RSA Algorithm in C
//Program for RSA asymmetric cryptographic algorithm
//for demonstration values are relatively small compared to practical application
#include<stdio.h>
#include<math.h>
//to find gcd
int gcd(int a, int h)
{
int temp;
while(1)
{
temp = a%h;
if(temp==0)
return h;
a = h;
h = temp;
}
}
Program for RSA Algorithm in C ..
int main()
{
//2 random prime numbers
double p = 3;
double q = 7;
double n=p*q;
double count;
double totient = (p-1)*(q-1);

//public key
//e stands for encrypt
double e=2;

//for checking co-prime which satisfies e>1


while(e<totient){
count = gcd(e,totient);
if(count==1)
break;
else
e++;
}
Program for RSA Algorithm in C …
//private key
//d stands for decrypt
double d;

//k can be any arbitrary value


double k = 2;

//choosing d such that it satisfies d*e = 1 + k * totient


d = (1 + (k*totient))/e;
double msg = 12;
double c = pow(msg,e);
double m = pow(c,d);
c=fmod(c,n);
m=fmod(m,n);
Program for RSA Algorithm in C …
printf("Message data = %lf",msg);
printf("\np = %lf",p);
printf("\nq = %lf",q);
printf("\nn = pq = %lf",n);
printf("\ntotient = %lf",totient);
printf("\ne = %lf",e);
printf("\nd = %lf",d);
printf("\nEncrypted data = %lf",c);
printf("\nOriginal Message Sent = %lf",m);

return 0;
}
Vigenere Cipher
Vigenere Cipher is kind of polyalphabetic substitution method. It is used for
encryption of alphabetic text. For encryption and decryption Vigenere Cipher
Table is used in which alphabets from A to Z are written in 26 rows.
Vigenere Cipher Table
Vigenere Cipher Encryption
Message Text: THECRAZYPROGRAMMER
Key: HELLO
Here we have to obtain a new key by repeating the given key till its length
become equal to original message length.
New Generated Key: HELLOHELLOHELLOHEL
For encryption take first letter of message and new key i.e. T and H. Take the
alphabet in Vigenere Cipher Table where T row and H column coincides i.e.
A.
Repeat the same process for all remaining alphabets in message text. Finally
the encrypted message text is:
Encrypted Message: ALPNFHDJAFVKCLATIC
Vigenere algorithm
The algorithm can be expressed in algebraic form as given below. The cipher
text can be generated by below equation.
• Ei = (Pi + Ki) mod 26
Here P is plain text and K is key.
Vigenere Cipher Decryption
• Encrypted Message: ALPNFHDJAFVKCLATIC
• Key: HELLO
• New Generated Key: HELLOHELLOHELLOHEL
Take first alphabet of encrypted message and generated key i.e. A and H.
Analyze Vigenere Cipher Table, look for alphabet A in column H, the
corresponding row will be the first alphabet of original message i.e. T.
Repeat the same process for all the alphabets in encrypted message.
• Original Message: THECRAZYPROGRAMMER
Above process can be represented in algebraic form by following equation.
• Pi = (Ei – Ki + 26) mod 26
Program for Vigenere Cipher in C
#include<stdio.h>
#include<string.h>

int main(){
char msg[] = "THECRAZYPROGRAMMER";
char key[] = "HELLO";
int msgLen = strlen(msg), keyLen = strlen(key), i, j;

char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen];

//generating new key


for(i = 0, j = 0; i < msgLen; ++i, ++j){
if(j == keyLen)
j = 0;

newKey[i] = key[j];
}

newKey[i] = “\0”;
Program for Vigenere Cipher in C ..
//encryption
for(i = 0; i < msgLen; ++i)
encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A';

encryptedMsg[i] = '\0';

//decryption
for(i = 0; i < msgLen; ++i)
decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A';

decryptedMsg[i] = '\0';

printf("Original Message: %s", msg);


printf("\nKey: %s", key);
printf("\nNew Generated Key: %s", newKey);
printf("\nEncrypted Message: %s", encryptedMsg);
printf("\nDecrypted Message: %s", decryptedMsg);

return 0;
}
What is Hill Cipher?
In cryptography (field related to encryption-decryption) hill cipher is a
polygraphic cipher based on linear algebra. Invented by Lester S. Hill in 1929
and thus got it’s name. It was the first cipher that was able to operate on 3
symbols at once.
• Encryption: The given message string and key string is represented in the
form of matrix. Then key and message matrix are multiplied. Finally modulo
26 is taken for each element of matrix obtained by multiplication. The key
matrix that we take here should be invertible, otherwise decryption will not
be possible.
• Decryption: The encrypted message matrix is multiplied by the inverse of
key matrix and finally its modulo 26 is taken to get the original message.
Hill Cipher Program in C
#include<stdio.h>
#include<math.h>

float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];

void encryption(); //encrypts the message


void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix

void main() {
getKeyMessage();
encryption();
decryption();
}
Hill Cipher Program in C
void encryption() {
int i, j, k;

for(i = 0; i < 3; i++)


for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];

printf("\nEncrypted string is: ");


for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(encrypt[i][0], 26) + 97));

}
Hill Cipher Program in C
void decryption() {
int i, j, k;

inverse();

for(i = 0; i < 3; i++)


for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];

printf("\nDecrypted string is: ");


for(i = 0; i < 3; i++)
printf("%c", (char)(fmod(decrypt[i][0], 26) + 97));

printf("\n");
}
Hill Cipher Program in C
void getKeyMessage() {
int i, j;
char msg[3];

printf("Enter 3x3 matrix for key (It should be inversible):\n");

for(i = 0; i < 3; i++)


for(j = 0; j < 3; j++) {
scanf("%f", &a[i][j]);
c[i][j] = a[i][j];
}

printf("\nEnter a 3 letter string: ");


scanf("%s", msg);

for(i = 0; i < 3; i++)


mes[i][0] = msg[i] - 97;
}
Hill Cipher Program in C
void inverse() {
int i, j, k;
float p, q;

for(i = 0; i < 3; i++)


for(j = 0; j < 3; j++) {
if(i == j)
b[i][j]=1;
else
b[i][j]=0;
}
Hill Cipher Program in C
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k];
q = c[k][k];

for(j = 0; j < 3; j++) {


if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
Hill Cipher Program in C
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];

printf("\n\nInverse Matrix is:\n");


for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
printf("%d ", b[i][j]);

printf("\n");
}
}
Data Encryption Standard (DES) Algorithm
Data Encryption Standard is a symmetric-key algorithm for the encrypting the
data. It comes under block cipher algorithm which follows Feistel structure.
Here is the block diagram of Data Encryption Standard.
Data Encryption Standard (DES) Algorithm
Data Encryption Standard (DES) Algorithm
Explanation for above diagram: Each character of plain text converted into
binary format. Every time we take 64 bits from that and give as input to DES
algorithm, then it processed through 16 rounds and then converted to cipher
text.
Initial Permutation
Initial Permutation: 64 bit plain text goes under initial permutation and then
given to round 1. Since initial permutation step receiving 64 bits, it contains
an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order.
After that, we arrange our original 64 bit text in the order mentioned in that
matrix
After initial permutation, 64 bit text passed through 16 rounds. In each round
it processed with 48 bit key. That means we need total 16 sub keys, one for
each round. See below diagram, it will show what happening in each round
of algorithm.
Data Encryption Standard (DES) Algorithm
DES
• Round i: In each round 64bit text divided into two 32bit parts. Left and
Right. You can see in diagram Li-1 and Ri-1. As algorithm says, Right 32bits
goes under Expansion Permutation.
• Expansion Permutation: Right side 32bit part of text given to expansion
permutation. It will produce a 48bit text as output. i.e. 16bits added in this
step. Some bits below 32 are repeated and arranged in an 1×48 matrix
form. We rearrange 32bit text by following the order of that matrix.
• After expansion permutation we have to XOR the output 48bit with a 48bit
sub key. Let see how that 48bit sub key generating from 64bit original key.
DES
• Permutated Choice 1: Initially we take a 64 bit key and then apply to
permutated choice 1. It contains a 1×56 matrix but with shuffled 1 to 64
numbers except multiples of number 8. i.e. 8, 16, 24, 32, 40, 48, 56, 64 will
be discarded. Remaining 64-8 = 56 number will be there in 1×56 matrix.
We rearrange key in matrix specified order. [You can see the matrix in
below code]
• Left Circular Shift: 56bit key from permutated choice 1 given to left circular
shift operation. Here that 56bit key divided into two equal halves of each
28bit. These 28bits shifted depends upon the round number. We already
have the data that in each round how many bits circularly we have to shift.
You can see this data in shifts array in code.
DES
• Permutated Choice 2: Result of Left circular shift 56bit key given to
permutated choice 2. This step will produce 48bit sub key. For this it has an
1×48 matrix, in which out of 56, some random 8 bits will be discarded. And
remaining 48 will be there. According to this bit positions we have to
rearrange the key. You can see this matrix in below code.
• Now output of permutated choice 2 will be Xor with output of expansion
permutation, which results a 48bit one. This 48bit again reduced to 32bit
using Substitution boxes [called S box].
DES
• Substitution boxes [S box]: In DES algorithm we have 8 S boxes. Input for S box
is 48bit. And output from S box is 32 bit. The input 48 bit will be divided
equally to 8 s boxes from s1, s2, … s8. So each s box will get 48/8= 6 bits as
input. This Each S box reduce 6 bits to 4 bits. i.e input for each S box is 6 bits
and output is 4 bits. Finally, 8*4 = 32 bit. Which is final output of S box
operation.
• Let see how 6bits converted to 4 bits from S box. S box is an 4×16 matrix
containing numbers in range 0 to 15. Take example, assume input 6 bits for S
box are 011011. In this first and last bit together represents row number. Since
maximum number with two bits is 3, S box also contains 0 to 3 rows total of 4.
And middle 4 numbers together represent column number. Since maximum
number with 4 bits is 15, S box also contains columns 0 to 15 total of 16. So
here first and last bit = 01 i.e. row number 1 and middle 4 bits 1101= 13 i.e.
column number 13. So for this input the number positioned at row 1 and
column 13 will be picked. As mentioned earlier S box only contains number in
range 0 to 15. All can be represented in 4 bits. So picked number 4 bits are
output for the S box. See the code for all S boxes.
DES
• Permutation: After getting output from all S boxes, we are applying again
permutation. Here also a matrix with different arrangements will be there,
we have to arrange according to that.
• Final XOR: After this permutation, take the left half which initially divided
64bit text to two halves. Do XOR with this permutation output to left 32bit
part. This result is new Right part. And Right 32bit part which passed
through all permutation will be come as new Left Part. These 2 parts will
be the inputs for the second round. Same as keys also, the parts before
left shift are next round input keys.
• All this explanation for a single round for a 62bit plain text. Like this, it
passes through total 16 rounds.
DES
• 32 bit swap: After completion of 16 rounds, final 64 bits divided into
two 32 bit parts and they swap each other.
• Inverse Initial Permutation: Here also a matrix will be there, in which
bits are just shuffled. No adding or subtracting bits. See the code for
this matrix.
Data encryption standard
• Data encryption standard (DES) has been found vulnerable against very
powerful attacks and therefore, the popularity of DES has been found
slightly on decline.
• DES is a block cipher, and encrypts data in blocks of size of 64 bit each,
means 64 bits of plain text goes as the input to DES, which produces 64 bits
of cipher text. The same algorithm and key are used for encryption and
decryption, with minor differences. The key length is 56 bits. The basic idea
is show in figure.
DES
DES
We have mention that DES uses a 56 bit key. Actually, the initial key
consists of 64 bits. However, before the DES process even starts, every 8th
bit of the key is discarded to produce a 56 bit key. That is bit position 8,
16, 24, 32, 40, 48, 56 and 64 are discarded.
DES
Thus, the discarding of every 8th bit of the key produces a 56-bit key from the
original 64-bit key.
DES is based on the two fundamental attributes of cryptography: substitution (also
called as confusion) and transposition (also called as diffusion). DES consists of 16
steps, each of which is called as a round. Each round performs the steps of
substitution and transposition. Let us now discuss the broad-level steps in DES.
1.In the first step, the 64 bit plain text block is handed over to an initial Permutation
(IP) function.
2.The initial permutation performed on plain text.
3.Next the initial permutation (IP) produces two halves of the permuted block; says
Left Plain Text (LPT) and Right Plain Text (RPT).
4.Now each LPT and RPT to go through 16 rounds of encryption process.
5.In the end, LPT and RPT are rejoined and a Final Permutation (FP) is performed on
the combined block
6.The result of this process produces 64 bit cipher text.
DES
Initial Permutation
As we have noted, the Initial permutation (IP) happens only once and it
happens before the first round. It suggests how the transposition in IP should
proceed, as shown in figure.
For example, it says that the IP replaces the first bit of the original plain text
block with the 58th bit of the original plain text, the second bit with the 50th
bit of the original plain text block and so on.
This is nothing but jugglery of bit positions of the original plain text block. the
same rule applies for all the other bit positions which shows in the figure.
Initial Permutation
Initial Permutation
• As we have noted after IP done, the resulting 64-bit permuted text
block is divided into two half blocks. Each half block consists of 32 bits,
and each of the 16 rounds, in turn, consists of the broad level steps
outlined in figure.
DES
Step-1 Key transformation
We have noted initial 64-bit key is transformed into a 56-bit key by discarding
every 8th bit of the initial key. Thus, for each a 56-bit key is available. From
this 56-bit key, a different 48-bit Sub Key is generated during each round
using a process called as key transformation. For this the 56 bit key is divided
into two halves, each of 28 bits. These halves are circularly shifted left by one
or two positions, depending on the round.
For example, if the round number 1, 2, 9 or 16 the shift is done by only
position for other rounds, the circular shift is done by two positions. The
number of key bits shifted per round is shown in figure.
Step-1 Key transformation

• After an appropriate shift, 48 of the 56 bit are selected. for selecting 48


of the 56 bits the table show in figure given below. For instance, after the
shift, bit number 14 moves on the first position, bit number 17 moves on
the second position and so on. If we observe the table carefully, we will
realize that it contains only 48 bit positions. Bit number 18 is discarded
(we will not find it in the table), like 7 others, to reduce a 56-bit key to a
48-bit key. Since the key transformation process involves permutation as
well as selection of a 48-bit sub set of the original 56-bit key it is called
Compression Permutation.
Compression Permutation
Because of this compression permutation technique, a different subset of key
bits is used in each round. That’s make DES not easy to crack.
Step-2: Expansion Permutation
Recall that after initial permutation, we had two 32-bit plain text areas
called as Left Plain Text(LPT) and Right Plain Text(RPT). During the expansion
permutation, the RPT is expanded from 32 bits to 48 bits. Bits are permuted
as well hence called as expansion permutation. This happens as the 32 bit
RPT is divided into 8 blocks, with each block consisting of 4 bits. Then, each
4 bit block of the previous step is then expanded to a corresponding 6 bit
block, i.e., per 4 bit block, 2 more bits are added.
Expansion Permutation
• This process results into expansion as well as permutation of the input bit
while creating output. Key transformation process compresses the 56-bit
key to 48 bits. Then the expansion permutation process expands the 32-bit
RPT to 48-bits. Now the 48-bit key is XOR with 48-bit RPT and resulting
output is given to the next step, which is the S-Box substitution.

You might also like