Advertisement
Mizuhara_Chizuru

Project Enc-Dec Final

Mar 14th, 2022
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.42 KB | None | 0 0
  1. /*Code by Surya a.k.a Sunny*/
  2. /* by https://www.codechef.com/users/spsc */
  3. #include <bits/stdc++.h>
  4. #include <string.h>
  5. #include <conio.h>
  6. #include<stdlib.h>
  7. //#include <boost/multiprecision/cpp_int.hpp>
  8. #define lli long long
  9. #define pi 3.14159265358979323846
  10. #define MOD 1000000007
  11. #define foi(n)  for(lli i=0;i<n;i++)
  12. #define foj(n)  for(lli j=0;j<n;j++)
  13. #define test(T) lli T;cin>>T;while(T--)
  14. #define loop(i, a, b) for(int i = (a); i<= (b); i++)
  15. #define unbuffer cin.clear(); cin.sync();
  16. #define unbuffer2 cout.flush();
  17. #define option cout<<"Choose from one of the options below : \n\n\n";
  18. using namespace std;
  19. lli pass=0;
  20. string ans,backup;
  21. //using namespace boost::multiprecision;
  22. string decrypt(string backup)
  23.   {
  24.     lli temp,len=backup.length(),i=0;
  25.     char ans2[len],x;
  26.     while(i<len)
  27.       {
  28.         if(pass>50)
  29.           pass=0;
  30.         temp=backup[i];
  31.         temp-=pass;
  32.         x=(char)temp;
  33.         ans2[i]=x;
  34.         i++;
  35.         pass++;
  36.       }
  37.     ans2[len]='\0';
  38.     //ans;
  39.     //cout<<backup<<"\n";
  40.     return ans2;
  41.   }
  42. string decryptv2()
  43.   {
  44.     string f;
  45.     cout<<"Enter the name(with extension) of file you want to open.\n\n\t";
  46.     cin>>f;
  47.     ifstream file;
  48.     file.open(f, ios_base::app);
  49.     unbuffer
  50.     getline(file,backup);
  51.     file.close();
  52.     return decrypt(backup);
  53.     //cout<<"yayayayayayayayayayayayayaya";
  54.     //decrypt()
  55.   }
  56. void save(string &backup)
  57.   {
  58.     string f;
  59.     cout<<"Enter the name of the file(with extension)\n\n\t";
  60.     cin>>f;
  61.     ofstream file;
  62.     file.open(f);
  63.     file<<backup;
  64.     cout<<"The text is succesfully saved in a file named \""<<f<<"\""<<char(24)<<"\n\n\t";
  65.     file.close();
  66.     getch();
  67.   }
  68. void save2(string &backup)
  69.   {
  70.     string f;
  71.     cout<<"Enter the name of the file(with extension)\n\n\t";
  72.     cin>>f;
  73.     ofstream file;
  74.     file.open(f);
  75.     file<<backup;
  76.     cout<<"The text is succesfully saved in a file named \""<<f<<"\""<<char(24)<<"\n\n\t";
  77.     file.close();
  78.     getch();
  79.   }
  80. string encrypt(string str)
  81.   {
  82.     lli len,i=0,temp;
  83.     //file.open("decrypted_v.1.00.txt");
  84.     //getline(file,str);
  85.     len=str.length();
  86.     str[len]='\0';
  87.     char enc[len],x;
  88.     while(i<len)
  89.       {
  90.         if(pass>50)
  91.           pass=0;
  92.         temp=str[i];
  93.         temp+=pass;
  94.         x=(char)temp;
  95.         enc[i]=x;//*********************************
  96.         i++;
  97.         pass++;
  98.       }
  99.       enc[len]='\0';
  100.     //cout<<"\n_________________________________________________________________\n\n"<<enc<<"\n\n";
  101.   i=0;
  102.   pass=0;
  103.   backup=enc;
  104.   return enc;
  105.   }
  106. void encryptv2()
  107.   {
  108.     cout<<"Enter the Filename(with extension).\n\n\t";
  109.     string str;
  110.     cin>>str;
  111.     ifstream file;
  112.     file.open(str);
  113.     getline(file,str);
  114.     encrypt(str);
  115.     file.close();
  116.   }
  117. int main()
  118. {
  119.   //ios_base::sync_with_stdio(false);
  120.    //cin.tie(NULL);
  121.    //cout.tie(NULL);
  122.    lli a,i=0,temp,len;
  123.    string str;
  124.    char x;
  125.    while(1)
  126.      {
  127.        i=0,pass=0;
  128.        system("cls");
  129.        //textcolor(11);
  130.        //setcolor(5);
  131.        cout<<setw(80)<<"DATA ENCRYPTION/DECRYPTION SOFTWARE v1.00\n\n\n";
  132.        //gotoxy(24,30);
  133.        option
  134.        cout<<" 1. Encryption of a text \n";
  135.        cout<<" 2. Decryption of a text \n";
  136.        cout<<" 3. Exit \n\n\n\t";
  137.        cin>>a;
  138.        system("cls");
  139.        if(a==1)
  140.         {
  141.           option
  142.           cout<<" 1. Enter a text \n";
  143.           cout<<" 2. Choose a .txt or .dat (binary) file by giving its name and extension \n\n\n \t";
  144.  
  145.           cin>>a;
  146.  
  147. /*---------X------------X-----------X-----------X----------X----------X--------X---------*/
  148.  
  149.           system("cls");
  150.    /*if(a==1)
  151.     {
  152.       cout<<"Enter your text below : \n";
  153.       unbuffer
  154.       getline(cin,str);
  155.       len=str.length();
  156.       str[len]='\0';
  157.       char enc[len],dec[len];
  158.       while(i<len)
  159.         {
  160.           /*switch(str[i])
  161.             {
  162.               case 's':
  163.                 enc[i]='m';
  164.                 break;
  165.               case 'u':
  166.                 enc[i]='n';
  167.                 break;
  168.               case 'r':
  169.                 enc[i]='g';
  170.                 break;
  171.               case 'y':
  172.                 enc[i]='l';
  173.                 break;
  174.               case 'a':
  175.                 enc[i]='p';
  176.                 break;
  177.             }
  178.           if(pass>50)
  179.             pass=0;
  180.           temp=str[i];
  181.           temp+=pass;
  182.           x=(char)temp;
  183.           //cout<<x;
  184.           enc[i]=x; //*********************************
  185.           i++;
  186.           pass++;
  187.         }
  188.         enc[len]='\0';
  189.       //cout<<str<<'\n';
  190.       cout<<"\n_________________________________________________________________\n\n"<<enc<<"\n\n";
  191.     i=0;
  192.     pass=0;
  193.     backup=enc; */
  194.  
  195. /*---------X------------X-----------X-----------X----------X----------X--------X---------*/
  196.  
  197.           if(a==1)
  198.             {
  199.               cout<<"Enter your text below : \n";
  200.               unbuffer
  201.               getline(cin,str);
  202.               ans=encrypt(str);
  203.             }
  204.           else if(a==2)
  205.             {
  206.               string f;
  207.               cout<<"Enter the Filename(with extension).\n\n\t";
  208.               cin>>f;
  209.               ifstream file;
  210.               file.open(f);
  211.               if(file)
  212.                 {
  213.                   getline(file,backup);
  214.                   ans=encrypt(backup);
  215.                 }
  216.               else
  217.                 {
  218.                   cout<<"\n\nEnter a valid Filename...";
  219.                   getch();
  220.                   break;
  221.                 }
  222.             }
  223.           cout<<"\n_________________________________________________________________\n\n"<<ans<<"\n\n";
  224.           cout<<"THIS IS YOUR ENCRYPTED TEXT \n\n Choose an option from below : \n \t1. Manually copy this text. \n \t2. Save this text to a file. \n\n";
  225.           cin>>a;
  226.           if(a==1)
  227.           {
  228.             cout<<"The text is succesfully saved in a variable named \"backup\" which is printed above, you can note it down "<<char(24)<<"\n\n\t";
  229.             getch();
  230.           }
  231.           else if(a==2)
  232.             {
  233.               save(ans);
  234.               getch();
  235.               /*ofstream file;
  236.               file.open("encryption_v1.00.txt");
  237.               file<<backup;
  238.               file.close();*/
  239.             }
  240.     /*while(i<len)
  241.       {
  242.         temp=backup[i];
  243.         temp-=i;
  244.         x=(char)temp;
  245.         backup[i]=x;
  246.         i++;
  247.       }
  248.     backup[len]='\0';
  249.     cout<<backup;*/
  250.  
  251.  
  252.  
  253.     //  decrypt(backup,len); ######################################
  254.         }
  255.  
  256.  /*---------X------------X-----------X-----------X----------X----------X--------X---------*/
  257.  
  258.  
  259.         else if(a==2)
  260.          {
  261.            option
  262.            cout<<"1. Paste the text here and decrypt it.\n2. Decrypt the text saved to a file .\n\n\t";
  263.            cin>>a;
  264.            if(a==1)
  265.            {
  266.              cout<<"\nPaste your text below "<<(char)25<<"\n\n\t";
  267.              unbuffer
  268.              getline(cin,str);
  269.              cout<<"\nDecrypted text is below "<<(char)25<<"\n\n\t"<<decrypt(str);
  270.              cout<<"\n\nTHIS IS YOUR DECRYPTED TEXT \n Choose an option from below : \n \t1. Manually copy this text. \n \t2. Save this text to a file. \n\n";
  271.              cin>>a;
  272.              if(a==1)
  273.                {
  274.                  cout<<"The text is succesfully saved in a variable named \"backup\" which is printed above, you can note it down "<<char(24)<<"\n\n\t";
  275.                  getch();
  276.                }
  277.              else if(a==2)
  278.              {
  279.                save2(str);
  280.                getch();
  281.              }
  282.            }
  283.            else if(a==2)
  284.            {
  285.              ans=decryptv2();
  286.              cout<<"\nDecrypted text is below "<<(char)25<<"\n\n\t"<<ans;
  287.              cout<<"\n\nTHIS IS YOUR DECRYPTED TEXT \n Choose an option from below : \n \t1. Manually copy this text. \n \t2. Save this text to a file. \n\n";
  288.              cin>>a;
  289.              if(a==1)
  290.              {
  291.                backup=ans;
  292.                cout<<ans<<"\nThe text is succesfully saved in a variable named \"backup\" which is printed above, you can note it down "<<char(24)<<"\n\n\t";
  293.                getch();
  294.              }
  295.              else if(a==2)
  296.              {
  297.                save2(ans);
  298.              }
  299.            }
  300.          }
  301.       else if(a==3)
  302.         break;
  303.         //exit(0);
  304.       else
  305.         {
  306.           cout<<"Enter a Valid Option. \n \n";
  307.         getch();
  308.       }
  309.      }
  310. }
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement