Advertisement
Guest User

C++ encryption

a guest
Feb 11th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include <iostream>
  2. //#include <math.h>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. string encrypt(char str[],short key);
  9. string decrypt(char str[],short key);
  10.  
  11. int main(int argc, char **argv)
  12. {
  13.     /*char str[150];
  14.     cout << "enter string to encrypt: ";
  15.     cin >> str;
  16.     encrypt(str,8);
  17.     cout << "\nencrypted: " << str;
  18.     decrypt(str,8);
  19.     cout << "\n\ndecrypted: " << str; */
  20.  
  21.     char filename[85];
  22.     char NewFilename[85];
  23.     string str;
  24.     string a1;
  25.     cout << "enter file to encrypt: ";
  26.     cin >> filename;
  27.     ifstream ifile(filename);
  28.     if(!ifile | !ifile.good())
  29.     {
  30.         cout << "error, invalid file.\n\n";
  31.         main(1,'\0');
  32.     }
  33.     cout << "enter new filename: ";
  34.     cin >> NewFilename;
  35.     ofstream ofile(NewFilename);
  36.     if(!ofile)
  37.     {
  38.         cout << "error, file failed to create.\n\n";
  39.         main(1,'\0');
  40.     }
  41.     int num;
  42.     for(int i = 0; !ifile.eof(); i++)
  43.     {
  44.         getline(ifile,a1);
  45.         str.append(a1);
  46.     }
  47. //*******************************//
  48.     char abc[str.size() +1];
  49.     for(int i = 0; i <= str.size(); i++)
  50.         abc[i] = str[i];
  51. //convert 'str' to a char array
  52. //*******************************//
  53.     encrypt(abc,8);
  54.     cout <<"\n\n\n"<< abc<<"\n\n\n";
  55.     ofile << abc;
  56.     return 0;
  57. }
  58.  
  59. string encrypt(char str[],short key) //encrypt function
  60. {
  61.     int a = 256-key;
  62.     for(int i =  0; i < sizeof(str); i++)
  63.     {
  64.         if(str[i] < a)
  65.         {
  66.             for(int b = 0; b < key; b++)
  67.                 (int)str[i]++;
  68.         }
  69.     }
  70.     return str;
  71. }
  72.  
  73. string decrypt(char str[],short key) //decrypt function
  74. {
  75.     for(int i = 0; i < sizeof(str); i++)
  76.     {
  77.         int a = 256-key;
  78.         if(str[i] < a)
  79.         {
  80.             for(int b = 0; b < key; b++)
  81.                 (int)str[i]--;
  82.         }
  83.     }
  84.     return str;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement