0% found this document useful (0 votes)
28 views8 pages

IS Lab Manual 8,9, 10 Experiments Programs

information security program

Uploaded by

Omkar Mujumdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views8 pages

IS Lab Manual 8,9, 10 Experiments Programs

information security program

Uploaded by

Omkar Mujumdar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

To study and implement of 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();

void main() {

printf("\nENTER FIRST PRIME NUMBER\n");

scanf("%ld",&p);

flag=prime(p);

if(flag==0)

{ printf("\nWRONG INPUT\n");

getch();

printf("\nENTER ANOTHER PRIME NUMBER\n");

scanf("%ld",&q);

flag=prime(q);

if(flag==0||p==q)

{ printf("\nWRONG INPUT\n"); getch(); }


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; }

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;

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; } 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]); }
Implementation of Diffie Hellman Key Exchange 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); }

void main()

{ int n,g,x,a,y,b;

//clrscr();

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 : ");

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)); getch(); }


Implementation of Digital Signature

import java.util.*;

import java.math.BigInteger;

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 (!test.isProbablePrime(99))

e: { test = test.add(one); }

return test; }

public static BigInteger findQ(BigInteger n)

{ BigInteger start = new BigInteger("2");

while (!n.isProbablePrime(99))

{ while (!((n.mod(start)).equals(zero)))

{ start = start.add(one);

} n = n.divide(start); }

return n; }

public static BigInteger getGen(BigInteger p, BigInteger q, Random r)

{ BigInteger h = new BigInteger(p.bitLength(), r);

h = h.mod(p);

return h.modPow((p.subtract(one)).divide(q), p); }

public static void main (String[] args) throws java.lang.Exception

{ Random randObj = new Random();

BigInteger p = getNextPrime("10600");
/* approximate prime */

BigInteger q = findQ(p.subtract(one));

BigInteger g = getGen(p,q,randObj);

System.out.println(" \n simulation of Digital Signature Algorithm \n");

System.out.println(" \n global public key components are:\n");

System.out.println("\np is: " + p); System.out.println("\nq is: " + q);

System.out.println("\ng is: " + g); BigInteger x = new BigInteger(q.bitLength(), randObj);

x = x.mod(q);

BigInteger y = g.modPow(x,p);

BigInteger k = ne

w BigInteger(q.bitLength(), randObj);

k = k.mod(q); BigInteger r = (g.modPow(k,p)).mod(q); BigInteger hashVal = new


BigInteger(p.bitLength(), randObj); BigInteger kInv = k.modInverse(q);

BigInteger s = kInv.multiply(hashVal.add(x.multiply(r))); s = s.mod(q);

System.out.println("\nsecret information are:\n"); System.out.println("x (private) is:" + x);

System.out.println("k (secret) is: " + k);

System.out.println("y (public) is: " + y);

System.out.println("h (rndhash) is: " + hashVal);

System.out.println("\n generating digital signature:\n");

System.out.println("r is : " + r); System.out.println("s is : " + s);

BigInteger w = s.modInverse(q); BigInteger u1 = (hashVal.multiply(w)).mod(q);

BigInteger u2 = (r.multiply(w)).mod(q); BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));

v = (v.mod(p)).mod(q);

System.out.println("\nverifying digital signature (checkpoints)\n:");

System.out.println("w is : " + w);

System.out.println("u1 is : " + u1);


System.out.println("u2 is : " + u2);

System.out.println("v is : " + v);

if (v.equals(r))

{ System.out.println("\nsuccess: digital signature isverified!\n " + r); }

else { System.out.println("\n error: incorrect digital signature\n "); }

You might also like