Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,629 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,049 people online right now. Registration is fast and FREE... Join Now!





Simplest Encryption

Shows Very Very Simple Encryption.

Submitted By: born2c0de
Actions:
Rating:
Views: 13,283

Language: C++

Last Modified: March 2, 2005

Snippet


  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. #include <fstream.h>
  4. #include <stdio.h>
  5.  
  6.  
  7. bool password();
  8.  
  9. void doCrypt(char *cool)
  10. {
  11.      int cryp;//the main int this function uses
  12.     char x;//to get the chars from the file
  13.     ifstream in;//the input stream
  14.     ofstream out;//the output stream
  15.     in.open(cool);//opening the input file
  16.     if(in.fail()){//check if it's not there
  17.          cout<<"Couldn't open the IN file\n";//if it's not, print
  18.         return;
  19.     }
  20.     cout<<"enter the OUT file name: ";//enter the output filename
  21.     cin>>cool;//get it
  22.     out.open(cool);//open the output file
  23.     in.get(x);//get the first char of input
  24.     if(in.eof())//if read beyong end of file
  25.          return;//quit
  26.     while(x != EOF){//while x is not beyong End Of File
  27.          cryp=x-0xFACA;//the crypting
  28.         out<<(char)cryp;//print the crypted char
  29.         in.get(x);//get another char
  30.         if(in.eof())//if read beyong end of file
  31.              break;
  32.     }
  33.     in.close();
  34.     out.close();
  35.     return;
  36. }
  37. void deCrypt(char *daf)
  38. {
  39.      int decr;//the main int
  40.     char x;//the input char
  41.     ifstream in;//input stream
  42.     ofstream out;//output stream
  43.     in.open(daf);//open the inout stream
  44.     if(in.fail()){//see if it's not there
  45.          cout<<"Error couldn't open the IN file\n";//print
  46.         return;
  47.     }
  48.     cout<<"Enter the OUT file name: ";//ask for the out filename
  49.     cin>>daf;
  50.     out.open(daf);//open the outfile
  51.     in.get(x);//get the input char
  52.     if(in.eof())//if read beyong end of file
  53.          return;
  54.     while(x!=EOF){//while x is not beyong end of file
  55.          decr=x+0xFACA;//the dectypring
  56.         out<<(char)decr;
  57.         in.get(x);//get the next char
  58.         if(in.eof())//if read beyong the eon of file
  59.              break;
  60.     }
  61.     return;
  62. }
  63. int main(void)
  64. {
  65.     if (password()) cout << "Access Granted: \n";
  66.     else
  67.     {
  68.     cout << "You are not Authorized to use this program!\n";
  69.     system("pause");
  70.     return 0;
  71.     }
  72.     char *cool=new char[20];//the name pointer
  73.     char nav;              //"navigation char"
  74.     cout << "*---------------------------------------------*\n";
  75.     cout << "| This is an encryption program.  I'm not     |\n";
  76.     cout << "| taking credit for this.  This was a         |\n";
  77.     cout << "| recent submission on planet-source-code     |\n";
  78.     cout << "| I just added password protection to it.     |\n";
  79.     cout << "*---------------------------------------------*\n\n";
  80.     system("pause");
  81.     cout << "\n\n";
  82.     cout << "Use \"m\" for the Menu\n";//print instructions
  83.      while(true){
  84.          cin >> nav;
  85.          switch(nav){
  86.              case 'm':
  87.                  cout << "COMMAND\tFUNCTION\n";
  88.                 cout << "e      \tencrypt\n"
  89.                           "d      \tdecrypt\n"
  90.                         "m      \t menu \n"
  91.                         "q      \t quit \n";
  92.                 break;
  93.              case 'e':
  94.                  cout << "Enter the file name: \nExample: \"file\".txt\n";
  95.                 cin >> cool;
  96.                 doCrypt(cool);
  97.                 break;
  98.             case 'd':
  99.                  cout << "Enter the file name: ";
  100.                 cin >> cool;
  101.                 deCrypt(cool);
  102.                 break;
  103.             case 'q':
  104.                  exit(0);
  105.                  break;
  106.             default:
  107.                  cout << "I do not understand you!\n";
  108.                 break;
  109.         }
  110.     }
  111. }
  112.  
  113. bool password()
  114. {
  115.      char string1[50];
  116.      cout << "Enter in the encryption password: \n";
  117.      gets(string1);
  118.  
  119.      if (strcmp(string1, "outlaw")) return 0;
  120.      return 1;
  121. }
  122.  

Copy & Paste


Comments


rissvann 2008-12-23 15:03:42

its good example & very useful for beginners thanks


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month