Advertisement
DrAungWinHtut

cipher.cpp

Mar 23rd, 2025
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4.  
  5. using namespace std;
  6. char* encrypt(const char* message, const int count, char* cipher, const int cipher_count);
  7. char* decrypt(const char* cipher, const int count, char* plain, const int plain_count);
  8. const char alpha[28] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  9. const char specialChars[28] = {
  10.         '-', '!', '+', '\\', '~', '@', '^', '$', '[', '/',
  11.         '>', ']', '#', '}', '(', '%', '*', '<', '}', '&',
  12.         ')', ' ', ';', '|', ':', ',', '"','\0' // Space at index 21, quote at index 26
  13. };
  14.  
  15.  
  16. int main()
  17. {
  18.     /*for (int i = 0; i < 28; i++)
  19.     {
  20.         if (alpha[i] == '\0')
  21.             break;
  22.        
  23.         cout << alpha[i] << " - "<<specialChars[i]<<endl;
  24.     }*/
  25.    
  26.     char message[10000] = { '\0' };
  27.     char cipher[10000] = { '\0' };
  28.     char plain[10000] = { '\0' };
  29.     cout << "Enter the message to be encrypted: ";
  30.     cin.getline(message, 10000);
  31.     int count = strlen(message);
  32.     encrypt(message, count,cipher,10000);
  33.  
  34.     cout << endl;
  35.     for (int i = 0; i < count; i++)
  36.     {
  37.         cout << cipher[i];
  38.     }
  39.     cout << endl;
  40.  
  41.     decrypt(cipher, count, plain, 10000);
  42.     for (int i = 0; i < count; i++)
  43.     {
  44.         cout << plain[i];
  45.     }
  46.     cout << endl;
  47.     return 0;
  48. }
  49.  
  50. char* encrypt(const char* message, const int count,char *cipher,const int cipher_count)
  51. {  
  52.     char msg = '\0';   
  53.     for (int i = 0; i < count; i++)
  54.     {
  55.         for (int j = 0; j < 28; j++)
  56.         {
  57.             if (message[i] == '\0')
  58.                 break;
  59.             if (message[i] >96)
  60.             {
  61.                 msg = message[i] - ('a' - 'A');            
  62.             }
  63.             else
  64.             {
  65.                 msg = message[i];
  66.             }
  67.            
  68.  
  69.             if (msg == alpha[j])
  70.             {
  71.                 cipher[i] = specialChars[j];
  72.                 break;
  73.             }
  74.         }
  75.     }
  76.     return cipher;
  77. }
  78.  
  79. char* decrypt(const char* cipher, const int count, char* plain, const int plain_count)
  80. {
  81.     char cip = '\0';
  82.     for (int i = 0; i < count; i++)
  83.     {
  84.         for (int j = 0; j < 28; j++)
  85.         {
  86.             if (cipher[i] == '\0')
  87.                 break;
  88.             cip = cipher[i];
  89.  
  90.  
  91.             if (cip == specialChars[j])
  92.             {
  93.                 plain[i] = alpha[j];
  94.                 break;
  95.             }
  96.         }
  97.     }
  98.     return plain;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement