Advertisement
Mizuhara_Chizuru

Untitled

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