Hope you realize that when you XOR a readable ASCII character you will very likely end up with a non-printable character so when you paste it in notepad or any other window it'll not show or look like a weird character. Also using ios:app is for appending which doesn't seem like what you'd want since it seeks to the end of the stream.
Anyway, if you want to put text in the clipboard include windows.h and do something like this:
Code:
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();
}
Edit: Not sure why it put asterisks in there. it's S e t C l ipboard
Also right now you're X-Oring with a 1-Byte Key.
I'll write up something that'll take a whole string
Edit: Here's a rough draft that'll take an input& output file with a key
or a string with a key (and copies it to the clipboard)
Code:
// 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);
}
}
No guarantees whether or not the above works 100% in all scenarios.. only rudimentary error checking implemented. If you want to make it check if the filekey is correct you could save out a header that needs to always be 'x' after decrypting and give it a custom file extension etc etc