You are on page 1of 21

Dr.

Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Lab Manual

2341 CSA - 3
Application Privacy and Security
[PRACTICAL ACTIVITY BOOK]

Dr. Vasanthi Muniasamy

Department of Computer Science,


Program: Web and Mobile Application
Academic year-2023-2024

Page 1
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Index Page
S. No Marks Given Date Submission Date Signature

1 3

2 3

3 3

4 3

5 3

6 3

7 3

8 3

9 3

10 3

Page 2
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

1. Write a C++ program to convert the plain text to cipher text using Caesar Cipher.

#include <iostream>
#include <string>

using namespace std;

char caesar( char );

int main()
{
string input;

do {
cout << "Enter any text " << endl;
getline(cin, input);
string output = "";
for(int x = 0; x < input.length(); x++)
{
output += caesar(input[x]);
}
cout << output << endl;
} while (!input.length() == 0);
}
char caesar( char c )
{
if( isalpha(c) )
{
c = toupper(c);

c = (((c-65)+13) % 26) + 65;


}
return c;
}

Page 3
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Output:

Page 4
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

2. Write a C++ program to encrypt the plain text to cipher text using columnar transposition cipher.

#include<stdio.h>

#include<cstring>

#include<iostream>
using namespace std;
int main()
{
char col[200][7];
char col_trans[200][7];
int i,j; for(i=0;i<200;i+
+) for(j=0;j<=7;j++)
col[i][j]=' ';
cout<<endl;
cout<<"Enter the text, for columnar transposition:";
cin.getline((char*)col,sizeof(col));
cout<<"\n\nBefore columnar transposition matrixis:\n\n";
for(i=0;i<strlen((char*)col)/7;i++)
{
for(j=0;j<7;j++)
cout<<col[i][j]<<" | ";
cout<<endl;

}
for(i=0;i<strlen((char*)col)/7;i++)
{
for(j=0;j<=7;j++)
{
switch(j)
{
case 0:
col_trans[i][4]=col[i][j];

Page 5
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
break;
case 1:
col_trans[i][3]=col[i][j];
break;
case 2:
col_trans[i][1]=col[i][j];
break;
case 3:
col_trans[i][2]=col[i][j];
break;
case 4:
col_trans[i][5]=col[i][j];
break;
case 5:
col_trans[i][6]=col[i][j];
break;
case 6:
col_trans[i][7]=col[i][j];
break;
}
}
}
cout<<"\n\n After columnar transposition matrixis:\n\n";

for(j=1;j<=7;j++)
for(i=0;i<strlen((char*)col)/7;i++)
{
cout<<col_trans[i][j]<<" ";
}
cout<<endl;
system("pause");

Page 6
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Output:

Page 7
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
3. Using C++ program decrypt the alphapets “a – z”

#include <cctype>
#include <string>
#include <iostream>
#include<conio.h>
using namespace std;
void decrypt(char * input, unsigned int offset) {
for (int i = 0; input[i] != 0; i++) {
if (input[i] == ' ')
continue;
char firstLetter = islower(input[i]) ? 'a' : 'A';
unsigned int alphaOffset = input[i] - firstLetter;
int newAlphaOffset = alphaOffset - offset;

if (newAlphaOffset < 0) {
newAlphaOffset += 26;
}
input[i] = firstLetter + (newAlphaOffset % 26);
}
}
int main()
{
std::string alphabeticalString = "abcdefghijklmnopqrstuvwxyz";
cout << "Decrypted from: \"" << alphabeticalString.c_str();
decrypt(const_cast<char*>(alphabeticalString.c_str()), 6);
cout << "\" to: " << alphabeticalString << endl;
getch();
return 0;
}

Page 8
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Output:

Page 9
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
4. Using C++ program encrypt the given string “WELCOME“.

#include <cctype>
#include <string>
#include <iostream>
#include<conio.h>
using namespace std;

void encrypt(char * input, unsigned int offset)


{
for (int i = 0; input[i] != 0; i++)
{
if (input[i] == ' ')
continue;
char firstLetter = islower(input[i]) ? 'a' : 'A';
unsigned int
alphaOffset = input[i] - firstLetter,
newAlphaOffset = alphaOffset+offset;
input[i] = firstLetter + newAlphaOffset % 26;
}
}
int main() {
std::string alphabeticalString = "acbdefghijklmnopqrstuvwxyz";
cout << "Enter a string to encrypt...\n\t> ";
getline(cin, alphabeticalString);
cout << "Encrypted from: \"" << alphabeticalString.c_str();
encrypt(const_cast<char*>(alphabeticalString.c_str()), 4);
cout << "\" to: " << alphabeticalString << endl;
getch();
return 0;
}

Page 10
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

Output:

5. Write a C++ Program to encrypt and decrypt the given string “MISS YOU”

Page 11
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

#include <cctype>
#include <string>
#include <iostream>
#include<conio.h>
using namespace std;
void encrypt(char * input, unsigned int offset) {
for (int i = 0; input[i] != 0; i++) {
if (input[i] == ' ')
continue;
char firstLetter = islower(input[i]) ? 'a' : 'A';
unsigned int
alphaOffset = input[i] - firstLetter,
newAlphaOffset = alphaOffset+offset;
input[i] = firstLetter + newAlphaOffset % 26;
}
}
void decrypt(char * input, unsigned int offset) {
for (int i = 0; input[i] != 0; i++) {
if (input[i] == ' ')
continue;
char firstLetter = islower(input[i]) ? 'a' : 'A';
unsigned int alphaOffset = input[i] - firstLetter;
int newAlphaOffset = alphaOffset - offset;
if (newAlphaOffset < 0) {
newAlphaOffset += 26;
}
input[i] = firstLetter + (newAlphaOffset % 26);
}
}

int main() {

Page 12
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
std::string alphabeticalString = "acbdefghijklmnopqrstuvwxyz";
cout << "Enter a string to encrypt...\n\t> ";
getline(cin, alphabeticalString);
cout << "Encrypted from: \"" << alphabeticalString.c_str();
encrypt(const_cast<char*>(alphabeticalString.c_str()), 4);
cout << "\" to: " << alphabeticalString << endl;
cout << "Decrypted back from: \"" << alphabeticalString.c_str();
decrypt(const_cast<char*>(alphabeticalString.c_str()), 4);
cout << "\" to: " << alphabeticalString << endl;
getch();
return 0;
}

Output:

6. Convert the plain text to cipher text using transposition cipher in C++.

Page 13
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

#include
<iostream>
//#include<conio.h>
using namespace std;
int main(){
char transposition[]={2,4,0,1,7,3,5,6};
char input[256] = "SECURITY\n";
int len = sizeof(transposition);
char ch, temp[len];
int i, j, k;
j=i=0;
do {

for( ; '\0'!=(ch = input[i]) && ch != '\n';++i){


temp[j++] = ch;
if(j == len){
j=0;
++i;
break;
}
}
while(j!=0){
temp[j++] = '.';//pading if(i % len != 0)
if(j == len)
j = 0;
}
for(k=0;i && k<len;++k)
{ cout<<temp[transposition[k]
]; cout<<"\n";
}
}while(ch != '\n' && ch != '\0');

Page 14
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
getch();
return 0;
}

Output:

Page 15
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

7. Encrypt the given plain text “CATCH ME IF YOU CAN” using Caesar Cipher with Shift of 4.

#include<iostream>
using namespace std;
string encrypt(string text, int s)
{
string result = "";
for (int i=0;i<text.length();i++)
{
if (isupper(text[i]))
result += char(int(text[i]+s-65)%26 +65);
else
result += char(int(text[i]+s-97)%26 +97);
}
return result;
}
int main()
{
string text="CATCH ME IF YOU CAN ";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
cout << "\n ";
system ("pause");
return 0;
}

Output:

Page 16
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

8. Decrypt the given Cipher text “QMWWY” using Caesar Cipher with Shift of 4.

#include<iostream>
using namespace std;
string encrypt(string text, int s)
{
string result = "";
for (int i=0;i<text.length();i++)
{
if (isupper(text[i]))
result += char(int(text[i]-s-65)%26 +65);
else
result += char(int(text[i]-s-97)%26 +97);
}
return result;
}
int main()
{
string text="QMWWY";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
system ("pause");
return 0;
}

Output:

Page 17
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

9. Encrypt the given Plain text “CATCH THE THIEF” using Caesar Cipher by entering the key 3
and get the Cipher text.

#include <iostream>
#include <string>
using namespace std;
int main()
{
char message[100], ch;
int i, key;
cout << "Enter a message to encrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}

Page 18
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus
}

cout << "Encrypted message: " << message;


cout << "\n";
system("pause");
return 0;
}

Output:

Page 19
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

10. Decrypt the given Cipher text “FDWFK” using Caesar Cipher by entering the key 3 and get the
Plain text “CATCH”.

#include <iostream>
#include <string>
using namespace std;
int main()
{
char message[100], ch;
int i, key;
cout << "Enter a message to decrypt: ";
cin.getline(message, 100);
cout << "Enter key: ";
cin >> key;

for(i = 0; message[i] != '\0'; ++i){


ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}

Page 20
Dr. Vasanthi Muniasamy, Department of Computer Science, Al-Mahala Female Campus

cout << "Decrypted message: " << message;


cout << "\n";
system("pause");
return 0;
}

Output:

Page 21

You might also like