isc
unknown
plain_text
2 years ago
7.5 kB
4
Indexable
Never
//one time pad #include<iostream> #include<string.h> using namespace std; int main() { cout << "~~~~~~~~~~~~~~~~~~ Practical 5 ~~~~~~~~~~~~~~~~~~\n"; cout << "| Aim: Perform encryption and decryption |\n"; cout << "| using One Time Pad. |\n"; cout << "-------------------------------------------------\n"; cout << "| Made By: Arunesh Prasad (39) |\n"; cout << "-------------------------------------------------\n"; int i, j, length,choice,lengthOfKey; char msg[100]; char key[100]; cout<<"| Enter the message: "; cin.getline(msg,100); length = strlen(msg); while(true){ cout << "| Enter key with same lenth: "; cin >> key; lengthOfKey = strlen(key); if(lengthOfKey == length){ cout << "-------------------------------------------------\n"; break; }else{ cout << "| Try again with same length! \n"; cout << "-------------------------------------------------\n"; } } cout <<"| -> Enter your choice \n| 1. Encryption \n| 2. Decryption \n"; cout << "| -> Your choice: "; cin>>choice; if (choice==1){ int chToIntInputText,chToIntInputKey; for(int i = 0; msg[i] != '\0'; ++i) { chToIntInputText = int(msg[i]); chToIntInputKey = int(key[i]); if (chToIntInputText >= 97 && chToIntInputText <= 122){ chToIntInputText = chToIntInputText + chToIntInputKey; if (chToIntInputText > 122) { chToIntInputText = chToIntInputText - 97; chToIntInputText = chToIntInputText % 26; chToIntInputText = chToIntInputText + 97; } msg[i] = char(chToIntInputText); } else if (chToIntInputText >= 65 && chToIntInputText <= 90){ chToIntInputText = chToIntInputText + chToIntInputKey; if (chToIntInputText > 90) { chToIntInputText = chToIntInputText - 65; chToIntInputText = chToIntInputText % 26; chToIntInputText = chToIntInputText + 65; } msg[i] = char(chToIntInputText); } } cout << "| Encrypted message: " << msg << "\n"; cout << "-------------------------------------------------\n"; } else if(choice == 2) { int chToIntInputText,chToIntInputKey; for(int i = 0; msg[i] != '\0'; ++i) { chToIntInputText = char(msg[i]); chToIntInputKey = int(key[i]); if (chToIntInputText >= 97 && chToIntInputText <= 122){ chToIntInputText = chToIntInputText - chToIntInputKey; if (chToIntInputText < 97) { chToIntInputText = -(chToIntInputText - 97); chToIntInputText = 26 - (chToIntInputText % 26); chToIntInputText = 97 + chToIntInputText; } msg[i] = char(chToIntInputText); } else if(chToIntInputText >= 65 && chToIntInputText <= 90){ chToIntInputText = chToIntInputText - chToIntInputKey; if (chToIntInputText < 65) { chToIntInputText = -(chToIntInputText - 65); chToIntInputText = 26 - (chToIntInputText % 26); chToIntInputText = 90 - chToIntInputText; } msg[i] = char(chToIntInputText); } } cout << "| Decrypted message: " << msg << "\n"; cout << "-------------------------------------------------\n"; } } //extended euclidean #include <iostream> #include <bits/stdc++.h> #include <tuple> using namespace std; //Recursive function to demonstrate the extended Euclidean algorithm tuple<int, int, int> extended_gcd(int a, int b) { if (a == 0) { return make_tuple(b, 0, 1); } int gcd, x, y; // unpack tuple returned by function into variables tie(gcd, x, y) = extended_gcd(b % a, a); return make_tuple(gcd, (y - (b/a) * x), x); } int main() { int a, b; cout<<"\n--"; cout << "\n\t\t+-----------------+" << endl; cout << "\t\t| Practical No. 6 |" << endl; cout << "\t\t+-----------------+" << endl; cout << "\t BY : Jaanhavi Gautam [A_21]" << endl; cout << "\nAIM : Write a program to implement extended Euclidean Algorithm"<<endl; cout<<"\n\nEnter the first number: "; cin>>a; cout<<"\nEnter the second number: "; cin>>b; tuple<int, int, int> t = extended_gcd(a, b); int gcd = get<0>(t); int x = get<1>(t); int y = get<2>(t); cout << "The GCD is : " << gcd << endl; cout << "where, x = " << x << " and y = " << y << endl; cout << "The equation ax + by = gcd(a, b) will be" << endl; cout << a << "*" << x << " + " << b << "*" << y << " = " << gcd << endl; return 0; } //RSA #include<iostream> #include<math.h> using namespace std; int gcd(int a, int b) { int t; while(1) { t= a%b; if(t==0) return b; a=b; b=t; } } int main() { double p = 13; double q = 11; double n=p*q;//calculate phi double phi= (p-1)*(q-1);//calculate phi double e=7; double track; while(e<phi) { track = gcd(e,phi); if(track==1) break; else e++; } double d1=1/e; double d=fmod(d1,phi); double message = 9; double c = pow(message,e); //encrypt the message double m = pow(c,d); c=fmod(c,n); m=fmod(m,n); cout<<"A_21 Jaanhavi Gautam"<<endl; cout<<"Original Message = "<<message; cout<<"\n"<<"p = "<<p; cout<<"\n"<<"q = "<<q; cout<<"\n"<<"n = pq = "<<n; cout<<"\n"<<"phi = "<<phi; cout<<"\n"<<"e = "<<e; cout<<"\n"<<"d = "<<d; cout<<"\n"<<"Encrypted message = "<<c; cout<<"\n"<<"Decrypted message = "<<m; return 0; } //deffie-hellmen #include<iostream> #include<cstdio> using namespace std; class DiffieHellman { public: long long int p,g,x,a,y,b,A,B; DiffieHellman(long long int p1,long long int g1,long long int x1,long long int y1) { p = p1; g = g1; x = x1; y = y1; a=power(g,x,p); b=power(g,y,p); A = power(b,x,p); B = power(a,y,p); } 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; int main() { long long int p,g,x,a,y,b,A,B; cout<<"Executed by A_21 Jaanhavi Gautam"<<endl; cout<<"Enter the values of p and g upon which User A And User B both will aggree : "<<endl; cin>>p>>g; cout<<"Enter the Secret Integer for User A : "; cin>>x; cout<<"Enter the Secret Integer for User B : "; cin>>y; cout<<endl; DiffieHellman dh(p,g,x,y); cout<<"User A's private key, known only to User A : "<<dh.a<<endl; cout<<"User B's private key known only to User B : "<<dh.b<<endl; cout<<endl; cout<<"User A's public key, known to User A and User B : "<<dh.A<<endl; cout<<"User B's public key, known to User A and User B : "<<dh.B<<endl; return 0; }