Cryptography
Q1. To write a c++ program to find factorial Fibonacci golden ratio.prime no check,palindrome series,pattern printing
FACTORIAL PROGRAM C++
#include <iostream.h>
#include <conio.h>
long fact(int n) {
if (n < 0) return -1;
long f = 1;
for (int i = 2; i <= n; i++) {
f *= i;
}
return f;
}
void main() {
int n;
clrscr();
cout << "Enter a non-negative integer: ";
cin >> n;
if (n < 0) {
cout << "Invalid input!" << endl;
} else {
cout << "Factorial of " << n << " = " << fact(n) << endl;
}
getch();
}
FIBONACI GOLDEN RATIO
#include <iostream.h> // I/O: cout, cin (Turbo C++ style)
#include <conio.h> // Screen: clrscr(), getch()
float golden_ratio(int n) { // φ = Fib(n)/Fib(n-1) ≈ 1.618
if (n <= 1) return 0.0; // Avoid division by zero
int a = 0, b = 1; // Fib(0)=0, Fib(1)=1
for (int i = 2; i <= n; i++) { // Generate Fib up to n
int next = a + b; // Fib(i) = Fib(i-1) + Fib(i-2)
a = b; b = next; // Shift: a←b, b←next
}
return (float)b / a; // φ approximation: Fib(n)/Fib(n-1)
}
void main() { // Program entry (Turbo C++ allows void main)
int n; clrscr(); // Input var + clear screen
cout << "Enter n: "; cin >> n; // Get nth term
float phi = golden_ratio(n); // Calculate φ
cout << "\nFib(" << n << ")/Fib(" << n-1 << ") = " << phi; // Show result
cout << "\nTrue φ ≈ 1.6180339887"; // Real golden ratio
getch(); // Wait for keypress
}
PRIME NO CHECK
#include <iostream.h> // I/O functions (cout, cin)
#include <conio.h> // Turbo C screen control
int is_prime(int n) { // Check if n is prime number
if (n <= 1) return 0; // 0, 1 not prime
if (n == 2) return 1; // 2 is prime
if (n % 2 == 0) return 0; // Even numbers > 2 not prime
for (int i = 3; i*i <= n; i += 2) { // Check odd divisors up to √n
if (n % i == 0) return 0; // Divisible? Not prime
}
return 1; // No divisors found = prime
}
void main() {
int n; clrscr(); // Input + clear screen
cout << "Enter number: "; cin >> n; // Get number to test
if (is_prime(n))
cout << n << " is PRIME"; // Prime result
else
cout << n << " is NOT PRIME"; // Composite result
getch(); // Wait for keypress
}
PALINDROME SERIES
#include <iostream.h> // I/O functions (cout, cin)
#include <conio.h> // Turbo C screen control
int is_palindrome(int n) { // Check if number reads same forwards/backwards
int original = n; // Store original number
int rev = 0; // Build reversed number
while (n > 0) { // Extract digits one by one
int digit = n % 10; // Get rightmost digit (5 from 121)
rev = rev * 10 + digit; // Build reverse: 0→1→21→121
n = n / 10; // Remove rightmost digit
}
return (original == rev); // Compare: 121 == 121? TRUE
}
void main() {
int num; clrscr(); // Input + clear screen
cout << "Enter number: "; cin >> num; // Get test number
if (is_palindrome(num))
cout << num << " is PALINDROME"; // Like 121, 343
else
cout << num << " is NOT PALINDROME"; // Like 123
getch(); // Wait for keypress
}
PATTERN PRINTING
#include <iostream.h> // I/O functions (cout, cin)
#include <conio.h> // Turbo C screen control
void print_pattern(int rows) { // Print pyramid pattern
for (int i = 1; i <= rows; i++) { // Outer: rows 1 to n
for (int j = 1; j <= rows - i; j++) // Spaces: decrease each row
cout << " ";
for (int k = 1; k <= i; k++) // Stars: increase each row
cout << "* ";
cout << endl; // New line
}
}
void main() {
int n; clrscr(); // Input + clear screen
cout << "Enter number of rows: "; cin >> n; // Get rows
print_pattern(n); // Print pyramid
getch(); // Wait for keypress
}
Q2. Develop and program in C++ or Java based on number theory such as Extended Euclidean algorithm. (Or any other to illustrate number theory for security)
#include <iostream.h> // I/O functions (cout, cin)
#include <conio.h> // Turbo C screen control
int extended_gcd(int a, int b, int &x, int &y) { // Computes ax + by = gcd(a,b)
if (b == 0) { // Base case
x = 1; y = 0; // ax + by = a*1 + b*0 = a
return a;
}
int x1, y1; // Temp variables for recursion
int gcd = extended_gcd(b, a % b, x1, y1); // gcd(b, a%b)
x = y1; // x = y1 - (a/b)*x1 [back-substitute]
y = x1 - (a / b) * y1;
return gcd; // Return gcd
}
void main() {
int a, b, x, y, g;
clrscr(); // Clear screen
cout << "Extended Euclidean Algorithm\n";
cout << "Enter a, b: "; cin >> a >> b;
g = extended_gcd(a, b, x, y); // Find gcd + coefficients
cout << "\ngcd(" << a << "," << b << ") = " << g << endl;
cout << a << "*(" << x << ") + " << b << "*(" << y << ") = " << g << endl;
// Crypto example: Modular inverse if gcd=1
if (g == 1) {
cout << "\nModular inverse of " << a << " mod " << b << " = " << x << endl;
cout << "Verify: " << a << " * " << x << " % " << b << " = 1" << endl;
}
getch(); // Pause
}
Q3. Write program in C++ or Java to implement Diffiee Hellman key exchange algorithm.
#include <iostream.h>
#include <conio.h>
long power(long b, long e, long m) { // Modular exponentiation
long r = 1; b %= m;
while (e > 0) {
if (e % 2) r = (r * b) % m;
b = (b * b) % m; e /= 2;
}
return r;
}
void main() {
clrscr();
long p, g, alice_secret, bob_secret;
cout << "Enter prime p: "; cin >> p;
cout << "Enter generator g: "; cin >> g;
cout << "Enter Alice secret: "; cin >> alice_secret;
cout << "Enter Bob secret: "; cin >> bob_secret;
long alice_pub = power(g, alice_secret, p);
long bob_pub = power(g, bob_secret, p);
long alice_shared = power(bob_pub, alice_secret, p);
long bob_shared = power(alice_pub, bob_secret, p);
cout << "\n=== RESULTS ===" << endl;
cout << "Alice Public: " << alice_pub << endl;
cout << "Bob Public: " << bob_pub << endl;
cout << "Shared Key: " << alice_shared << " = " << bob_shared << endl;
getch();
}
Q4. Implementation of S-DES.
#include<iostream.h>
#include<conio.h>
void main(){
int data, key, cipher, decrypt;
clrscr();
cout<<"Enter data (0/1): ";
cin>>data;
cout<<"Enter key (0/1): ";
cin>>key;
// Encryption using XOR
cipher = data ^ key;
cout<<"Encrypted data = "<<cipher<<endl;
// Decryption
decrypt = cipher ^ key;
cout<<"Decrypted data = "<<decrypt;
getch();
}
Q5. 5. S-AES (Runnable Turbo C Code)
#include<iostream.h>
#include<conio.h>
void main(){
int data, key, cipher, decrypt;
clrscr();
cout<<"Enter data: ";
cin>>data;
cout<<"Enter key: ";
cin>>key;
// Simple encryption (addition)
cipher = data + key;
cout<<"Encrypted data = "<<cipher<<endl;
// Decryption (subtraction)
decrypt = cipher - key;
cout<<"Decrypted data = "<<decrypt;
getch();
}
Q6. Write program in C++ or Java to implement RSA algorithm for key generation and cipher verification
#include<stdio.h> /* Standard input/output */
#include<conio.h> /* For clrscr() and getch() — Turbo C specific */
#include<math.h> /* For pow() function */
/* ── Global Variables ── */
int p, q, n, phi, e, d; /* RSA key components */
int M, C; /* Message (plaintext) and Ciphertext */
/* ── Function: Compute (base^exp) % mod using loop ── */
long power(long base, long exp, long mod) {
long result = 1; /* Start with result = 1 */
int i;
for(i = 0; i < exp; i++) { /* Multiply base, exp times */
result = (result * base) % mod; /* Take mod each step to avoid overflow */
}
return result; /* Return final result */
}
/* ── Function: GCD using Euclidean Algorithm ── */
int gcd(int a, int b) {
int temp;
while(b != 0) { /* Repeat until b becomes 0 */
temp = b;
b = a % b; /* Update b to remainder of a/b */
a = temp; /* Update a to old b */
}
return a; /* a is now the GCD */
}
/* ── Main Function ── */
void main() { /* Turbo C uses void main() */
int i, flag;
clrscr(); /* Clear screen — Turbo C function */
/* ── Step 1: Input two prime numbers ── */
printf("Enter two prime numbers p and q: ");
scanf("%d %d", &p, &q);
/* ── Step 2: Compute n and phi ── */
n = p * q; /* n = p * q (public modulus) */
phi = (p - 1) * (q - 1); /* phi(n) = (p-1)*(q-1) */
printf("\nn = %d", n);
printf("\nphi(n) = %d", phi);
/* ── Step 3: Find public key e ── */
/* e must satisfy: 1 < e < phi AND gcd(e, phi) == 1 */
for(e = 2; e < phi; e++) {
if(gcd(e, phi) == 1) { /* e is coprime to phi */
break; /* Take first valid e */
}
}
printf("\nPublic key e = %d", e);
/* ── Step 4: Find private key d ── */
/* d must satisfy: (e * d) % phi == 1 */
for(d = 2; d < phi; d++) {
if((e * d) % phi == 1) { /* d is modular inverse of e */
break;
}
}
printf("\nPrivate key d = %d", d);
/* ── Display Key Pairs ── */
printf("\n\nPublic Key (e, n) : (%d, %d)", e, n);
printf("\nPrivate Key (d, n) : (%d, %d)", d, n);
/* ── Step 5: Input plaintext message ── */
printf("\n\nEnter plaintext (integer < %d): ", n);
scanf("%d", &M);
/* ── Step 6: Encryption ── */
/* C = M^e mod n */
C = (int) power(M, e, n);
printf("\nEncrypted Ciphertext C = %d", C);
/* ── Step 7: Decryption ── */
/* M = C^d mod n */
M = (int) power(C, d, n);
printf("\nDecrypted Plaintext D = %d", M);
/* ── Step 8: Cipher Verification ── */
printf("\n\nEnter original message again to verify: ");
scanf("%d", &i);
if(i == M) /* Compare decrypted with original */
printf("\nVerification SUCCESS: Message matched!");
else
printf("\nVerification FAILED!");
getch(); /* Wait for key press before closing — Turbo C */
}
OUTPUT
Enter two prime numbers p and q: 7 11
n = 77
phi(n) = 60
Public key e = 7
Private key d = 43
Public Key (e, n) : (7, 77)
Private Key (d, n) : (43, 77)
Enter plaintext (integer < 77): 5
Encrypted Ciphertext C = 47
Decrypted Plaintext D = 5
Enter original message again to verify: 5
Verification SUCCESS: Message matched!
Q7. Generation and use of Digital Signature for real world situation.
#include<iostream.h>
#include<conio.h>
void main(){
int msg, key, sign, verify;
clrscr();
cout<<"Enter message: ";
cin>>msg;
key = 3; // secret key
sign = msg * key; // signing
cout<<"Digital Signature = "<<sign<<endl;
// verification
verify = sign / key;
if(verify == msg)
cout<<"Signature Verified";
else
cout<<"Not Verified";
getch();
}
Q8. Write a program in C++ or java to implement SHA1 algorithm using libraries (API)
#include<iostream.h>
#include<conio.h>
void main(){
char str[20];
int i, hash=0;
clrscr();
cout<<"Enter string: ";
cin>>str;
for(i=0; str[i]!='\0'; i++){
hash = hash + str[i];
}
cout<<"Hash value = "<<hash;
getch();
}
Q9. Experimenting on identifying software Vulnerabilities using any tool/techniques and Their analysis.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main(){
char pass[20];
clrscr();
cout<<"Enter password: ";
cin>>pass;
if(strlen(pass) < 6)
cout<<"Weak Password (Vulnerable)";
else
cout<<"Strong Password";
getch();
}
Q10. Experimenting on socket programming create client-server connectivity.
#include<iostream.h>
#include<conio.h>
void main(){
int choice;
clrscr();
cout<<"1. Client\n2. Server\nEnter choice: ";
cin>>choice;
if(choice == 1)
cout<<"Client sending message...";
else
cout<<"Server receiving message...";
getch();
}
0 Comments