Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void mapitE(string,char[]);
- void mapitD(string,char[]);
- void encryptdecrypt(const string,const char[],int,string&);
- int main()
- {
- string encrypt_decrypt[] = {"encrypt","decrypt"}, buffer, key, newbuffer = "";
- char filename[80];
- char encryptmap[128];
- char decryptmap[1280];
- char repeat;
- int i;
- int maplength;
- int choice;
- ofstream out;
- ifstream in;
- do
- {
- cout << "Enter 1 to Encrypt the file or 2 to Decrypt the file: ";
- cin >> choice;
- while (choice<1||choice>2)
- {
- cout << "Invalid choice!!!\n";
- cout << "Enter 1 to Encrypt or 2 to Decrypt: ";
- cin >> choice;
- }
- cout << "Enter the name of your file to " << encrypt_decrypt[choice-1] << ": ";
- cin >> filename;
- in.open(filename);
- if (in.fail())
- {
- cout << "The Input file could not open!!!\nCheck that the Input file is valid!!!\n\n";
- cout << "Enter the name of your file to " << encrypt_decrypt[choice-1] << ": ";
- cin >> filename;
- in.open(filename);
- }
- cout << "Enter the name that you want your Output file to be: ";
- cin >> filename;
- out.open(filename);
- cout << "Enter your Encryption Key with a max of 128 characters, all LOWER case: ";
- cin >> key;
- if (key.length()>128)
- {
- cout << "Your Encryption Key is too long!!!\n";
- cout << "Enter your Encryption Key with a max of 128 lower case characters: ";
- cin >> key;
- }
- maplength=key.length();
- if(choice==1)
- {
- mapitE(key,encryptmap);
- }
- else
- {
- mapitD(key,decryptmap);
- }
- getline(in,buffer);
- while(in)
- {
- if(choice==1)
- {
- encryptdecrypt(buffer,encryptmap,maplength,newbuffer);
- }
- else
- {
- encryptdecrypt(buffer,decryptmap,maplength,newbuffer);
- }
- out << newbuffer;
- newbuffer.erase(0);
- getline(in,buffer);
- }
- out.close();
- in.close();
- in.clear();
- out.clear();
- newbuffer.erase(0);
- cout << "Would you like to run the program again (y/n)?";
- cin >> repeat;
- }while (repeat == 'Y' || repeat == 'y');
- return 0;
- }
- void encryptdecrypt(const string buffer,const char map[],int len,string& newbuffer)
- {
- int i=0;
- char t;
- char code;
- for (i=0;i<buffer.length();i++)
- {
- t=buffer[i];
- (t += 251 - ((i * 14) & 255));
- cout << "Buffer length: " << buffer.length() << endl;
- cout << "newbuffer length: " << newbuffer.length() << endl;
- newbuffer.push_back(t);
- }
- newbuffer.push_back('\n');
- }
- void mapitE(string key,char map[])
- {
- int i;
- for (i=0;i<key.length();i++)
- map[i]=key[i]-'a';
- }
- void mapitD(string key,char map[])
- {
- int i;
- for (i=0;i<key.length();i++)
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement