Advertisement
Guest User

Untitled

a guest
Jun 30th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.65 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. void mapitE(string,char[]);
  10. void mapitD(string,char[]);
  11. void encryptdecrypt(const string,const char[],int,string&);
  12.  
  13. int main()
  14. {
  15.     string encrypt_decrypt[] = {"encrypt","decrypt"}, buffer, key, newbuffer = "";
  16.     char filename[80];
  17.     char encryptmap[128];
  18.     char decryptmap[1280];
  19.     char repeat;
  20.     int i;
  21.     int maplength;
  22.     int choice;
  23.     ofstream out;
  24.     ifstream in;
  25.    
  26.  
  27.     do
  28.     {
  29.     cout << "Enter 1 to Encrypt the file or 2 to Decrypt the file: ";
  30.     cin >> choice;
  31.    
  32.     while (choice<1||choice>2)
  33.     {
  34.         cout << "Invalid choice!!!\n";
  35.         cout << "Enter 1 to Encrypt or 2 to Decrypt: ";
  36.         cin >> choice;
  37.     }
  38.  
  39.     cout << "Enter the name of your file to " << encrypt_decrypt[choice-1] << ": ";
  40.     cin >> filename;
  41.     in.open(filename);
  42.  
  43.     if (in.fail())          
  44.     {
  45.         cout << "The Input file could not open!!!\nCheck that the Input file is valid!!!\n\n";
  46.         cout << "Enter the name of your file to " << encrypt_decrypt[choice-1] << ": ";
  47.         cin >> filename;
  48.         in.open(filename);
  49.     }
  50.  
  51.     cout << "Enter the name that you want your Output file to be: ";
  52.     cin >> filename;
  53.     out.open(filename);
  54.     cout << "Enter your Encryption Key with a max of 128 characters, all LOWER case: ";
  55.     cin >> key;
  56.  
  57.     if (key.length()>128)
  58.     {
  59.         cout << "Your Encryption Key is too long!!!\n";
  60.         cout << "Enter your Encryption Key with a max of 128 lower case characters: ";
  61.         cin >> key;
  62.     }
  63.  
  64.     maplength=key.length();
  65.  
  66.     if(choice==1)
  67.     {
  68.         mapitE(key,encryptmap);
  69.     }
  70.     else
  71.     {
  72.         mapitD(key,decryptmap);
  73.     }      
  74.     getline(in,buffer);
  75.            
  76.     while(in)
  77.     {
  78.     if(choice==1)
  79.     {
  80.         encryptdecrypt(buffer,encryptmap,maplength,newbuffer);
  81.     }
  82.     else
  83.     {
  84.         encryptdecrypt(buffer,decryptmap,maplength,newbuffer);
  85.     }
  86.     out << newbuffer;
  87.     newbuffer.erase(0);
  88.     getline(in,buffer);
  89.     }
  90.  
  91.     out.close();
  92.     in.close();
  93.     in.clear();
  94.     out.clear();
  95.     newbuffer.erase(0);
  96.  
  97.     cout << "Would you like to run the program again (y/n)?";
  98.     cin >> repeat;
  99.     }while (repeat == 'Y' || repeat == 'y');
  100.  
  101.     return 0;
  102. }
  103.  
  104. void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)
  105. {
  106.     int i=0;
  107.     char t;
  108.     char code;
  109.     for (i=0;i<buffer.length();i++)
  110.     {
  111.         t=buffer[i];
  112.         (t += 251 - ((i * 14) & 255));
  113.         cout << "Buffer length: " << buffer.length() << endl;
  114.         cout << "newbuffer length: " << newbuffer.length() << endl;
  115.         newbuffer.push_back(t);
  116.     }
  117.     newbuffer.push_back('\n');
  118. }
  119.  
  120. void mapitE(string key,char map[])
  121. {
  122.     int i;
  123.  
  124.     for (i=0;i<key.length();i++)
  125.     map[i]=key[i]-'a';
  126. }
  127.  
  128. void mapitD(string key,char map[])
  129. {
  130.     int i;
  131.  
  132.     for (i=0;i<key.length();i++)
  133.         cout << endl;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement