Advertisement
DrAungWinHtut

sub_cipher.cpp

Mar 20th, 2025
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include<iostream>
  2. //#include<array>
  3. #include<cstdlib>
  4. #include<fstream>
  5. #include<ctime>
  6. #include<chrono>
  7.  
  8. using namespace std;
  9. char alphabets[27] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m',
  10.                           'n','o','p','q','r','s','t','u','v','w','x','y','z',' ' };
  11.  
  12. //create an array of numbers 0 to 25
  13. int numbers[27] = { '@','7','1','#','$','9','0','6','!','*','(',')','~','^','8','&','/','3',';','<','2','5','?','[','4',']',' ' };
  14. char* encrypt(char* msg, int total_msg, char* cip, int  total_cip);
  15. char* decrypt(char* cip, int total_cip, char* pln, int  total_pln);
  16.  
  17. int main() {
  18.     //create an array of alphabets a to z
  19.    
  20.  
  21.     char message[1000] = { '\0'};
  22.     char cipher[1000] = { '\0' };
  23.     char plain_text[1000] = { '\0' };
  24.  
  25.     cout << "Enter the message to be encrypted: ";
  26.     cin.getline(message, 1000);
  27.     //encrypting the message
  28.     encrypt(message, 1000, cipher, 1000);
  29.     cout << "The encrypted message is: " << cipher  << endl;
  30.     decrypt(cipher, 1000, plain_text, 1000);
  31.     cout << "The decrypted message is: " << plain_text << endl;
  32.     return 0;
  33. }
  34.  
  35. char* encrypt(char * msg,  int total_msg,char *  cip, int  total_cip) {
  36.     for (int i = 0; i < total_msg; i++) {
  37.         if (msg[i] == '\0') {
  38.             break;
  39.         }
  40.         for (int j = 0; j < 27; j++) {
  41.             if (msg[i] == alphabets[j]) {
  42.                 cip[i] = numbers[j];
  43.                 break;
  44.             }
  45.         }
  46.     }  
  47.     return cip;
  48. }
  49.  
  50. char* decrypt(char* cip, int total_cip, char* pln, int  total_pln) {
  51.     for (int i = 0; i < total_cip; i++) {
  52.         if (cip[i] == '\0') {
  53.             break;
  54.         }
  55.         for (int j = 0; j < 27; j++) {
  56.             if (cip[i] == numbers[j]) {
  57.                 pln[i] = alphabets[j];
  58.                 break;
  59.             }
  60.         }
  61.     }
  62.    
  63.     return pln;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement