Пример #1
0
void exercise20(){
//	vector.push_back() is busted
//
// ONLY EXERCISE THAT HAS NOT BEEN FIXED
/* Exercise 20 */

	vector<Xstr> strings;
	vector<Xstr> ciphers;

	Xstr random_key = generate_random_AES_key();

	Xstr newstr;
	Xstr next_cipher;

	/* Make 8-byte nonce for CTR, fill with 0's
	 * We use the same nonce for all encryptions, which is where the
	 * weakness is */
	Xstr nonce;
	nonce.resize(AES::CTR_NONCE_SIZE, 0);

	ifstream string_file("test_files/plaintext_ex20.txt");
	string line;

	cout << "HERE" << endl;
	int i = 0;
	// read string into vector from file and encrypt
	// TODO: make function that will encrypt batch
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			newstr = Xstr(line, Xstr::BASE64_ENCODED);
//				cout << "\tHERE0" << endl;
//				strings[i] = ( newstr );

//				cout << "\tHERE1" << endl;
			next_cipher = BlockCipher::encrypt(
					EncryptType::CTR_ENCRYPT, nonce, random_key, nonce);

//				cout << newstr.as_base64() << endl;

//				cout << "\tHERE2" << endl;
//				ciphers.push_back(next_cipher);
//				cout << "\tHERE3" << endl;
			i++;
			cout << ">>>>> " << i << endl;
		}
		cout << "HERE" << endl;
		string_file.close();
	}
	else{
		cout << " Unable to open file" << endl;
	}

	cout << "HERE" << endl;
	vector<Xstr> decoded = break_fixed_nonce_CTR_statistically(ciphers);

//		for (auto i = decoded.begin(); i != decoded.end(); ++i)
//		    std::cout << *i << ' ' << endl;
}
Пример #2
0
void exercise6(){
/* Exercise 6 */
// Break repeating-key XOR

	bool failed = false;

	Xstr decoded_part = "I'm back and I'm ringin' the bell \n"
						"A rockin' on the mike while the fly girls yell \n"
						"In ecstasy in the back of me \n"
						"Well that's my DJ Deshay cuttin' all them Z's \n"
						"Hittin' hard and the girlies goin' crazy \n"
						"Vanilla's on the mike, man I'm not lazy.";

	string line;
	string base64_encoded = string("");
	ifstream string_file("test_files/encoded_ex6.txt");

	Xstr ascii_encoded;
	decoded_message message;
	Xstr decoded;

	// testing hamming distance - should be 37
	int hamming_dist = Xstr("this is a test").hamming_distance("wokka wokka!!!");

	if(hamming_dist != 37){
		failed = true;
		goto eval6;
	}

	// read string into vector from file
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			base64_encoded += line;
		}

		string_file.close();
	}
	else{
		cout << "ERROR: Unable to open file" << endl;
		failed = true;
		goto eval6;
	}

	ascii_encoded = Xstr(base64_encoded, Xstr::BASE64_ENCODED );
	message = solve_repeating_key_xor( ascii_encoded );

	decoded = ascii_encoded.XOR_wraparound(message.key);

	if(decoded.substr(0, decoded_part.size()) != decoded_part)
		failed = true;

	eval6: ;
	crypto_exercise_test(6, !failed);
}
Пример #3
0
void exercise4(){
/* Exercise 4 */
// Detect single-character XOR

	bool failed = false;

	vector<Xstr> possible_strings;
	string line;
	ifstream string_file("test_files/encoded_ex4.txt");
	Xstr output("Now that the party is jumping\n");

	int max_idx = -1;
	bool max_found = false;

	decoded_message max_message;
	max_message.score = 0;
	max_message.decoded = Xstr();

	// read string into vector from file
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			Xstr ascii_string(line, Xstr::HEX_ENCODED );
			possible_strings.push_back( ascii_string );
		}

		string_file.close();
	}
	else{
		cout << "Unable to open file" << endl;
		failed = true;
		goto eval4;
	}

	// iterate through potential strings and find the best solution for each.
	// Regard string with highest score as encoded string
	for(Xstr next_str: possible_strings){
		decoded_message message = solve_single_byte_xor( next_str );

		if(message.score > max_message.score){
			max_found = true;
			max_message.score = message.score;
			max_message.decoded = message.decoded;
		}
	}

	if(max_message.decoded != output)
		failed = true;

	eval4:
		crypto_exercise_test(4, !failed);
}
Пример #4
0
void exercise8(){
/* Exercise 8 */

	bool failed = false;

	Xstr expected_ECB_cipher = Xstr(
			"D880619740A8A19B7840A8A31C810A3D08649AF70DC06F4FD5D2D69C744C"
			"D283E2DD052F6B641DBF9D11B0348542BB5708649AF70DC06F4FD5D2D69C"
			"744CD2839475C9DFDBC1D46597949D9C7E82BF5A08649AF70DC06F4FD5D2"
			"D69C744CD28397A93EAB8D6AECD566489154789A6B0308649AF70DC06F4F"
			"D5D2D69C744CD283D403180C98C8F6DB1F2A3F9C4040DEB0AB51B29933F2"
			"C123C58386B06FBA186A", Xstr::HEX_ENCODED);

	ifstream string_file("test_files/encoded_ex8.txt");

	vector<Xstr> possible_ciphers;
	string line;
	Xstr ECB_found;


	// read string into vector from file
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			Xstr ascii_string = Xstr( line, Xstr::HEX_ENCODED );
			possible_ciphers.push_back( ascii_string );
		}

		string_file.close();
	}
	else{
		failed = true;
		goto eval8;
		cout << "ERROR: Unable to open file" << endl;;
	}

	// iterate through ciphers and see if they are ECB encoded
	for(Xstr cipher: possible_ciphers){
		if( detect_ECB_AES_encryption( cipher ) ){
			ECB_found = cipher;
			break;
		}
	}

	if(expected_ECB_cipher != ECB_found)
		failed = true;

	eval8: ;
	crypto_exercise_test(8, !failed);

}
Пример #5
0
void exercise7(){
/* Exercise 7 */

	bool failed = false;
	string line;
	string base64_encoded = string("");
	Xstr encoded, decoded;

	Xstr decoded_part = "I'm back and I'm ringin' the bell \n"
						"A rockin' on the mike while the fly girls yell \n"
						"In ecstasy in the back of me \n"
						"Well that's my DJ Deshay cuttin' all them Z's \n"
						"Hittin' hard and the girlies goin' crazy \n"
						"Vanilla's on the mike, man I'm not lazy.";

	ifstream string_file("test_files/encoded_ex7.txt");
	string key = string("YELLOW SUBMARINE");


	// read string in from file
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			base64_encoded += line;
		}

		string_file.close();
	}
	else{
		cout << "ERROR: Unable to open file" << endl;
		//TODO: put two next lines in all file open operations
		//TODO: also, make read_file() function
		failed = true;
		goto eval7;
	}

	encoded = Xstr( base64_encoded, Xstr::BASE64_ENCODED );
	decoded = BlockCipher::decrypt(EncryptType::ECB_ENCRYPT, encoded, key);

//		cout << decoded_part.size() << endl;
//		cout << decoded.substr(0, decoded_part.size()).size() << endl;
//		cout << decoded.substr(0, decoded_part.size()) << endl;

	if(decoded.substr(0, decoded_part.size()) != decoded_part){
		failed = true;
	}

	eval7: ;
	crypto_exercise_test(7, !failed);
}
Пример #6
0
void exercise25(){
/* Exercise 25 */
// TODO: Needs fixing, decoded output is returning known_text (all X's)

	bool failed = false;
	string line;
	string base64_encoded = string("");
	Xstr cipher, intermediate, decoded, edited_cipher;
	Xstr known_text;

	ifstream string_file("test_files/encoded_ex7.txt");


	// read string into vector from file
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			base64_encoded += line;
		}

		string_file.close();
	}
	else{
		cout << "ERROR: Unable to open file" << endl;
		failed = true;
		goto eval25;
	}

	cipher = Xstr( base64_encoded, Xstr::BASE64_ENCODED );
	known_text = Xstr(cipher.size(), 'X'); // fill known text with X's


	edited_cipher = server_API_cipher_edit(EncryptType::CTR_ENCRYPT, cipher, 0, known_text);

	intermediate = edited_cipher ^ known_text;

	decoded = intermediate ^ cipher;

//		if(decoded.substr(0, decoded_part.size()) != decoded_part){
//			failed = true;
//		}

	eval25: ;
	crypto_exercise_test(25, !failed);
}
Пример #7
0
file_types_default_t::file_types_default_t(string_t const &, config_t const &config) :
	file_types_t() {

	if(config::check)
		return;

	string_t string = string_file(config.mime_types_filename.str());
	in_t::ptr_t p = string;
	parse_item(string_t::empty, p, 0);

	if(!size)
		return;

	for(
		config::list_t<string_t>::ptr_t ptr = config.allow_gzip_list;
		ptr; ++ptr
	) {
		item_t *item = lookup_item(ptr.val());

		if(item) item->allow_gzip = true;
	}
}
Пример #8
0
void exercise10(){
/* Exercise 10 */

	static string output =
		"I'm back and I'm ringin' the bell \nA rockin' on the mike while the fly girls yell \nIn ecstasy in the back of me \nWell that's my DJ Deshay cuttin' all them Z's \nHittin' hard and the girlies goin' crazy \nVanilla's on the mike, man I'm not lazy. \n\nI'm lettin' my drug kick in \nIt controls my mouth and I begin \nTo just let it flow, let my concepts go \nMy posse's to the side yellin', Go Vanilla Go! \n\nSmooth 'cause that's the way I will be \nAnd if you don't give a damn, then \nWhy you starin' at me \nSo get off 'cause I control the stage \nThere's no dissin' allowed \nI'm in my own phase \nThe girlies sa y they love me and that is ok \n"
		"And I can dance better than any kid n' play \n\nStage 2 -- Yea the one ya' wanna listen to \nIt's off my head so let the beat play through \nSo I can funk it up and make it sound good \n1-2-3 Yo -- Knock on some wood \nFor good luck, I like my rhymes atrocious \nSupercalafragilisticexpialidocious \nI'm an effect and that you can bet \nI can take a fly girl and make her wet. \n\nI'm like Samson -- Samson to Delilah \nThere's no denyin', You can try to hang \nBut you'll keep tryin' to get my style \nOver and over, practice makes perfect \nBut not if you're a loafer. \n\nYou'll get nowhere, no place, no time, no girls \nSoon -- Oh my God, homebody, you probably eat \n"
		"Spaghetti with a spoon! Come on and say it! \n\nVIP. Vanilla Ice yep, yep, I'm comin' hard like a rhino \nIntoxicating so you stagger like a wino \nSo punks stop trying and girl stop cryin' \nVanilla Ice is sellin' and you people are buyin' \n'Cause why the freaks are jockin' like Crazy Glue \nMovin' and groovin' trying to sing along \nAll through the ghetto groovin' this here song \nNow you're amazed by the VIP posse. \n\nSteppin' so hard like a German Nazi \nStartled by the bases hittin' ground \nThere's no trippin' on mine, I'm just gettin' down \nSparkamatic, I'm hangin' tight like a fanatic \nYou trapped me once and I thought that \nYou might have it \nSo step down and lend me your ear \n"
		"'89 in my time! You, '90 is my year. \n\nYou're weakenin' fast, YO! and I can tell it \nYour body's gettin' hot, so, so I can smell it \nSo don't be mad and don't be sad \n'Cause the lyrics belong to ICE, You can call me Dad \nYou're pitchin' a fit, so step back and endure \nLet the witch doctor, Ice, do the dance to cure \nSo come up close and don't be square \nYou wanna battle me -- Anytime, anywhere \n\nYou thought that I was weak, Boy, you're dead wrong \nSo come on, everybody and sing this song \n\nSay -- Play that funky music Say, go white boy, go white boy go \nplay that funky music Go white boy, go white boy, go \nLay down and boogie and play that funky music till you die. \n\n"
		"Play that funky music Come on, Come on, let me hear \nPlay that funky music white boy you say it, say it \nPlay that funky music A little louder now \nPlay that funky music, white boy Come on, Come on, Come on \nPlay that funky music \n";

	string line;
	string base64_encoded = string("");
	string key = string("YELLOW SUBMARINE");

	string IV = string();
	IV.resize(AES::BLOCKSIZE, 0); // size 16 - IV is all zeroes!

	ifstream string_file("test_files/encoded_ex10.txt");

	// read string in from file
	if (string_file.is_open()){
		while( getline(string_file, line) ){
			base64_encoded += line;
		}
		string_file.close();
	}
	else{
		cout << "Exercise 10: Unable to open file" << endl;
	}

	Xstr ascii_encoded = Xstr( base64_encoded, Xstr::BASE64_ENCODED );
	Xstr message = BlockCipher::decrypt(EncryptType::CBC_ENCRYPT, ascii_encoded, key, IV);

	crypto_exercise_test(10,
				output == message.remove_padding(Xstr::UNKNOWN_PADDING)
			);
}