All in One
Aishu
plain_text
4 years ago
18 kB
7
Indexable
//Practical 1
#include<iostream>
#include<string.h>
using namespace std;
int main() {
cout<<"Enter the message:\n";
char msg[100];
cin.getline(msg,100); //take the message as input
int i, j, length,choice,key;
cout << "Enter key: ";
cin >> key; //take the key as input
length = strlen(msg);
cout<<"Enter your choice \n1. Encryption \n2. Decryption \n";
cin>>choice;
if (choice==1) //for encryption{
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//encrypt for lowercase letter
If (ch >= 'a' && ch <= 'z'){
ch = ch + key;
if (ch > 'z') {
ch = ch - 'z' + 'a' - 1;
}
msg[i] = ch;
}
//encrypt for uppercase letter
else if (ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if (ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
msg[i] = ch;
}
}
printf("Encrypted message: %s", msg);
}
else if (choice == 2) { //for decryption
char ch;
for(int i = 0; msg[i] != '\0'; ++i) {
ch = msg[i];
//decrypt for lowercase letter
if(ch >= 'a' && ch <= 'z') {
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
msg[i] = ch;
}
//decrypt for uppercase letter
else if(ch >= 'A' && ch <= 'Z') {
ch = ch - key;
if(ch < 'A') {
ch = ch + 'Z' - 'A' + 1;
}
msg[i] = ch;
}
}
cout << "Decrypted message: " << msg;
}
}
//Practical 2
key=input("Enter key : ")
key=key.replace(" ", "")
key=key.upper()
def matrix(x,y,initial):
return [[initial for i in range(x)] for j in range(y)]
result=list()
for c in key: #storing key
if c not in result:
if c=='J':
result.append('I')
else:
result.append(c)
flag=0
for i in range(65,91): #storing other character
if chr(i) not in result:
if i==73 and chr(74) not in result:
result.append("I")
flag=1
elif flag==0 and i==73 or i==74:
pass
else:
result.append(chr(i))
k=0
my_matrix=matrix(5,5,0) #initialize matrix
for i in range(0,5): #making matrix
for j in range(0,5):
my_matrix[i][j]=result[k]
k+=1
def locindex(c): #get location of each character
loc=list()
if c=='J':
c='I'
for i ,j in enumerate(my_matrix):
for k,l in enumerate(j):
if c==l:
loc.append(i)
loc.append(k)
return loc
def encrypt(): #Encryption
msg=str(input("\nENTER MSG: "))
msg=msg.upper()
msg=msg.replace(" ", "")
i=0
for s in range(0,len(msg)+1,2):
if s<len(msg)-1:
if msg[s]==msg[s+1]:
msg=msg[:s+1]+'X'+msg[s+1:]
if len(msg)%2!=0:
msg=msg[:]+'X'
print("\nCIPHER TEXT:",end=' ')
while i<len(msg):
loc=list()
loc=locindex(msg[i])
loc1=list()
loc1=locindex(msg[i+1])
if loc[1]==loc1[1]:
print("{}{}".format(my_matrix[(loc[0]+1)%5][loc[1]],my_matrix[(loc1[0]+1)%5][loc1[1]]),end=' ')
elif loc[0]==loc1[0]:
print("{}{}".format(my_matrix[loc[0]][(loc[1]+1)%5],my_matrix[loc1[0]][(loc1[1]+1)%5]),end=' ')
else:
print("{}{}".format(my_matrix[loc[0]][loc1[1]],my_matrix[loc1[0]][loc[1]]),end=' ')
i=i+2
def decrypt(): #decryption
msg=str(input("\nENTER CIPHER TEXT: "))
msg=msg.upper()
msg=msg.replace(" ", "")
print("\nPLAIN TEXT: ",end=' ')
i=0
while i<len(msg):
loc=list()
loc=locindex(msg[i])
loc1=list()
loc1=locindex(msg[i+1])
if loc[1]==loc1[1]:
print("{}{}".format(my_matrix[(loc[0]-1)%5][loc[1]],my_matrix[(loc1[0]-1)%5][loc1[1]]),end=' ')
elif loc[0]==loc1[0]:
print("{}{}".format(my_matrix[loc[0]][(loc[1]-1)%5],my_matrix[loc1[0]][(loc1[1]-1)%5]),end=' ')
else:
print("{}{}".format(my_matrix[loc[0]][loc1[1]],my_matrix[loc1[0]][loc[1]]),end=' ')
i=i+2
while(1):
choice=int(input("\n 1.Encryption \n 2.Decryption: \n 3.EXIT\n"))
if choice==1:
encrypt()
elif choice==2:
decrypt()
elif choice==3:
exit()
else:
print("Choose correct choice: ")
//Practical 3
#include<iostream>
#include<string.h>
using namespace std;
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;
}
cout<<"\nEncrypted Message: ";
for(i = 0; i < key; ++i)
for(j = 0; j < msgLen; ++j)
if(railMatrix[i][j] != '\n')
cout<<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;
cout<<"\nDecrypted Message: ";
for(i = 0; i < msgLen; ++i){
cout<<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;
cout<<"Original Message: "<<msg;
encryptMsg(msg, key);
decryptMsg(enMsg, key);
return 0;
}
//Practical 4:
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String line = System.getProperty("line.separator");
scan.useDelimiter(line);
System.out.print("1. Encryt 2.Decrypt : ");
int option = scan.nextInt();
switch (option) {
case 1:
System.out.print("Enter String:");
String text = scan.next();
System.out.print("Enter Key:");
String key = scan.next();
System.out.println(encryptCT(key, text).toUpperCase());
break;
case 2:
System.out.print("Enter Encrypted String:");
text = scan.next();
System.out.print("Enter Key:");
key = scan.next();
System.out.println(decryptCT(key, text));
break;
default:
break;
}
}
public static String encryptCT(String key, String text) {
int[] arrange = arrangeKey(key);
int lenkey = arrange.length;
int lentext = text.length();
int row = (int) Math.ceil((double) lentext / lenkey);
char[][] grid = new char[row][lenkey];
int z = 0;
for (int x = 0; x < row; x++) {
for (int y = 0; y < lenkey; y++) {
if (lentext == z) {
// at random alpha for trailing null grid
grid[x][y] = RandomAlpha();
z--;
} else {
grid[x][y] = text.charAt(z);
}
z++;
}
}
String enc = "";
for (int x = 0; x < lenkey; x++) {
for (int y = 0; y < lenkey; y++) {
if (x == arrange[y]) {
for (int a = 0; a < row; a++) {
enc = enc + grid[a][y];
}
}
}
}
return enc;
}
public static String decryptCT(String key, String text) {
int[] arrange = arrangeKey(key);
int lenkey = arrange.length;
int lentext = text.length();
int row = (int) Math.ceil((double) lentext / lenkey);
String regex = "(?<=\\G.{" + row + "})";
String[] get = text.split(regex);
char[][] grid = new char[row][lenkey];
for (int x = 0; x < lenkey; x++) {
for (int y = 0; y < lenkey; y++) {
if (arrange[x] == y) {
for (int z = 0; z < row; z++) {
grid[z][y] = get[arrange[y]].charAt(z);
}
}
}
}
String dec = "";
for (int x = 0; x < row; x++) {
for (int y = 0; y < lenkey; y++) {
dec = dec + grid[x][y];
}
}
return dec;
}
public static char RandomAlpha() {
//generate random alpha for null space
Random r = new Random();
return (char)(r.nextInt(26) + 'a');
}
public static int[] arrangeKey(String key) {
//arrange position of grid
String[] keys = key.split("");
Arrays.sort(keys);
int[] num = new int[key.length()];
for (int x = 0; x < keys.length; x++) {
for (int y = 0; y < key.length(); y++) {
if (keys[x].equals(key.charAt(y) + "")) {
num[y] = x;
break;
}
}
}
return num;
}
}
//Practical 5:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the plain text : ");
String text = sc.nextLine();
String key = RandomAlpha(text.length());
String enc = OTPEncryption(text,key);
System.out.println("\nPlaintext : "+text);
System.out.println("\nKey : "+key);
System.out.println("\nEncrypted : "+enc);
System.out.println("\nDecrypted : "+OTPDecryption(enc,key));
}
public static String RandomAlpha(int len){
Random r = new Random();
String key = "";
for(int x=0;x<len;x++)
key = key + (char) (r.nextInt(26) + 'A');
return key;
}
public static String OTPEncryption(String text,String key){
String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alphaL = "abcdefghijklmnopqrstuvwxyz";
int len = text.length();
String sb = "";
for(int x=0;x<len;x++){
char get = text.charAt(x);
char keyget = key.charAt(x);
if(Character.isUpperCase(get)){
int index = alphaU.indexOf(get);
int keydex = alphaU.indexOf(Character.toUpperCase(keyget));
int total = (index + keydex) % 26;
sb = sb+ alphaU.charAt(total);
}
else if(Character.isLowerCase(get)){
int index = alphaL.indexOf(get);
int keydex = alphaU.indexOf(Character.toLowerCase(keyget));
int total = (index + keydex) % 26;
sb = sb+ alphaL.charAt(total);
}
else{
sb = sb + get;
}
}
return sb;
}
public static String OTPDecryption(String text,String key){
String alphaU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alphaL = "abcdefghijklmnopqrstuvwxyz";
int len = text.length();
String sb = "";
for(int x=0;x<len;x++){
char get = text.charAt(x);
char keyget = key.charAt(x);
if(Character.isUpperCase(get)){
int index = alphaU.indexOf(get);
int keydex = alphaU.indexOf(Character.toUpperCase(keyget));
int total = (index - keydex) % 26;
total = (total<0)? total + 26 : total;
sb = sb+ alphaU.charAt(total);
}
else if(Character.isLowerCase(get)){
int index = alphaL.indexOf(get);
int keydex = alphaU.indexOf(Character.toLowerCase(keyget));
int total = (index - keydex) % 26;
total = (total<0)? total + 26 : total;
sb = sb+ alphaL.charAt(total);
}
else{
sb = sb + get;
}
}
return sb;
}
}
//Practical 6:
#include <iostream>
#include <tuple> // std::tuple, std::make_tuple, std::tie
using namespace std;
// Recursive function to demonstrate the extended Euclidean algorithm.
// It returns multiple values using tuple in C++.
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 = 30;
int b = 50;
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 << "x = " << x << " y = " << y << endl;
cout << a << "*" << x << " + " << b << "*" << y << " = " << gcd << endl;
return 0;
}
//Practical 7:
#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, q, e, message, track;
cout << "\n\t\t+-----------------+" << endl;
cout << "\t\t| Practical No. 7 |";
cout << "\n\t\t+-----------------+" << endl;
cout << " \n \t BY: Aishwarya Patle [A_07]" << endl;
cout << "\nAIM : Write a program to develop a secure system" << endl;
cout << "\tby applying RSA Cryptography Algorithm" << endl;
cout << "\n Enter the first prime number (p): ";
cin >> p;
cout << "\n Enter the second prime number (q): ";
cin >> q;
double n = p * q;
double phi = (p - 1) * (q - 1);
cout << "\n Enter the value of e : ";
cin >> e;
while (e < phi)
{
track = gcd(e, phi);
if (track == 1)
break;
else
e++;
}
double d1 = 1 / e;
double d = fmod(d1, phi);
cout << "\n Enter the plaintext (M): ";
cin >> message;
double c = pow(message, e);
double m = pow(c, d);
c = fmod(c, n);
m = fmod(m, n);
cout << "\n\n\t\t+---------------+" << endl;
cout << "\t\t| RSA Algorithm |";
cout << "\n\t\t+---------------+" << endl;
cout << "\n\n The Original Message (M)is = " << message;
cout << "\n\n The value of first prime number (p) = " << p;
cout << "\n\n The value of second prime number (q) = " << q;
cout << "\n\n The value of n = p*q = " << n;
cout << "\n\n The value of phi = " << phi;
cout << "\n\n The value of e = " << e;
cout << "\n\n The value of d = " << d;
cout << "\n\n The Encrypted message (C) = " << c;
cout << "\n\n The Decrypted message (M) = " << m;
return 0;
}
//Practical no.8
#include<iostream>
#include<cstdio>
#include<math.h>
using namespace std;
int UserA(int,int,int);
int UserB(int,int,int);
int main()
{
long int g, x, y, a, b, k1, k2, n;
cout << "\n\t\t+-----------------+" << endl;
cout << "\t\t| Practical No. 8 |";
cout << "\n\t\t+-----------------+" << endl;
cout << " \t BY: Aishwarya Patle [A_07]" << endl;
cout << "\n AIM : Write a program to implement Diffie-Hellman Key Exchange." <<endl;
cout << "\n\t Enter the value of q (prime number) : ";
cin >> g;
cout << "\t Enter the value of α (primitive root of q) : ";
cin >> n;
cout << "\n\t Enter the value of private key XA (for User A) : ";
cin >> x;
cout << "\t Enter the value of private key XB (for User B) : ";
cin >> y;
a = UserA(n,g,x);
cout << "\n\t The public key YA = α^(XA)*mod q : "<<a;
b = UserB(n,g,y);
cout << "\n\t The public key YB = α^(XB)*mod q : "<<b;
k1 = UserA(n,b,x);
cout << "\n\n\t Secret Key calculated by User A (K1) : "<<k1;
k2 = UserB(n,a,y);
cout << "\n\t Secret Key calculated by User B (K2) : "<<k2;
return 0;
}
int UserA(int n,int g,int x)
{
long int a,a1;
a1=pow(g,x);
a=a1%n;
return(a);
}
int UserB(int n,int g,int y)
{
long int b,b1,k2,t2;
b1=pow(g,y);
b=b1%n;
return(b);
}Editor is loading...