0% found this document useful (0 votes)
164 views30 pages

Problem 1

The document discusses implementing various classical encryption techniques: 1) Caesar cipher, Playfair cipher, Hill cipher, Vigenere cipher, and Rail Fence cipher. Code samples are provided to encrypt and decrypt messages using each technique. The Caesar cipher code shifts each letter by a key value. The Playfair cipher encrypts pairs of letters based on their positions in a 5x5 keyword matrix. The Hill cipher uses matrix multiplication for encryption/decryption. The Vigenere cipher encrypts letters by adding the corresponding letter from a repeating keyword. The Rail Fence cipher arranges letters in a zigzag pattern in a matrix based on a fence height key.

Uploaded by

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

Problem 1

The document discusses implementing various classical encryption techniques: 1) Caesar cipher, Playfair cipher, Hill cipher, Vigenere cipher, and Rail Fence cipher. Code samples are provided to encrypt and decrypt messages using each technique. The Caesar cipher code shifts each letter by a key value. The Playfair cipher encrypts pairs of letters based on their positions in a 5x5 keyword matrix. The Hill cipher uses matrix multiplication for encryption/decryption. The Vigenere cipher encrypts letters by adding the corresponding letter from a repeating keyword. The Rail Fence cipher arranges letters in a zigzag pattern in a matrix based on a fence height key.

Uploaded by

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

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 javax.swing.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;
class DES {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage;
public DES()
{
try
{
generateSymmetricKey();
inputMessage=JOptionPane.showInputDialog(null,"Enter message to encrypt");
byte[] ibyte = inputMessage.getBytes();
byte[] ebyte=encrypt(raw, ibyte);
String encryptedData = new String(ebyte);
System.out.println("Encrypted message "+encryptedData);
JOptionPane.showMessageDialog(null,"Encrypted Data"+"\n"+encryptedData);
byte[] dbyte= decrypt(raw,ebyte);
String decryptedMessage = new String(dbyte);
System.out.println("Decrypted message"+decryptedMessage);

10
PARUL MITTAL Information Network Security

JOptionPane.showMessageDialog(null,"Decrypted Data"+"\n"+decryptedMessage);
}
catch(Exception e)
{
System.out.println(e);
}
}
void generateSymmetricKey() {
try {
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey=getRawKey(knumb);
skeyString = new String(skey);
System.out.println("DES Symmetric key = "+skeyString);
}
catch(Exception e)
{
System.out.println(e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(56, sr);

11
PARUL MITTAL Information Network Security

SecretKey skey = kgen.generateKey();


raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws
Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw,
"DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted)
throws Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw,"DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(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 java.security.*;
public class SHA1 {
public static void main(String[] a) {
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = " +md.getAlgorithm());
System.out.println(" Provider = " +md.getProvider());
System.out.println(" ToString = " +md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") =
+bytesToHex(output));
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\""+input+"\") = "
+bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\"" +input+"\") = "
+bytesToHex(output));

25
PARUL MITTAL Information Network Security

System.out.println(""); }
catch (Exception e) {
System.out.println("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<b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]); }
return buf.toString();
}
}

26
PARUL MITTAL Information Network Security

Problem 3. Implement the Signature Scheme - Digital Signature Standard


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)
{

27
PARUL MITTAL Information Network Security

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 = new 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);

28
PARUL MITTAL Information Network Security

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 is verified!\n " + r);
}
else
{
System.out.println("\n error: incorrect digital signature\n ");
}}}

29
PARUL MITTAL Information Network Security

30

You might also like