[ create a new paste ] login | about

Link: http://codepad.org/WdIYcYVM    [ raw code | output | fork ]

C++, pasted on May 23:
#include <iostream>
#include <iomanip>
#include <algorithm>

using namespace std;

struct Encryptor {
	const int key;
	int count;
	
	Encryptor( int key ) : key(key), count(0) {
	}
	
	char operator() ( char ch ) {
		ch = (ch ^ key) + count;
		++count;
		return ch;
	}	
};

struct Decryptor {
	const int key;
	int count;
	
	Decryptor( int key) : key(key), count(0) {
	}
	
	char operator() ( char ch ) {
		ch = (ch - count) ^ key;
		++count;
		return ch;
	}
};

void hexDump( const string& str ) {
	unsigned track = 0;
	unsigned len = str.length();
	unsigned lineStart = 0;
	
	unsigned nearest16 = ((len + 16) / 16) * 16;
	for ( unsigned i = 0; i < nearest16; ++i ) {
		if ( i < len )
			cout << setw( 2 ) << setfill( '0' ) << hex << uppercase << 
				(unsigned)reinterpret_cast<const unsigned char&>(str[i]) << ' ';
		else
			cout << "   ";
			
		track ++;
		if ( track == 16 ) {
			cout << "  ";
			track = 0;
			for ( unsigned j = 0; j < 16 && lineStart + j < len; ++j ) {
				unsigned char ch = reinterpret_cast<const unsigned char&>( str[lineStart + j] );
				if ( ch < 32 || ch >= 127 )
					ch = '.';
				cout << ch;
			}
			cout << endl;
			lineStart = i+1;
		}
	}
}

int main() {
	int key = 0x01;
	string line = "The quick brown fox jumps over the lazy dog.";
	
	cout << "Input:" << endl;
	hexDump( line );
	cout << endl;
	
	cout << "Encrypted output:" << endl;
	transform( line.begin(), line.end(), line.begin(), Encryptor( key ) );
	hexDump( line );
	cout << endl;
	
	cout << "Decrypted output:" << endl;
	transform( line.begin(), line.end(), line.begin(), Decryptor( key ) );
	hexDump( line );
	cout << endl;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Input:
54 68 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20   The quick brown 
66 6F 78 20 6A 75 6D 70 73 20 6F 76 65 72 20 74   fox jumps over t
68 65 20 6C 61 7A 79 20 64 6F 67 2E               he lazy dog.

Encrypted output:
55 6A 66 24 74 79 6E 69 72 2A 6D 7E 7A 83 7D 30   Ujf$tynir*m~z.}0
77 7F 8B 34 7F 89 82 88 8A 3A 88 92 80 90 3F 94   w..4.....:....?.
89 85 43 90 84 A0 9E 48 8D 97 90 5A               ..C....H...Z

Decrypted output:
54 68 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20   The quick brown 
66 6F 78 20 6A 75 6D 70 73 20 6F 76 65 72 20 74   fox jumps over t
68 65 20 6C 61 7A 79 20 64 6F 67 2E               he lazy dog.



Create a new paste based on this one


Comments: