コード例 #1
0
//
// This function makes a polymorphic call to both the encryption
// and decryption functions, displaying the phrase both before
// and after each.
//
void  encryptAndDecrypt(Crypto  &msg)
{
	cout << "Original - " << msg.phrase() << endl;
	cout << "           Level = " << msg.encryptionLevel() << endl;
	cout << "   # Conversions = " << msg.numConversions() << endl;

	//
	// Encrypt
	//
	msg.encrypt();
	cout << " Encrypt - " << msg.phrase() << endl;
	cout << "           Level = " << msg.encryptionLevel() << endl;
	cout << "   # Conversions = " << msg.numConversions() << endl;

	//
	// Decyrpt
	//
	msg.decrypt();
	cout << " Decrypt - " << msg.phrase() << endl;
	cout << "           Level = " << msg.encryptionLevel() << endl;
	cout << "   # Conversions = " << msg.numConversions() << endl;
}
コード例 #2
0
ファイル: otpnitro.cpp プロジェクト: capitanx/otpnitro
int main(int argc, char **argv)
{
	// Header
	cout << "OTP Nitro " << VERSION << endl << "---------------" << endl;
	if (argc == 1)
	cout << "ERROR: You must enter a valid argument, see -h" << endl << endl;

	// Arguments
	signed char c;
	bool gen = false, enc = false, dec = false, brn = false, lst = false;
	string send, id, msg, file;
	int pnum = 0;

	while ( (c = getopt(argc, argv, "h?gledbs:r:m:p:f:")) != -1) {
	    switch (c) {
		case 'g':
			// Generate OTP
			gen = true;
		break;
		case 'e':
			// Encrypt
			enc = true;
		break;
		case 'd':
			// Decrypt
			dec = true;
		break;
		case 'b':
			// Burn page
			brn = true;
		break;
		case 'l':
			// List available pages
			lst = true;
		break;
		case 's':
			// sender
			send = optarg;
		break;
		case 'r':
			// book - recv id
			id   = optarg;
		break;
		case 'm':
			// Message
			msg  = optarg;
		break;
		case 'p':
			// Page num
			if(optarg)
				pnum = atoi(optarg);
		break;
		case 'f':
			// File to parse
			file = optarg;
		break;
		case 'h':
		case '?':
			printf("\n"
				"Modes:\n"			\
				"\t-l	List Books	\n"	\
				"\t-g	Gen. Book	[-r]\n"	\
				"\t-b	Burn page	[-r -p]\n"	\
				"\t-e	Encrypt		[-s -r -m]\n"		\
				"\t-d	Decrypt		[-s -r -m -p] [-f]\n"	\
				"\n"				\
				"Opts:\n"			\
				"\t-s	<sender>\n"		\
				"\t-r	<code book>\n"		\
				"\t-p	<page num>\n"		\
				"\t-m	<\"message text\">\n"	\
				"\t-f	<\"crypted format\">\n\n");
		exit(1);
		break;
	    }
	}

	if (gen) {
		Page   * page   = new Page;

		// Generate OTP for ID
		cout << "[I] Generating OTP: " << id;
		page->generate(id);
		cout << ". OK" << endl << endl;

		delete page;
		exit(0);
	}

	if (enc) {
		cout << "[I] Encrypted msg:" << endl;

		Page   * page   = new Page;
		Crypto * crypto = new Crypto;

		// Get a usable page
		pnum = page->next(id);
		if (pnum == -1) {
			cout << "[E] Not found pages in book: " << id << endl;
			cout << "[I] You can generate them with: otpnitro -g -r " << id << endl << endl;
			exit(1);
		}

		// Read page X from Book (RECV ID)
		string out = page->read(pnum,id);

		if (msg.size() > out.size())
		{
			cout << "You need " << msg.size() - out.size() << " more bytes in the selected book page";
		        delete page;
		        delete crypto;
		        exit(1);
		}

		// Crypto
		string encrypted = crypto->encrypt(msg,out);

		// Print page
		Text * txt = new Text;
		txt->create(pnum,id,send,encrypted);
		cout << txt->print(1);

		delete txt;
		delete page;
		delete crypto;
		exit(0);
	}

	if (dec) {
		cout << "[I] Decrypted msg:" << endl;

		Page   * page   = new Page;
		Crypto * crypto = new Crypto;
		Text   * txt    = new Text;

		if (file.length() > 0)
			txt->parse(file);
		else
			txt->create(pnum,id,send,msg);

		// Read page X from Book (RECV ID)
		string out = page->read(txt->page,txt->book);

		if (out.length() == 0) {
			cout << "[E] The page " << pnum << " in the book " << id << " dont exist." << endl;
			cout << "[I] You can check if you recieved the " << id << " book, or if it was burned." << endl;
			cout << "[I] Check: otpnitro -l" << endl << endl;
			exit(1);
		}

		// Crypto
		txt->replaceAll(txt->msg," ","");
		txt->msg = crypto->decrypt(txt->msg,out);

		// Print MSG
		cout << txt->print(0);

		delete txt;
		delete page;
		delete crypto;
	   	exit(0);
	}

	if (brn) {
		cout << "[I] Burn page " << pnum;

		Page   * page   = new Page;
		if (page->burn(pnum,id))
		cout << ". OK"   << endl << endl;
		else
		cout << ". FAIL" << endl << endl;
		delete page;
		exit(0);
	}

	if (lst) {
		Page   * page   = new Page;

		cout << "[I] Available books:" << endl;
		cout << page->list();
		cout << endl;

		delete page;
		exit(0);
	}

	return(0);
}
コード例 #3
0
ファイル: test.cpp プロジェクト: klevstul/CSE-4001-OO
int main()
{
    
    Crypto* trans;
    Crypto* trans2;
    Crypto* trans3;
    Crypto* trans4;

    string org_msg;
    string enc_msg;
    string dec_msg;

    // - - - - - -
    // Ceasar
    // - - - - - -

    try
    {
        trans = Crypto::getCrypto("caesar", "5");
    }
    catch(InvalidKeyException e)
    {
        exit(-1);
    }

    org_msg = "Pull the brown book on the top shelf to activate";
    enc_msg = trans->encrypt(org_msg);
    dec_msg = trans->decrypt(enc_msg);

    cout << "Caesar: original message: \"" << org_msg << "\"" << endl;
    cout << "Caesar: encoded message: \"" << enc_msg << "\"" << endl;
    cout << "Caesar: decoded message: \"" << dec_msg << "\"" << endl << endl;

    delete trans;
    trans = 0;          // sets pointer to zero to avoid potential problems with dangling pointers

    // - - - - - -
    // Monoalpha
    // - - - - - -

    try
    {
        trans2 = Crypto::getCrypto("monoalpha", "QA Zwsxedcrfvtgbyhnujmikolp");
    }
    catch(InvalidKeyException e)
    {
        exit(-1);
    }

    org_msg = "Pull the brown book on the top shelf to activate";
    enc_msg = trans2->encrypt(org_msg);
    dec_msg = trans2->decrypt(enc_msg);

    cout << "Monoalpha: original message: \"" << org_msg << "\"" << endl;
    cout << "Monoalpha: encoded message: \"" << enc_msg << "\"" << endl;
    cout << "Monoalpha: decoded message: \"" << dec_msg << "\"" << endl << endl;

    delete trans2;
    trans2 = 0;

    // - - - - - -
    // Transposition
    // - - - - - -

    try
    {
        trans3 = Crypto::getCrypto("transposition", "240153");
    }
    catch(InvalidKeyException e)
    {
        exit(-1);
    }


    org_msg = "The password for today is deceptive";
    enc_msg = trans3->encrypt(org_msg);
    dec_msg = trans3->decrypt(enc_msg);

    cout << "Transposition: original message: \"" << org_msg << "\"" << endl;
    cout << "Transposition: encoded message: \"" << enc_msg << "\"" << endl;
    cout << "Transposition: decoded message: \"" << dec_msg << "\"" << endl << endl;

    string s1 = "test transposition with same object";
    string s2 = trans3->encrypt(s1);
    string s3 = trans3->decrypt(s2);

    cout << "Transposition: original message: \"" << s1 << "\"" << endl;
    cout << "Transposition: encoded message: \"" << s2 << "\"" << endl;
    cout << "Transposition: decoded message: \"" << s3 << "\"" << endl << endl;


    delete trans3;
    trans3 = 0;

    // - - - - - - - - 
    // Cencryption 
    // - - - - - - - -
    try
    {
        trans4 = Crypto::getCrypto("cencryption", "caesar,16,50;transposition,30142,50;caesar,21,30;");
    }
    catch(InvalidKeyException e)
    {
        exit(-1);
    }

    org_msg = "In this assignment you will develop a simple cryptographic library that combines three different encryption algorithms  The encryption algorithms are very old an well known and are not the strongest encryption schemes available today but are much more interesting to program  The algorithms can be used together to encrypt different parts of a single text message";
    enc_msg = trans4->encrypt(org_msg);
    dec_msg = trans4->decrypt(enc_msg);

    cout << "Cencryption: original message: \"" << org_msg << "\"" << endl;
    cout << "Cencryption: encoded message: \"" << enc_msg << "\"" << endl;
    cout << "Cencryption: decoded message: \"" << dec_msg << "\"" << endl << endl;

    delete trans4;
    trans4 = 0;


    return 0;
}