Publicidad
Asssignment2
Asssignment2
Asssignment2
Asssignment2
Publicidad
Asssignment2
Asssignment2
Asssignment2
Asssignment2
Asssignment2
Próximo SlideShare
Microsoft Word   Hw#1Microsoft Word Hw#1
Cargando en ... 3
1 de 9
Publicidad

Más contenido relacionado

Publicidad

Asssignment2

  1. JAWARIYA JAWAAD 393/BSIT/F19 ANAM SALEEM 398/BSIT/F19 ASSIGNMENT 2 ScreenShots:
  2. Code: // security.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include<string> using namespace std; char MsgAutoKey[1000], cip[36], enc[1000], decy[1000]; int keyAK[1000]; class Vig { public: string k;
  3. Vig(string k) { for (int i = 0; i <= k.size(); ++i) { if (k[i] >= 'A' && k[i] <= 'Z') this->k += k[i]; else if (k[i] >= 'a' && k[i] <= 'z') this->k += k[i] + 'A' - 'a'; } } string encryption(string t) { string output; for (int i = 0, j = 0; i <= t.length(); ++i) { char c = t[i]; if (c >= 'a' && c <= 'z') c += 'A' - 'a'; else if (c < 'A' || c > 'Z') continue; output += (c + k[j] - 2 * 'A') % 26 + 'A'; //added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ] j = (j + 1) % k.length(); } return output; } string decryption(string t) { string output; for (int i = 0, j = 0; i <= t.length(); ++i) { char c = t[i]; if (c >= 'a' && c <= 'z') c += 'A' - 'a'; else if (c < 'A' || c > 'Z') continue; output += (c - k[j] + 26) % 26 + 'A';//added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ] j = (j + 1) % k.length(); } return output; } }; string encryption(string m) { int a = 1, b = 2; cout << "enter 1 key " << endl; cin >> a; cout << "enter 2 key " << endl;
  4. cin >> b; //Cipher Text initially empty string c = ""; for (int i = 0; i < m.length(); i++) { // Avoid space to be encrypted if (m[i] != ' ') // added 'A' to bring it in range of ASCII alphabet [ 65-90 | A-Z ] c = c + (char)((((a * (m[i] - 'A')) + b) % 26) + 'A'); else //else append space character c += m[i]; } return c; }; int findIndexAK(char ch) { int x; for (int i = 0; i < 36; i++) { if (cip[i] == ch) { x = i; i = 36; } } return x; } void selectiontable(string k, string s) { int i, l = 0, x; cout << "----key select from these words----"<<endl; for (i = 0; i < 10; i++) { cip[l] = i + 48; cout << cip[l]; l++; } cout <<" "<< endl; for (i = 0; i < 26; i++) { cip[l] = i + 97; cout << cip[l]; l++; } cout << " " << endl; cout << endl;
  5. cout << "************Key Code : " ; for (i = 0; i < s.size(); i++) { x = findIndexAK(k[i]); keyAK[i] = x; cout << k[i]; } cout << endl; } int EncryptAK(string s, string k) { int i, l = 0, x, y = 0; for (i = 0; i < s.size(); i++) { if ((s[i] > 47 && s[i] < 58) || (s[i] > 96 && s[i] < 123)) { x = findIndexAK(s[i]); enc[i] = cip[(keyAK[y] + x) % 36]; y++; if (y > k.size()) { y = 0; } } cout << enc[i]; } return i; } void DecryptAK(char s[], string k, int j) { int i, l = 0, x, z, y = 0; for (i = 0; i < j; i++) { if ((s[i] > 47 && s[i] < 58) || (s[i] > 96 && s[i] < 123)) { x = findIndexAK(s[i]); z = x - keyAK[y]; if (z < 0) { z = 36 + z; } decy[i] = cip[z]; y++; if (y > k.size())
  6. { y = 0; } } cout << decy[i]; } return; } int main() { string s, k; string UserWord, Enc, Dec, Plantext, Codetext; int EncDecNumber, Cipher, key,number; do { cout << " Enter Cipher U want to use" << endl; cout << "1: Ceaser Cipher n2: PlayFair Ciphern3: Affine n4: AutoKey Cipher n5: Vigener " << endl; cin >> Cipher; if (Cipher == 1) { char msg[100]; cout << "Ceaser Cipher" << endl; cout << "Enter the message:n"; cin >> msg; //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
  7. else if (ch >= 'A' && ch <= 'Z') { ch = ch + key; if (ch > 'Z') { ch = ch - 'Z' + 'A' - 1; } msg[i] = ch; } } cout<<"Encrypted message: “<< msg<<endl; } 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; } else { cout << "Match NOT Found" << endl; } } else if (Cipher == 2) { cout << "PlayFair Cipher Alphabet" << endl; cout << " Enter any string " << endl; cin >> UserWord; cout << "What do u Wantn1: Encryption norn2: DecryptionnPress number" << endl; cin >> EncDecNumber;
  8. if (EncDecNumber == 1) { cout << "PlayFair Cipher Alphabet Encrypted data" << endl; } else if (EncDecNumber == 2) { cout << "PlayFair Cipher Decrypted data" << endl; } else { cout << "Match NOT Found" << endl; } } else if (Cipher == 3) { cout << "Affine" << endl; cout << " Enter any string " << endl; cin >> UserWord; string c = encryption(UserWord); cout << "Encrypted Message is : " << c << endl; } else if (Cipher == 4) { cout << "Autokey Cipher " << endl; cout << " Enter any string " << endl; cin >> UserWord; cout << "What do u Wantn1: Encryption norn2: DecryptionnPress number" << endl; cin >> EncDecNumber; cout << "Enter the Key : " << endl; cin >> k; int i, j{}, f = 0; for (i = 0; i < UserWord.size(); i++) { if ((UserWord[i] > 47 && UserWord[i] < 58) || (UserWord[i] > 96 && UserWord[i] < 123)) { k.append(UserWord, i, 1); } } selectiontable(k, UserWord); if (EncDecNumber == 1) { cout << "Autokey Cipher Encrypted data" << endl; cout << " your data : " << UserWord; cout << "nEncrypted Message : "; j = EncryptAK(UserWord, k);
  9. } else if (EncDecNumber == 2) { cout << "Autokey Cipher Decrypted data" << endl; cout << " your data : " << UserWord; cout << "nDecrypted Message : "; DecryptAK(enc, k, j); } else { cout << "Match NOT Found" << endl; } } else if (Cipher == 5) { string qi; cout << " enter key" << endl; cin >> qi; Vig v(qi); cout << "Keyless Vigener " << endl; cout << " Enter any string " << endl; cin >> UserWord; cout << "What do u Wantn1: Encryption norn2: DecryptionnPress number" << endl; cin >> EncDecNumber; if (EncDecNumber == 1) { cout << "Vigener Encrypted data" << endl; string encrypt = v.encryption(UserWord); cout << "Original Message: " << UserWord << endl; cout << "Encrypted Message: " << encrypt << endl; } else if (EncDecNumber == 2) { cout << "Vigener Decrypted data" << endl; string decrypt = v.decryption(UserWord); cout << "Decrypted Message: " << decrypt << endl; } else { cout << "Match NOT Found" << endl; } } cout << " enter 0 when u eixt" << endl; }while (Cipher != 0); system("pause"); }
Publicidad