⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encdec.cpp

📁 VC++编程
💻 CPP
字号:
/*  EncDec.cpp
 By:  Nick Amodio
 This program encrypts and decrypts text files using strings.
 Upper case letters, lower case letters, and numbers are encrypted/decrypted.
*/

#include <fstream.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <dos.h>

char EncUpper[] = "GKDLVZJNSQBPAWFTXCEOIYMHUR";
              //   ABCDEFGHIJKLMNOPQRSTUVWXYZ

char EncLower[] = "udzrlnxcyfwphkvagjmbtiesoq";
              //   abcdefghijklmnopqrstuvwxyz

char EncNum[] =   "7419820536";
              //   0123456789

char DecUpper[] = "MKRCSOAXUGBDWHTLJZIPYENQVF";
              //   ABCDEFGHIJKLMNOPQRSTUVWXYZ

char DecLower[] = "pthbwjqmvrnesfylzdxuaokgic";
              //   abcdefghijklmnopqrstuvwxyz

char DecNum[] =   "6258179043";
              //   0123456789

void EncTitle();
void DecTitle();
void Encrypt(char &x);
void Decrypt(char &x);

main()
{
   bool again=true;
   int choice;
   char c;
   char targetfile[30];
   char newfile[30];

   while(again)
   {
      again=false;
      cout << "What do you want to do?";
      cout << "\n\n1.  Encrypt";
      cout << "\n2.  Decrypt";
      cout << "\n3.  Quit\n";
      cout << "\nchoice: ";
      cin >> choice;
      clrscr();
      switch(choice)
      {
         case 1:
         {
            EncTitle();
            cout << " What file do you want to encrypt?\t";
            cin >> targetfile;
            cout << '\n' << " What do you want the encrypted file to be saved as?\t";
            cin >> newfile;
            ifstream in(targetfile);
            if(!in)
            {
               clrscr();
               cout << "Error finding/opening file!";
               getch();
               exit(1);
            }
            in.unsetf(ios::skipws);
            ofstream out(newfile);
            while(in >> c)
            {
               Encrypt(c);
               out << c;
            }
            cout << " Encrypted file successfully created!\n";
            break;
         }
         case 2:
         {
            DecTitle();
            cout << " What file do you want to decrypt?\t";
            cin >> targetfile;
            cout << '\n' << " What do you want the decrypted file to be saved as?\t";
            cin >> newfile;
            ifstream in(targetfile);
            if(!in)
            {
               clrscr();
               cout << "Error finding/opening file!";
               getch();
               exit(1);
            }
            in.unsetf(ios::skipws);
            ofstream out(newfile);
            while(in >> c)
            {
               Decrypt(c);
               out << c;
            }
            cout << " Decrypted file successfully created!\n";
            break;
         }
         case 3:
         {
            clrscr();
            cout << "\n Thank You For Using This Program.";
            sleep(2);
            exit(1);
         }
         default:
            again=true;
            break;
      }  // end switch
      if(again) //if the choice is not valid.
      {
         cout << "\n\t\t\t   *** INVALID PARAMETERS ***";
         cout << "\n\n\n\nEnter only a 1, 2, or 3.  Press any key to continue";
         getch();
         clrscr();
      }
      else
      {
         cout << "\n\nDo you still want to do more?(1 for yes, 0 for no)";
         cin >> again;
      }
   } // end while loop
   clrscr();
   cout << "\n Thank You For Using This Program.";
   sleep(2);
   exit(1);
}
void EncTitle()
{
   cout << "\n\t\t\t    THE ENCRYPTOR(beta 1.0)";
   cout << "\n\n----------------------------------------------------------------";
   cout << "----------------";
   cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+";
   cout << "-+-+-+-+-+-+-\n\n\n";
}

void DecTitle()
{
   cout << "\n\t\t\t    THE DECRYPTOR(beta 1.0)";
   cout << "\n\n----------------------------------------------------------------";
   cout << "----------------";
   cout << "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+";
   cout << "-+-+-+-+-+-+-\n\n\n";
}

void Encrypt(char &x)
{
   int pos;
   if(x >= '0' && x <= '9')
   {
      pos = x - 48;
      x = EncNum[pos];
   }
   if(!isalpha(x))
      return;
   if(x >= 'A' && x<=  'Z')
   {
      pos = x - 65;
      x = EncUpper[pos];
   }
   else if(x >= 'a' && x <= 'z')
   {
      pos = x - 97;
      x = EncLower[pos];
   }
}

void Decrypt(char &x)
{
   int pos;

   if(x >= '0' && x <= '9')
   {
      pos = x - 48;
      x = DecNum[pos];
   }
   if(!isalpha(x))
      return;

   if(x >= 'A' && x<=  'Z')
   {
      pos = x - 65;
      x = DecUpper[pos];
   }
   else if(x >= 'a' && x <= 'z')
   {
      pos = x - 97;
      x = DecLower[pos];
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -