PARUL MITTAL Information Network Security
Problem 1. Implement the following Substitution & Transposition Techniques
1.1) Caesar Cipher
For Encryption:-
#include<stdio.h>
int main()
{ char message[100], ch;
int i, key;
printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}}
printf("Encrypted message: %s", message);
1
PARUL MITTAL Information Network Security
return 0;}
For Decryption:-
#include<stdio.h>
int main()
{ char message[100], ch;
int i, key;
printf("Enter a message to decrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch < 'A'){
ch = ch + 'Z' - 'A' + 1; }
message[i] = ch;
}} printf("Decrypted message: %s", message);
return 0; }
2
PARUL MITTAL Information Network Security
1.2) Playfair Cipher:-
#include<stdio.h>
int main()
{
char arr[15][15]= {"MONAR","CHYBD","EFGIK","LPQST","UVWXZ"};
char pt[10];
int i, j, r1=0, r2=0, c1=0, c2=0;
printf("Playfair Keymatrix\n==================\n");
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
printf("%c ", arr[i][j]);
printf("\n");
}
printf("Enter your plain text:");
scanf("%s",pt);
printf("Your plain text = %s", pt);
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(arr[i][j] == pt[0])
{
r1=i;
c1=j;
}
if(arr[i][j] == pt[1])
{
3
PARUL MITTAL Information Network Security
r2=i;
c2=j;
}
}
}
if(r1==r2)
{
if(c2==4)
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][0]);
else
printf("Ciphertext = %c%c \n", arr[r1][c1+1], arr[r2][c2+1]);
}
if(c1==c2)
{
if(r2==4) //for char in last row
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[0][c2]);
else
printf("Ciphertext = %c%c \n", arr[r1+1][c1], arr[r2+1][c2]);
}
if(r1 != r2 && c1 != c2)
{
printf("\nCiphertext = %c%c \n", arr[r1][c2], arr[r2][c1]);
}
return 0;
}
4
PARUL MITTAL Information Network Security
1.3) Hill Cipher:-
#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();
void decryption();
void getKeyMessage();
void inverse();
int main()
{
getKeyMessage();
encryption();
decryption();
}
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));
}
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");
}
void getKeyMessage()
{
int i, j;
char msg[3];
printf("Enter 3x3 matrix for key (It should be inversible):\n");
5
PARUL MITTAL Information Network Security
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;
}
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;
}
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];
}
}
}
}
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");
}
}
6
PARUL MITTAL Information Network Security
1.4) Vigenere Cipher
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int count, j;
char message[50];
char key[20];
printf("\nEnter Message To Encode:\t");
fflush(stdin);
scanf("%[^\n]s", message);
printf("\nEnter Key:\t");
fflush(stdin);
scanf("%[^\n]s", key);
int message_length = strlen(message), key_length = strlen(key);
char temp_key[message_length], encrypted_message[message_length],
decrypted_message[message_length];
for(count = 0, j = 0; count < message_length; ++count, ++j)
{
if(j == key_length)
{
j = 0;
}
temp_key[count] = key[j];
}
temp_key[count] = '\0';
count = 0;
while(count < message_length)
{
encrypted_message[count] = ((message[count] + temp_key[count]) % 26) + 'A';
count = count + 1;
}
encrypted_message[count] = '\0';
count = 0;
while(count < message_length)
{
decrypted_message[count] = (((encrypted_message[count] - temp_key[count]) +
26) % 26) + 'A';
count = count + 1;
}
decrypted_message[count] = '\0';
printf("\nIntial String:\t%s", message);
printf("\nKey:\t%s", key);
printf("\nGenerated Key:\t%s", temp_key);
printf("\nEncrypted Message:\t%s", encrypted_message);
printf("\nDecrypted Message:\t%s", decrypted_message);return 0;}
7
PARUL MITTAL Information Network Security
1.5) Rail Fence – Row and Column Transposition
#include<stdio.h>
#include<string.h>
void encryptMsg(char msg[], int key){
int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0;
char railMatrix[key][msgLen];
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = msg[i];
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
printf("\nEncrypted Message: ");
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] != '\n')
printf("%c", railMatrix[i][j]);
}
void decryptMsg(char enMsg[], int key){
int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0;
char railMatrix[key][msgLen];
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){
railMatrix[row][col++] = '*';
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] == '*')
railMatrix[i][j] = enMsg[m++];
row = col = 0;
k = -1;
printf("\nDecrypted Message: ");
for(i = 0; i < msgLen; ++i){
8
PARUL MITTAL Information Network Security
printf("%c", railMatrix[row][col++]);
if(row == 0 || row == key-1)
k= k * (-1);
row = row + k;
}
}
int main(){
char msg[] = "Hello World";
char enMsg[] = "Horel ollWd";
int key = 3;
printf("Original Message: %s", msg);
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
}
9
PARUL MITTAL Information Network Security
Problem 2. Implement the following Algorithms
2.1 DES (Data Encryption Standard)
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link] ;
class DES {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public DES()
{
try
{
generateSymmetricKey();
inputMessage=[Link](null,"Enter message to encrypt");
byte[] ibyte = [Link]();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
[Link]("Encrypted message "+encryptedData);
[Link](null,"Encrypted Data"+"\n"+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
[Link]("Decrypted message"+decryptedMessage);
10
PARUL MITTAL Information Network Security
[Link](null,"Decrypted Data"+"\n"+decryptedMessage);
}
catch(Exception e)
{
[Link](e);
}
}
void generateSymmetricKey() {
try {
Random r = new Random();
int num = [Link](10000);
String knum = [Link](num);
byte[] knumb = [Link]();
skey=getRawKey(knumb);
skeyString = new String(skey);
[Link]("DES Symmetric key = "+skeyString);
}
catch(Exception e)
{
[Link](e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception
{
KeyGenerator kgen = [Link]("DES");
SecureRandom sr = [Link]("SHA1PRNG");
[Link](seed);
[Link](56, sr);
11
PARUL MITTAL Information Network Security
SecretKey skey = [Link]();
raw = [Link]();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws
Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw,
"DES");
Cipher cipher = [Link]("DES");
[Link](Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = [Link](clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw,"DES");
Cipher cipher = [Link]("DES");
[Link](Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = [Link](encrypted);
return decrypted;
}
public static void main(String args[]) {
DES des = new DES();
}}
12
PARUL MITTAL Information Network Security
2.2 RSA Algorithm
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
int main() {
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0) {
printf("\nWRONG INPUT\n");
getch();
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q) {
13
PARUL MITTAL Information Network Security
printf("\nWRONG INPUT\n");
getch();
exit(1);}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for (i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getch();
}
int prime(long int pr) {
int i;
j=sqrt(pr);
for (i=2;i<=j;i++) {
if(pr%i==0)
return 0;
}
return 1;
}
14
PARUL MITTAL Information Network Security
void ce() {
int k;
k=0;
for (i=2;i<t;i++) {
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q) {
e[k]=i;
flag=cd(e[k]);
if(flag>0) {
d[k]=flag;
k++;}
if(k==99)
break;
}
}
}
long int cd(long int x) {
long int k=1;
while(1) {
k=k+t;
if(k%x==0)
return(k/x);
}}
void encrypt() {
long int pt,ct,key=e[0],k,len;
15
PARUL MITTAL Information Network Security
i=0;
len=strlen(msg);
while(i!=len) {
pt=m[i];
pt=pt-96;
k=1;
for (j=0;j<key;j++) {
k=k*pt;
k=k%n;}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt() {
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1) {
ct=temp[i];
k=1;
for (j=0;j<key;j++) {
k=k*ct;
k=k%n;}
16
PARUL MITTAL Information Network Security
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}
17
PARUL MITTAL Information Network Security
2.3 Diffie Hellman Algorithm:-
#include<stdio.h>
#include<conio.h>
long long int power(int a, int b, int mod)
{
long long int t;
if(b==1)
return a;
t=power(a,b/2,mod);
if(b%2==0)
return (t*t)%mod;
else
return (((t*t)%mod)*a)%mod;
}
long int calculateKey(int a, int x, int n)
{
return power(a,x,n);
}
int main()
{
int n,g,x,a,y,b;
printf("Enter the value of n and g : ");
scanf("%d%d",&n,&g);
printf("Enter the value of x for the first person : ");
scanf("%d",&x);
a=power(g,x,n);
printf("Enter the value of y for the second person : ");
18
PARUL MITTAL Information Network Security
scanf("%d",&y);
b=power(g,y,n);
printf("key for the first person is :%lld\n",power(b,x,n));
printf("key for the second person is :%lld\n",power(a,y,n));
}
19
PARUL MITTAL Information Network Security
2.4 Md5 Algorithm:-
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include<conio.h>
typedef union uwb
{
unsigned w;
unsigned char b[4];
} MD5union;
typedef unsigned DigestArray[4];
unsigned func0( unsigned abcd[] ){
return ( abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]);}
unsigned func1( unsigned abcd[] ){
return ( abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);}
unsigned func2( unsigned abcd[] ){
return abcd[1] ^ abcd[2] ^ abcd[3];}
unsigned func3( unsigned abcd[] ){
return abcd[2] ^ (abcd[1] |~ abcd[3]);}
typedef unsigned (*DgstFctn)(unsigned a[]);
unsigned *calctable( unsigned *k)
{
double s, pwr;
int i;
pwr = pow( 2, 32);
for (i=0; i<64; i++)
{
20
PARUL MITTAL Information Network Security
s = fabs(sin(1+i));
k[i] = (unsigned)( s * pwr );
}
return k;
}
unsigned rol( unsigned r, short N )
{
unsigned mask1 = (1<<N) -1;
return ((r>>(32-N)) & mask1) | ((r<<N) & ~mask1);
}unsigned *md5( const char *msg, int mlen)
{
static DigestArray h0 = { 0x67452301, 0xEFCDAB89,
0x98BADCFE, 0x10325476 };
static DgstFctn ff[] = { &func0, &func1, &func2, &func3};
static short M[] = { 1, 5, 3, 7 };
static short O[] = { 0, 1, 5, 0 };
static short rot0[] = { 7,12,17,22};
static short rot1[] = { 5, 9,14,20};
static short rot2[] = { 4,11,16,23};
static short rot3[] = { 6,10,15,21};
static short *rots[] = {rot0, rot1, rot2, rot3 };
static unsigned kspace[64];
static unsigned *k;
static DigestArray h;
DigestArray abcd;
DgstFctn fctn;
short m, o, g;
unsigned f;
21
PARUL MITTAL Information Network Security
short *rotn;
union
{
unsigned w[16];
char b[64];
}mm;
int os = 0;
int grp, grps, q, p;
unsigned char *msg2;
if (k==NULL) k= calctable(kspace);
for (q=0; q<4; q++) h[q] = h0[q]; // initialize
{
grps = 1 + (mlen+8)/64;
msg2 = malloc( 64*grps);
memcpy( msg2, msg, mlen);
msg2[mlen] = (unsigned char)0x80;
q = mlen + 1;
while (q < 64*grps){ msg2[q] = 0; q++ ; }
{
MD5union u;
u.w = 8*mlen;
q -= 8;
memcpy(msg2+q, &u.w, 4 );
}
}
for (grp=0; grp<grps; grp++)
{
22
PARUL MITTAL Information Network Security
memcpy( mm.b, msg2+os, 64);
for(q=0;q<4;q++) abcd[q] = h[q];
for (p = 0; p<4; p++)
{
fctn = ff[p];
rotn = rots[p];
m = M[p]; o= O[p];
for (q=0; q<16; q++)
{
g = (m*q + o) % 16;
f = abcd[1] + rol( abcd[0]+ fctn(abcd)+k[q+16*p]
+ mm.w[g], rotn[q%4]);
abcd[0] = abcd[3];
abcd[3] = abcd[2];
abcd[2] = abcd[1];
abcd[1] = f;
}}
for (p=0; p<4; p++)
h[p] += abcd[p];
os += 64;
}
return h;}
int main()
{
int j,k;
const char *msg = "parul mittal";
unsigned *d = md5(msg, strlen(msg));
MD5union u;
23
PARUL MITTAL Information Network Security
printf("\t MD5 ENCRYPTION ALGORITHM IN C \n\n");
printf("Input String to be Encrypted using MD5 :\n\t%s",msg);
printf("\n\nThe MD5 code for input string is: \n");
printf("\t= 0x");
for (j=0;j<4; j++){
u.w = d[j];
for (k=0;k<4;k++) printf("%02x",u.b[k]);
}
printf("\n");
printf("\n\t MD5 Encyption Successfully Completed!!!\n\n");
getch();
system("pause");
getch();
}
24
PARUL MITTAL Information Network Security
2.5 SHA-1 Algorithm
import [Link].*;
public class SHA1 {
public static void main(String[] a) {
try {
MessageDigest md = [Link]("SHA1");
[Link]("Message digest object info: ");
[Link](" Algorithm = " +[Link]());
[Link](" Provider = " +[Link]());
[Link](" ToString = " +[Link]());
String input = "";
[Link]([Link]());
byte[] output = [Link]();
[Link]();
[Link]("SHA1(\""+input+"\") =
+bytesToHex(output));
input = "abc";
[Link]([Link]());
output = [Link]();
[Link]();
[Link]("SHA1(\""+input+"\") = "
+bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
[Link]([Link]());
output = [Link]();
[Link]();
[Link]("SHA1(\"" +input+"\") = "
+bytesToHex(output));
25
PARUL MITTAL Information Network Security
[Link](""); }
catch (Exception e) {
[Link]("Exception: " +e);
}
}
public static String bytesToHex(byte[] b)
{
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
StringBuffer buf = new StringBuffer();
for (int j=0; j<[Link]; j++) {
[Link](hexDigit[(b[j] >> 4) & 0x0f]);
[Link](hexDigit[b[j] & 0x0f]); }
return [Link]();
}
}
26
PARUL MITTAL Information Network Security
Problem 3. Implement the Signature Scheme - Digital Signature Standard
import [Link].*;
import [Link];
class dsaAlg {
final static BigInteger one = new BigInteger("1");
final static BigInteger zero = new BigInteger("0");
public static BigInteger getNextPrime(String ans)
{
BigInteger test = new BigInteger(ans);
while ()
e:{
test = [Link](one);}
return test;}
public static BigInteger findQ(BigInteger n)
{ BigInteger start = new BigInteger("2");
while ()
{ while (!(([Link](start)).equals(zero)))
{
start = [Link](one);
}
n = [Link](start);
}
return n;
}
public static BigInteger getGen(BigInteger p, BigInteger q,
Random r)
{
27
PARUL MITTAL Information Network Security
BigInteger h = new BigInteger([Link](), r);
h = [Link](p);
return [Link](([Link](one)).divide(q), p);
}
public static void main (String[] args) throws
[Link]
{
Random randObj = new Random();
BigInteger p = getNextPrime("10600"); /* approximate
prime */
BigInteger q = findQ([Link](one));
BigInteger g = getGen(p,q,randObj);
[Link](" \n simulation of Digital Signature Algorithm \n");
[Link](" \n global public key components are:\n");
[Link]("\np is: " + p);
[Link]("\nq is: " + q);
[Link]("\ng is: " + g);
BigInteger x = new BigInteger([Link](), randObj);
x = [Link](q);
BigInteger y = [Link](x,p);
BigInteger k = new BigInteger([Link](), randObj);
k = [Link](q);
BigInteger r = ([Link](k,p)).mod(q);
BigInteger hashVal = new BigInteger([Link](),
randObj);
BigInteger kInv = [Link](q);
BigInteger s = [Link]([Link]([Link](r)));
s = [Link](q);
28
PARUL MITTAL Information Network Security
[Link]("\nsecret information are:\n");
[Link]("x (private) is:" + x);
[Link]("k (secret) is: " + k);
[Link]("y (public) is: " + y);
[Link]("h (rndhash) is: " + hashVal);
[Link]("\n generating digital signature:\n");
[Link]("r is : " + r);
[Link]("s is : " + s);
BigInteger w = [Link](q);
BigInteger u1 = ([Link](w)).mod(q);
BigInteger u2 = ([Link](w)).mod(q);
BigInteger v = ([Link](u1,p)).multiply([Link](u2,p));
v = ([Link](p)).mod(q);
[Link]("\nverifying digital signature checkpoints\n:");
[Link]("w is : " + w);
[Link]("u1 is : " + u1);
[Link]("u2 is : " + u2);
[Link]("v is : " + v);
if ([Link](r))
{
[Link]("\nsuccess: digital signature is verified!\n " + r);
}
else
{
[Link]("\n error: incorrect digital signature\n ");
}}}
29
PARUL MITTAL Information Network Security
30