Ex: No: IMPLEMENTATION OF DIFFIE HELLMAN KEY EXCHANGE ALGORITHM
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <math.h>
int main() {
int p, g, a, b, A, B, keyA, keyB;
// Step 1: Public values
printf("Enter a prime number (p): ");
scanf("%d", &p);
printf("Enter primitive root (g): ");
scanf("%d", &g);
// Step 2: Private keys
printf("Alice, enter private key (a): ");
scanf("%d", &a);
printf("Bob, enter private key (b): ");
scanf("%d", &b);
// Step 3: Compute public values
A = (int)pow(g, a) % p; // Alice computes A
B = (int)pow(g, b) % p; // Bob computes B
printf("\nAlice sends A = %d", A);
printf("\nBob sends B = %d", B);
// Step 4: Compute shared secret keys
keyA = (int)pow(B, a) % p; // Alice computes key
keyB = (int)pow(A, b) % p; // Bob computes key
printf("\n\nAlice's Secret Key = %d", keyA);
printf("\nBob's Secret Key = %d", keyB);
if (keyA == keyB)
printf("\n\nKey Exchange Successful! Shared Key = %d\n", keyA);
else
printf("\n\nKey Exchange Failed!\n");
return 0;
}
OUTPUT:
RESULT:
Ex.No: IMPLEMENTATION OF SHA-I
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
#include <string.h>
// simple XOR function
int permute(int x, int y) {
return x ^ y; // XOR for mixing
}
// right shift
int rightShift(int x, int s) {
return (x >> s) | (x << (8 - s)); // rotate right by s bits
}
int main() {
char msg[256];
int A, B, C, D, E, F, s = 2, weight = 7;
int newA,newB,newC,newD,newE;
// Step-1: Input message (simulate as 5 blocks = 5 chars)
printf("Enter a 5-character message: ");
scanf("%s", msg);
// Step-2: Divide into blocks
A = msg[0];
B = msg[1];
C = msg[2];
D = msg[3];
E = msg[4];
printf("\nInitial Blocks:");
printf("\nA=%d, B=%d, C=%d, D=%d, E=%d", A, B, C, D, E);
// Step-3: Function F using (B,C,D)
F = (B & C) | (~B & D); // simple SHA-1 style function
printf("\n\nF(B,C,D) = %d", F);
// Step-4: Permute with block E
F = permute(F, E);
printf("\nAfter permutation with E: %d", F);
// Step-5: Shift A and permute
A = rightShift(A, s);
F = permute(F, A);
printf("\nAfter shifting A and permutation: %d", F);
// Step-6: Permute with weight
F = permute(F, weight);
printf("\nAfter permutation with weight: %d", F);
// Step-7: Update blocks
newA = F; // new A
newB = A; // old A becomes new B
newC = rightShift(B, s);
newD = C;
newE = D;
printf("\n\nFinal Blocks:");
printf("\nA=%d, B=%d, C=%d, D=%d, E=%d", newA, newB, newC, newD, newE);
printf("\n\nSimulated SHA-1 Hash Value: %d%d%d%d%d\n",
newA, newB, newC, newD, newE);
return 0;
}
OUTPUT:
RESULT:
Ex.No: IMPLEMENTATION OF DIGITAL SIGNATURE STANDARD
Date:
Aim:
Algorithm:
Program:
#include <stdio.h>
// Function to find gcd (Euclidean Algorithm)
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
// Function to find modular inverse using Extended Euclidean Algorithm
int modInverse(int a, int m) {
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1) return 0;
while (a > 1) {
q = a / m;
t = m;
m = a % m; a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0) x1 += m0;
return x1;
}
// Function to do (base^exp) % mod
int power(int base, int exp, int mod) {
long long result = 1;
base = base % mod;
while (exp > 0) {
if (exp % 2 == 1) result = (result * base) % mod;
exp = exp / 2;
base = (base * base) % mod;
}
return (int)result;
}
int main() {
int p, g, x, y, k, r, s, H,v1,v2,k_inv;
int clrscr();
// Step-1: Public values
printf("Enter a prime number p: ");
scanf("%d", &p);
printf("Enter a generator g: ");
scanf("%d", &g);
// Step-2: Private key (x)
printf("Enter private key x (Alice): ");
scanf("%d", &x);
// Compute public key y = g^x mod p
y = power(g, x, p);
printf("Public key y = %d\n", y);
// Step-3: Message hash (just use small number instead of real hash)
printf("Enter message hash (H): ");
scanf("%d", &H);
// Step-4: Choose random k (coprime with p-1)
printf("Enter random number k (coprime with %d): ", p-1);
scanf("%d", &k);
if (gcd(k, p-1) != 1) {
printf("Error: k must be coprime with p-1!\n");
return 0;
}
// Step-5: Signature generation
r = power(g, k, p); // r = g^k mod p
k_inv = modInverse(k, p-1);
s = (k_inv * (H - x * r)) % (p-1);
if (s < 0) s += (p-1);
printf("\nSignature (r, s) = (%d, %d)\n", r, s);
// Step-6: Verification
v1 = power(g, H, p); // g^H mod p
v2 = (power(y, r, p) * power(r, s, p)) % p; // y^r * r^s mod p
printf("\nVerification values:\n");
printf("g^H mod p = %d\n", v1);
printf("y^r * r^s mod p = %d\n", v2);
if (v1 == v2)
printf("\nSignature is VALID (not a forgery)\n");
else
printf("\nSignature is INVALID (forgery detected)\n");
return 0;
}
OUTPUT:
RESULT: