ISExp 2
ISExp 2
02
SIGNATURE OF LECTURER/
DATE OF ASSESSMENT:
TTA:
Theory:
1) Ceaser cipher: The earliest known, and the simplest, use of a substitution
cipher was by Julius Caesar. The Caesar cipher involves replacing each
letter of the alphabet with the letter standing three places further down the
alphabet.
The general Caesar algorithm is:
Decryption:
1
4) Hill cipher: Another interesting multilateral cipher is the Hill cipher,
developed by the mathematician Lester Hill in 1929. We define the inverse
M-1 of a square matrix M by the equation M (M-1 ) = M-1 M = I, where I
is the identity matrix. I is a square matrix that is all zeros except for ones
along the main diagonal from upper left to lower right. The inverse of a
matrix does not always exist, but when it does, it satisfies the preceding
equation.
Decryption:
2
6) One-Time Pad: An Army Signal Corp officer, Joseph Mauborgne,
proposed an improvement to the Vernam cipher that yields the ultimate in
security. Mauborgne suggested using a random key that is as long as the
message, so that the key need not be repeated. In addition, the key is to be
used to encrypt and decrypt a single message, and then is discarded. Each
new message requires a new key of the same length as the new message.
Such a scheme, known as a one-time pad, is unbreakable. It produces
random output that bears no statistical relationship to the plaintext.
Because the cipher text contains no information whatsoever about the
plaintext, there is simply no way to break the code.
The one-time pad offers complete security but, in practice, has two
fundamental difficulties:
1. There is the practical problem of making large quantities of random
keys. Any heavily used system might require millions of random characters
on a regular basis. Supplying truly random characters in this volume is a
significant task.
2. Even more daunting is the problem of key distribution and protection.
For every message to be sent, a key of equal length is needed by both
sender and receiver. Thus, a mammoth key distribution problem exists.
Transposition techniques:
1) Rail fence: The rail fence cipher (also called a zigzag cipher) is
a classical type of transposition cipher. It derives its name from the manner
in which encryption is performed, in analogy to a fence built with
horizontal rails.
3
2) Row Column technique: In cryptography, transposition ciphers are a type
of encryption method that involves rearranging the order of characters in
the plaintext to produce the cipher text. Columnar transposition cipher is
one such method that involves transposing the plaintext by columns before
encrypting it.
int main() {
// Declaring variables
int i, key;
char s[1000], c;
// Taking Input
printf("Enter a plaintext to encrypt:\n");
fgets(s, sizeof(s), stdin);
printf("Enter key:\n");
scanf("%d", &key);
int n = strlen(s);
4
} else if (c >= 'A' && c <= 'Z') {
c = c + key;
if (c > 'Z') {
c = c - 'Z' + 'A' - 1;
}
s[i] = c;
}
}
return 0;
}
Output:
int main() {
// Declaring variables
int i, key;
char s[1000], c;
int n = strlen(s);
5
// Decrypting each character according to the given key
for (i = 0; i < n; i++) {
c = s[i];
if (c >= 'a' && c <= 'z') {
c = c - key;
if (c < 'a') {
c = c + 'z' - 'a' + 1;
}
s[i] = c;
} else if (c >= 'A' && c <= 'Z') {
c = c - key;
if (c < 'A') {
c = c + 'Z' - 'A' + 1;
}
s[i] = c;
}
}
return 0;
}
Output:
6
'j', 'm' }, { 'k', 'o' }, { 'l', 'p' }, { 'm', 'q' }, { 'n', 'r' }, {
'o', 's' }, { 'p', 't' }, { 'q', 'v' }, { 'r', 'w' }, { 's', 'x' }, {
't', 'y' }, { 'u', 'z' }, { 'v', 'b' }, { 'w', 'c' }, { 'x', 'd' }, {
'y', 'e' }, { 'z', 'h' } };
char str[20];
int main() {
char str[20], str2[20];
int i;
printf("\n Enter String..");
gets(str);
for (i = 0; str[i]; i++) {
str2[i] = monocipher_encr(str[i]);
}
str2[i] = '\0';
printf("\n Before Decryption..%s", str);
printf("\n After Decryption..%s\n", str2);
}
char monocipher_encr(char a) {
int i;
for (i = 0; i < 27; i++) {
if (a == alpha[i][0])
break;
}
return alpha[i][1];
}
Output:
7
#include <string.h> //header file
#define SIZE 30
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}
dicty['j' - 97] = 1;
i = 0;
8
j = 0;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {
9
for (j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}
10
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
// this function will encrypt cipher text using Playfair Cipher algorithm
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
// main code
int main()
{
char str[SIZE], key[SIZE];
11
// key text
strcpy(key, "Algorithm");
printf("Key text: %s\n", key);
// Plaintext
strcpy(str, "Programming");
printf("Plain text: %s\n", str);
return 0;
}
Output:
12
for(i=0;i<=l1-1;i++)
{
if(p[i]>=97&&p[i]<=122)
{
c[i]=(((p[i]-97)+(key[j]-97))%26)+97;
c[i+1]='\0';
}
else if(p[i]>=65&&p[i]<=90)
{
c[i]=(((p[i]-65)+(key[j]-65))%26)+65;
c[i+1]='\0';
}
else
{
c[i]=p[i];
c[i+1]='\0';
}
if(j<l2-1)
{
j++;
}
else
{
j=0;
}
}
j=0;
for(i=0;i<=l1-1;i++)
{
if(p[i]>=97&&p[i]<=122)
{
d[i]=((((c[i]-97)-(key[j]-97))+26)%26)+97;
d[i+1]='\0';
}
else if(p[i]>=65&&p[i]<=90)
{
d[i]=((((c[i]-65)+(key[j]-65))+26)%26)+65;
13
d[i+1]='\0';
}
else
{
d[i]=c[i];
d[i+1]='\0';
}
if(j<l2-1)
{
j++;
}
else
{
j=0;
}
}
printf("The encrypted word is :- ");
puts(c);
printf("\n The Decrypted word is :- ");
puts(d);
getch();
}
Output:
14
char msg[20];
int determinant = 0, t = 0;
;
printf("Enter plain text\n ");
scanf("%s", msg);
for (i = 0; i < 3; i++) {
c[i] = msg[i] - 65;
printf("%d ", c[i]);
}
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26;
}
printf("\nEncrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26;
}
printf("\nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}
Output:
15
Program 7) write a program for encryption using one time pad cipher.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
//All the text which ever entered is converted to upper and without spaces
int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];
char str[100],key[100],cipher[100];
printf("Enter a string text to encrypt\n");
gets(str);
for(i=0,j=0;i<strlen(str);i++)
{
if(str[i]!=' ')
{
str[j]=toupper(str[i]);
j++;
}
}
str[j]='\0';
//obtaining numerical plain text ex A-0,B-1,C-2
for(i=0;i<strlen(str);i++)
{
numstr[i]=str[i]-'A';
}
printf("Enter key string of random text\n");
gets(key);
for(i=0,j=0;i<strlen(key);i++)
{
if(key[i]!=' ')
{
key[j]=toupper(key[i]);
j++;
}
}
key[j]='\0';
//obtaining numerical one time pad(OTP) or key
16
for(i=0;i<strlen(key);i++)
{
numkey[i]=key[i]-'A';
}
for(i=0;i<strlen(str);i++)
{
numcipher[i]=numstr[i]+numkey[i];
}
for(i=0;i<strlen(str);i++)
{
if(numcipher[i]>25)
{
numcipher[i]=numcipher[i]-26;
}
}
printf("One Time Pad Cipher text is\n");
for(i=0;i<strlen(str);i++)
{
printf("%c",(numcipher[i]+'A'));
}
printf("\n");
Output:
18
Output:
int main() {
char *message, *dmessage;
strcpy(temp, key);
klen = strlen(key);
k = 0;
19
for (i = 0;; i++) {
if (flag == 1)
break;
k = 0;
emessage[k] = '\0';
printf("\nEncrypted message is\n");
for (i = 0; emessage[i] != NULL; i++)
printf("%c", emessage[i]);
printf("\n\n");
//deciphering
20
emlen = strlen(emessage);
//emlen is length of encrypted message
strcpy(temp, key);
k = 0;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
printf("%c ", darr[i][j]);
//retrieving message
retmessage[k++] = darr[i][j];
}
printf("\n");
}
retmessage[k] = '\0';
21
for (i = 0; retmessage[i] != NULL; i++)
printf("%c", retmessage[i]);
getch();
return (0);
}
int findMin() {
int i, j, min, index;
min = temp[0];
index = 0;
for (j = 0; temp[j] != NULL; j++) {
if (temp[j] < min) {
min = temp[j];
index = j;
}
}
22
temp[index] = 123;
return (index);
}
Output:
23
24