LAB Exercises
LAB Exercises
L1
L2
a. Ceaser Cipher
b. Substitution Cipher
c. Hill Cipher
a) Ceaser Cipher
java.util.Scanner;
staticBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));public
static void
System.out.println("\n");
int c= str.charAt(i);
if (Character.isUpperCase(c))
c = c + (key % 26);
if (c > 'Z')
c = c - 26;
else if (Character.isLowerCase(c)) {
c = c + (key % 26);
if (c > 'z')
c = c - 26;
encrypted += (char) c;
return encrypted;
int c= str.charAt(i);
if(Character.isUpperCase(c))
c = c - (key % 26);
if (c < 'A')
c = c + 26;
else if (Character.isLowerCase(c))
{
c = c - (key % 26);
if (c < 'a')
c = c + 26;
Output:
b) Substitution Cipher
PROGRAM:
import java.io.*;
import java.util.*;
staticBufferedReaderbr=newBufferedReader(newInputStreamReader(System.in))
String a
String a= "abcdefghijklmnopqrstuvwxyz";
String b = "zyxwvutsrqponmlkjihgfedcba";
char c;
for(int i=0;i<str.length();i++)
c=str.charAt(i);
int j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
Output:
c)HillCipher
PROGRAM:
import java.io.*;
import java.util.*;
import java.io.*; public class HillCipher {
staticfloat[][] a= newfloat[3][3];
static float[][]b=newfloat[3][3];
staticfloat[][] mes=newfloat[3][1];
for(int i=0;i<3;i++)
for(int j=0;j<1;j++)
(int k=0;k<3;k++) {
res[i][j]=res[i][j]+a[i][k]*mes[k][j];
for(int i=0;i<3;i++)
{ System.out.print((char)(res[i][0]%26+97));
[i][0]=res[i][0];
inverse();
for(int i=0;i<3; i++)
System.out.print((char)(decrypt[i][0]% 26+97));
System.out.print("\n");
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]= sc.nextFloat();
floatp,q;
float[][] c= a;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
//a[i][j]=sc.nextFl oat();
if(i==j)
b[i][j]=1;
else
b[i][j]=0;
for(int i=0;i<3;i++)
p = c[i][k];
q = c[k][k];
for(int 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];
}}}}
{ b[i][j] = b[i][j]/c[i][i]; }
System.out.print("\n"); }
}}
Output:
ii) Write a C program to perform encryption and decryption using the following
algorithms:
a. Ceaser Cipher
b. RSA
c. Hill Cipher
#include <stdio.h>
#include <string.h>
encrypt(text, shift);
printf("Encrypted message: %s\n", text);
decrypt(text, shift);
printf("Decrypted message: %s\n", text);
return 0;
}
b. RSA
int main() {
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0) {
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%ld", &q);
flag = prime(q);
if (flag == 0 || p == q) {
printf("\nWRONG INPUT\n");
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%[^\n]", msg);
for (i = 0; msg[i] != '\0'; 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("%ld\t%ld\n", e[i], d[i]);
encrypt();
decrypt();
return 0;
}
void ce() {
int 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;
}
}
}
void encrypt() {
long int pt, ct, key = e[0], k, len;
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;
}
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]);
}
c) Hill Cipher
// C++ code to implement Hill Cipher
#include <iostream>
using namespace std;
int messageVector[3][1];
int cipherMatrix[3][1];
string CipherText;
HillCipher(message, key);
return 0;
}
Snap_Shorts of Crypttool_Exercise
Snap_Shorts of Wireshark_Exercise