Sie sind auf Seite 1von 8

CRYPTOGRAPHY

DISCRETE ASSINGMENT

SYEDA ZEHRA IKRAM


02-235172-050
BS-IT-3A
Submitted To: Miss Kaneez Fatima
CRPTOGRAPHY:
Cryptography had primarily been based on the use of symmetric keys. In symmetric key
algorithms, two users who wish to communicate a message with each other use the same
cryptographic keys for both the encryption of the plaintext and decryption of the cipher text. The
keys represent a shared secret between the two parties and can be used as a private form of
communication. Both partners need to know the keys to encrypt or decrypt the message.

Types Of Cryptography:
As manually encryption and decryption of any type of cryptography takes time that’s why to
solve that problem and make you able to complete your work faster I have made programs in
each type. From these programs you have only to type your message which you want to encrypt
or decrypt.

Ceaser’s Cipher:
It is one of the simplest encryption technique in which each character in plain text is replaced by
a character some fixed number of positions down to it. For example, if key is 3 then we have to
replace character by another character that is 3 position down to it. Like A will be replaced by D,
C will be replaced by F and so on.
Ceaser Cipher Encryption :
Code :
#include<iostream>
using namespace std;
int main()
{
char message[100], ch;
int i, key;

cout << "ENTER YOUR MESSAGE FOR ENCRYPTION : ";


cin.getline(message, 100);
cout << "ENTER YOUR DESIRE 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;
}
}

cout << "YOUR ENCRYPTED MESSAGE IS : " << message;


return 0;
}

Ceaser Cipher Decryption :

Code:
#include<iostream>
using namespace std;
int main()
{
char message[100], ch;
int i, key;

cout << "ENTER YOUR MESSAGE FOR DECRYPTION : ";


cin.getline(message, 100);
cout << "ENTER YOUR DESIRE KEY : ";
cin >> key;

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


ch = message[i];

if(ch >= 'a' && ch <= 'z'){


ch = ch - key;

if(ch < 'a'){


ch = ch + 'z' - 'a' + 1;
}

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

if(ch > 'a'){


ch = ch + 'Z' - 'A' + 1;
}

message[i] = ch;
}
}

cout << "YOUR DECRYPTED MESSAGE : " << message;


return 0;
}

Affine Cipher :

Introduction:

The Affine cipher is a special case of the more general monoalphabetic substitution cipher.
The cipher is less secure than a substitution cipher as it is vulnerable to all of the attacks that
work against substitution ciphers, in addition to other attacks.

ALGORITHM USE FOR ITS ENCRPTIION AND DECRYPTION:

The 'key' for the Affine cipher consists of 2 numbers, we'll call them a and b. The following
discussion assumes the use of a 26 character alphabet (m = 26). a should be chosen to be
relatively prime to m (i.e. a should have no factors in common with m).

AFFINE ENCRPTION AND DECRPTION:

CODE:
#include<iostream>

using namespace std;


string encryptionMessage(string Msg)
{
string CTxt = "";
int a = 3;
int b = 6;
for (int i = 0; i < Msg.length(); i++)
{
CTxt = CTxt + (char) ((((a * Msg[i]) + b) % 26) + 65);
}
return CTxt;
}

string decryptionMessage(string CTxt)


{
string Msg = "";
int a = 3;
int b = 6;
int a_inv = 0;
int flag = 0;
for (int i = 0; i < 26; i++)
{
flag = (a * i) % 26;
if (flag == 1)
{
a_inv = i;
}
}
for (int i = 0; i < CTxt.length(); i++)
{
Msg = Msg + (char) (((a_inv * ((CTxt[i] - b)) % 26)) + 65);
}
return Msg;
}
int main()
{
cout << "ENTER YOUR MESSAGE : ";
string message;
cin >> message;
cout << "SENT MESSAGE IS :" << message;
cout << "\nENCRYPTED MESSAGE IS : " << encryptionMessage(message);

cout << "\nDECRYPTED MESSAGE IS : " << decryptionMessage(


encryptionMessage(message));
}
RSA CRPTOGRAPY:

INTRODUCTION:
RSA encryption is a public-key encryption technology developed by RSA Data Security. The
RSA algorithm is based on the difficulty in factoring very large numbers. Based on this
principle, the RSA encryption algorithm uses prime factorization as the trap door for
encryption. Deducing an RSA key, therefore, takes a huge amount of time and processing
power. RSA is the standard encryption method for important data, especially data that's
transmitted over the Internet.
RSA stands for the creators of the technique, Rivest, Shamir and Adelman.

RSA INCRPTION AND DECRPTION:

CODE:

#include <iostream>
using namespace std;

int main()
{
int i, x;
char str[100];

cout << "PLEASE ENTER YOUR DESIRE MESSAGE :\t";


cin >> str;

cout << "\nPLEASE CHOSE FOLLOWING OPTIONS :\n";


cout << "1 = ENCRPT THE STRING.\n";
cout << "2 = DECRYPT THE STRING.\n";
cin >> x;
switch(x)
{
case 1:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] + 2;

cout << "\nYOUR MESSAGE AFTER INCRPTION : " << str << endl;
break;
case 2:
for(i = 0; (i < 100 && str[i] != '\0'); i++)
str[i] = str[i] - 2;
cout << "\nYOUR MESSAGE AFTER DECRYPTION : " <<str << endl;
break;

default:
cout << "\nINVALID INPUT !!!\n";
}
return 0;
}

Das könnte Ihnen auch gefallen