Advertisement
DrAungWinHtut

cipherbasic.cpp

Jul 18th, 2023
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. int encryption();
  5. int decryption();
  6.  
  7. int main()
  8. {
  9.     encryption();
  10.     decryption();
  11.     return 0;
  12. }
  13.  
  14. int encryption()
  15. {
  16.     ifstream iFile;
  17.     ofstream oFile;
  18.     int key = 3;
  19.     char line[200] = "";
  20.     char cipher[200] = "";
  21.     iFile.open("plaintext.txt", ios_base::in);
  22.     oFile.open("cipher.txt", ios_base::out);
  23.  
  24.     if (!iFile.is_open() || !oFile.is_open()) //! - logical NOT operator
  25.     {
  26.         cout << "Input FileIO error!, exiting...." << endl;
  27.         exit(0);
  28.     }
  29.     while (iFile.getline(line, 200))
  30.     {
  31.         puts(line);
  32.         int i = 0;
  33.         for (i = 0;  i < 200; i++)
  34.         {
  35.             cipher[i] = '\0';
  36.         }
  37.         for (i = 0; line[i] != '\0' && i < 200; i++)
  38.         {
  39.             cipher[i] = line[i] + key;
  40.         }
  41.         cipher[i] = '\0';  
  42.         oFile << cipher << endl;
  43.         puts(cipher);
  44.         for (int j = 0; j < 200; j++)
  45.         {          
  46.             line[j] = '\0';
  47.         }      
  48.     }
  49.     iFile.close();
  50.     oFile.close();
  51.     return 0;
  52. }
  53.  
  54. int decryption()
  55. {
  56.     ifstream iFile;
  57.     ofstream oFile;
  58.     int key = 3;
  59.     char line[200] = "";
  60.     char plain[200] = "";
  61.     iFile.open("cipher.txt", ios_base::in);
  62.     oFile.open("original.txt", ios_base::out);
  63.     if (!iFile.is_open() || !oFile.is_open())
  64.     {
  65.         cout << "Input FileIO error!, exiting...." << endl;
  66.         exit(0);
  67.     }
  68.     while (iFile.getline(line, 200))
  69.     {
  70.         puts(line);
  71.         for (int i = 0; i < 200; i++)
  72.         {
  73.             plain[i] = '\0';
  74.         }
  75.         for (int i = 0; line[i] != '\0' && i < 200; i++)
  76.         {
  77.             plain[i] = line[i] - key;
  78.         }
  79.         oFile << plain << endl;
  80.         puts(plain);
  81.     }
  82.     iFile.close();
  83.     oFile.close();
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement