Beispiel #1
0
void aesDecrypt(std::string filename, std::string key) {
	if (!(key.length() == 16 || key.length() == 24 || key.length() == 32)) {
		std::cout << "Key is not a valid length\n";
		return;
	}

	auto RoundKeys = rijindaelKeySchedule(key);

	std::ifstream input(filename, std::ios::binary);

	if (input.fail()) {
		std::cerr << "Failed to open file\n";
		return;
	}

	std::vector<BYTE> ciphertext((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
	input.close();

	std::ofstream out(filename + "_decrypted.txt", std::ios::binary);

	for (size_t i = 0; i < ciphertext.size(); i += 16) {
		StateBlock state(ciphertext.begin() + i);
		invCipher(state, RoundKeys);
		state.writeToFile(out);
	}

	out.close();
}
void test_invCipher_given_192_bit_chiper_key(void) {

    printf("No2.0 - cipher 192-bit key Size \n");

    uint8_t plainText[4][4] = { {0x00,0x44,0x88,0xcc},

        {0x11,0x55,0x99,0xdd},

        {0x22,0x66,0xaa,0xee},

        {0x33,0x77,0xbb,0xff}
    };

    uint8_t expOut[4][4] = { {0xdd,0x86,0x6e,0xec},

        {0xa9,0x4c,0xaf,0x0d},

        {0x7c,0xdf,0x70,0x71},

        {0xa4,0xe0,0xa0,0x91}
    };

    uint8_t cipcherkey[] = {0x00,0x01,0x02,0x03,

                            0x04,0x05,0x06,0x07,

                            0x08,0x09,0x0a,0x0b,

                            0x0c,0x0d,0x0e,0x0f,

                            0x10,0x11,0x12,0x13,

                            0x14,0x15,0x16,0x17
                           };

    uint32_t word[52];

    uint8_t encrypOut[4][4];

    uint8_t decrypOut[4][4];

    keyExpansion(cipcherkey,word,6,12);

    cipher(plainText,encrypOut,word,12);

    {
        customTestAssertEqualState(expOut,encrypOut,66);
    };

    invCipher(encrypOut,decrypOut,word,12);





    {
        customTestAssertEqualState(plainText,decrypOut,70);
    };

}
void test_invCipher_given_256_bit_chiper_key(void) {
    printf("No3.0 - cipher 256-bit key Size \n");
    uint8_t plainText[4][4] = { {0x00,0x44,0x88,0xcc},\
        {0x11,0x55,0x99,0xdd},\
        {0x22,0x66,0xaa,0xee},\
        {0x33,0x77,0xbb,0xff}
    };
    uint8_t expOut[4][4] = { {0x8e,0x51,0xea,0x4b},\
        {0xa2,0x67,0xfc,0x49},\
        {0xb7,0x45,0x49,0x60},\
        {0xca,0xbf,0x90,0x89}
    };
    uint8_t cipcherkey[] = {0x00,0x01,0x02,0x03,\
                            0x04,0x05,0x06,0x07,\
                            0x08,0x09,0x0a,0x0b,\
                            0x0c,0x0d,0x0e,0x0f,\
                            0x10,0x11,0x12,0x13,\
                            0x14,0x15,0x16,0x17,\
                            0x18,0x19,0x1a,0x1b,\
                            0x1c,0x1d,0x1e,0x1f
                           };
    uint32_t word[60];
    uint8_t encrypOut[4][4];
    uint8_t decrypOut[4][4];
    keyExpansion(cipcherkey,word,8,14);
    cipher(plainText,encrypOut,word,14);
    TEST_ASSERT_EQUAL_STATE(expOut,encrypOut);
    invCipher(encrypOut,decrypOut,word,14);
    // printf("\n");
    // printfState(decrypOut);
    TEST_ASSERT_EQUAL_STATE(plainText,decrypOut);
}
void test_invCipher_given_192_bit_chiper_key(void) {
    printf("No2.0 - cipher 192-bit key Size \n");
    uint8_t plainText[4][4] = { {0x00,0x44,0x88,0xcc},\
        {0x11,0x55,0x99,0xdd},\
        {0x22,0x66,0xaa,0xee},\
        {0x33,0x77,0xbb,0xff}
    };
    uint8_t expOut[4][4] = { {0xdd,0x86,0x6e,0xec},\
        {0xa9,0x4c,0xaf,0x0d},\
        {0x7c,0xdf,0x70,0x71},\
        {0xa4,0xe0,0xa0,0x91}
    };
    uint8_t cipcherkey[] = {0x00,0x01,0x02,0x03,\
                            0x04,0x05,0x06,0x07,\
                            0x08,0x09,0x0a,0x0b,\
                            0x0c,0x0d,0x0e,0x0f,\
                            0x10,0x11,0x12,0x13,\
                            0x14,0x15,0x16,0x17
                           };
    uint32_t word[52];
    uint8_t encrypOut[4][4];
    uint8_t decrypOut[4][4];
    keyExpansion(cipcherkey,word,6,12);
    cipher(plainText,encrypOut,word,12);
    TEST_ASSERT_EQUAL_STATE(expOut,encrypOut);
    invCipher(encrypOut,decrypOut,word,12);
    // printf("\n");
    // printfState(decrypOut);
    TEST_ASSERT_EQUAL_STATE(plainText,decrypOut);
}
void test_invCipher_given_128_bit_chiper_key(void) {
    printf("No1.0 - cipher 128-bit key Size \n");
    uint8_t plainText[4][4] = { {0x00,0x44,0x88,0xcc},\
        {0x11,0x55,0x99,0xdd},\
        {0x22,0x66,0xaa,0xee},\
        {0x33,0x77,0xbb,0xff}
    };
    uint8_t expOut[4][4] =  { {0x69,0x6a,0xd8,0x70},\
        {0xc4,0x7b,0xcd,0xb4},\
        {0xe0,0x04,0xb7,0xc5},\
        {0xd8,0x30,0x80,0x5a}
    };
    uint8_t cipcherkey[] = { 0x00,0x01,0x02,0x03,\
                             0x04,0x05,0x06,0x07,\
                             0x08,0x09,0x0a,0x0b,\
                             0x0c,0x0d,0x0e,0x0f
                           };
    uint32_t word[44];
    uint8_t encrypOut[4][4];
    uint8_t decrypOut[4][4];
    keyExpansion(cipcherkey,word,4,10);
    cipher(plainText,encrypOut,word,10);
    TEST_ASSERT_EQUAL_STATE(expOut,encrypOut);
    invCipher(encrypOut,decrypOut,word,10);
    // printf("\n");
    // printfState(decrypOut);
    TEST_ASSERT_EQUAL_STATE(plainText,decrypOut);
}
void test_invCipher_given_128_bit_chiper_key(void) {

    printf("No1.0 - cipher 128-bit key Size \n");

    uint8_t plainText[4][4] = { {0x00,0x44,0x88,0xcc},

        {0x11,0x55,0x99,0xdd},

        {0x22,0x66,0xaa,0xee},

        {0x33,0x77,0xbb,0xff}
    };

    uint8_t expOut[4][4] = { {0x69,0x6a,0xd8,0x70},

        {0xc4,0x7b,0xcd,0xb4},

        {0xe0,0x04,0xb7,0xc5},

        {0xd8,0x30,0x80,0x5a}
    };

    uint8_t cipcherkey[] = { 0x00,0x01,0x02,0x03,

                             0x04,0x05,0x06,0x07,

                             0x08,0x09,0x0a,0x0b,

                             0x0c,0x0d,0x0e,0x0f
                           };

    uint32_t word[44];

    uint8_t encrypOut[4][4];

    uint8_t decrypOut[4][4];

    keyExpansion(cipcherkey,word,4,10);

    cipher(plainText,encrypOut,word,10);

    {
        customTestAssertEqualState(expOut,encrypOut,38);
    };

    invCipher(encrypOut,decrypOut,word,10);





    {
        customTestAssertEqualState(plainText,decrypOut,42);
    };

}