#include <iostream>
using namespace std;
int main()
{
char key;
char str[9000];
cout << "Enter the message you want to encrypt: "<<endl;
cin >> str;
cout << "Enter your encryption key(any character): ";
cin >> key;
for(int i = 0;str[i]; i++)
{
str[i] = str[i] ^ key;
}
cout << "\nHere is your encrypted message: \n" << str << endl << "\n use key of: " << key << " to decode.\n";
system("pause");
return 0;
}


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void encrypt();
void decrypt();
void start();
int main()
{
start();
system("pause");
return 0;
}
void start(){
char choice;
cout << "Would you like to encrypt, or Decrypt a file?\n" << endl;
cout << "q.) Quit\n";
cout << "e.) Encrypt/Decrypt\n";
cin >> choice;
switch(choice){
case 'q':
break;
case 'e':
system("cls");
decrypt();
system("cls");
start();
break;
default:
system("cls");
start();
break;
}
}
void decrypt(){
ifstream ifile ("encrypt.txt", ios::app);
char key;
string iput;
cout << "Place the text you want to decrypt or encrypt in the file 'encrypt.txt'"<<endl;
if (ifile.is_open()) // I tested this part and nothing executes within this if statement. idk why?
{ // Is it that encrypt.txt isn't open? Why not jst read the file without checking if its open or not?
while (!ifile.eof())
{
getline(ifile, iput);
}
ifile.close();
}
cout << "What key is the input encrypted in?" << endl;
cin >> key;
for(int i = 0;iput[i]; i++)
{
iput[i] = iput[i] ^ key;
}
cout << iput << endl;
//ifile << "\nHere is your encrypted message: \n" << iput << endl << "\n use key of: " << key << " to decode.\n";
//for above to work, class needs to be ofstream, or f stream
system("pause");
}
void CopyToClipboard(char* text)
{
HGLOBAL clipbuffer;
char* temp_ptr ;
clipbuffer = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (strlen(text)+1) * sizeof(char));
temp_ptr = (char*)GlobalLock(clipbuffer);
strcpy(temp_ptr, text);
GlobalUnlock(clipbuffer);
OpenClipboard(0); // 0 means no window
EmptyClipboard();
Se***ipboardData(CF_TEXT, clipbuffer);
CloseClipboard();
}
// Encrypt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
void XOR(BYTE* buffer,long length, char* key)
{
int keyLength = strlen(key);
int keyIndex = 0;
for(int i = 0 ; i< length; i++)
{
buffer[i] ^= (BYTE)key[keyIndex++];
if(keyIndex >= keyLength)
keyIndex = 0;
}
}
void CopyToClipboard(char* text)
{
HGLOBAL clipbuffer;
char* temp_ptr ;
clipbuffer = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, (strlen(text)+1) * sizeof(char));
temp_ptr = (char*)GlobalLock(clipbuffer);
strcpy(temp_ptr, text);
GlobalUnlock(clipbuffer);
OpenClipboard(0); // 0 means no window
EmptyClipboard();
Se***ipboardData(CF_TEXT, clipbuffer);
CloseClipboard();
}
/// Making the main a void since we don't care about what it returns
void main(int argc, char* argv[])
{
int bufferSize = 0;
////define some variables we'll be using
char* filename = NULL;
char* outputFile = NULL;
char* text = NULL;
char* key = NULL;
//Parse arguments - could be done better
for(int i = 0; i < argc; i++)
{
if(_stricmp(argv[i],"/fin")==0 || _stricmp(argv[i],"-fin") == 0)
filename = argv[++i];
else if(_stricmp(argv[i],"/fout")==0 || _stricmp(argv[i],"-fout") == 0)
outputFile = argv[++i];
else if(_stricmp(argv[i],"/t")==0 || _stricmp(argv[i],"-t") == 0)
text= argv[++i];
else if(_stricmp(argv[i],"/k")==0 || _stricmp(argv[i],"-k")==0)
key = argv[++i];
}
//If we're not being called correctly, print out what we expect
if((argc != 5 && argc != 7) || !key || (!filename && !key) || (filename && !outputFile) || (outputFile && !filename))
{
cout << "Encrypt Sample for Why06 by B1ackAnge1" << endl;
cout << "--------------------------------------" << endl;
cout << "Usage:" << endl;
cout << "Encrypt a file using a given key:" << endl;
cout << "Encrypt.exe /fin file.txt /fout file.out /k key" << endl << endl;
cout << "Encrypt a string using a given key and place on clipboard" << endl;
cout << "Encrypt.exe /t sometext /k key" << endl;
cin.get();
return;
}
if(filename) // same as: if(filename != null)
{
bufferSize = strlen(key) * 8192; //Don't ask ;)
BYTE* buffer = new BYTE[bufferSize];
FILE *fp = fopen(filename,"r+b");
FILE *fpout = fopen(outputFile,"w+b");
while(fp && feof(fp) == 0)
{
BYTE* buffer = new BYTE[bufferSize];
int numBytes = fread(buffer,sizeof(byte),bufferSize,fp);
XOR(buffer,numBytes,key);
//long curPos = ftell(fp);
fwrite(buffer,sizeof(byte),numBytes,fpout);
fflush(fpout);
delete[] buffer;
}
if(fp)
fclose(fp);
if(fpout)
fclose(fpout);
}
else if(text)
{
XOR((byte*)text,strlen(text),key);
CopyToClipboard(text);
}
}
