// EPOS Simmetric Test Program // // Author: fabio // Documentation: $EPOS/doc/crypter Date: 29 Jun 2004 #include "rc5.h" #include #include #include #include __USING_SYS /** ********************************************************************* * prints hexadecimal values from an UCHAR* to stdout ********************************************************************/ void printHex(UCHAR *bytes, int size) { for (int i = 0; i < size; i++) { printf("%02X ", bytes[i]); } printf("\n"); } /** ********************************************************************* * main * Runs test RC5 cases ********************************************************************/ int main() { OStream cout; cout << "Simmetric test\n"; Simmetric crypter; WORD pt1[2], pt2[2], ct[2] = {0,0}; UCHAR key[16]; printf("RC5-32/12/16 tests:\n"); RC5 rc5; rc5.setRounds(12); // teste1 printf("\nTest1: RC5 class test\n"); for (int i = 1; i < 6; i++) { // Initialize pt1 and key pseudorandomly based on previous ct pt1[0] = ct[0]; pt1[1] = ct[1]; for (int j = 0; j < 16; j++) { key[j] = ct[0] % (255 - j); } // Setup, encrypt, and decrypt RC5::Key rckey(key, 16); rc5.setKey(rckey); RC5::Block in_block((UCHAR*)&pt1); RC5::Block out_block = rc5.encrypt(in_block); out_block.extract((UCHAR*)&ct); RC5::Block dec_block = rc5.decrypt(out_block); dec_block.extract((UCHAR*)&pt2); // Print out results, checking for decryption failure printf("\n%d. key = ", i); for (int j = 0; j < 16; j++) { printf("%.2X ", key[j]); } printf("\n plaintext %.8lX %.8lX ---> ciphertext %.8lX %.8lX \n", pt1[0], pt1[1], ct[0], ct[1]); if (pt1[0] != pt2[0] || pt1[1] != pt2[1]) { printf("Decryption Error!"); } } // Teste2 UCHAR in [32] = "1234567890123456"; UCHAR out[32]; UCHAR dec[32]; strcat ((char*)in, "7890123456789012"); crypter.init(); crypter.seedKey(key, 16); printf("\nTest2: Assimetric family tests\n"); printf("plaintext : "); printHex(in, 32); crypter.encrypt(in, 32, out, 32); printf("cyphertext: "); printHex(out, 32); crypter.decrypt(out, 32, dec, 32); printf("plaintext : "); printHex(dec, 32); // Teste3 printf("\nTest3:\n Assimetric family tests - same value as test1 last encrypt"); printf("plaintext : "); printHex((UCHAR*)&pt1, 8); crypter.encrypt((UCHAR*)&pt1, 8, dec, 32); printf("cyphertext: "); printHex(dec, 8); printf("\n\n"); return 0; }