int main() { UINT64 plain, key, o; char *passphrase = "SECURITY"; char *plaintext = "NETWORK INFORMATION SECURITY"; char output[100]; char output2[100]; key = *(UINT64*)passphrase; int len = strlen(plaintext)+1; int outputlen = (len+7)&0xfffffff8; for (int i=0; i<len; i+=8) { *(UINT64*)(output+i) = DES_encrypt(*(UINT64*)(plaintext+i), key); } puts("\nDES (CBC mode) final result:"); for (int i=0; i<outputlen; i++) { printf("%02hhx ", output[i]); } puts("\n"); // decrypt to verify puts("decrypt the text to verify:"); for (int i=0; i<outputlen; i+=8) { *(UINT64*)(output2+i) = DES_decrypt(*(UINT64*)(output+i), key); } puts(output2); return 0; }
int main() { freopen("./tests.txt", "r", stdin); int tests_count = 0; scanf("%d\n", &tests_count); for (int test = 0; test < tests_count; ++test) { uint8_t key[8] = {0}; read_block(key); uint8_t data[block_length] = {0}; uint8_t encrypted[block_length] = {0}; read_block(data); uint8_t expected_result[block_length] = {0}; read_block(expected_result); DES_encrypt(data, encrypted, key, block_length); for (size_t i = 0; i < block_length; ++i) { assert(encrypted[i] == expected_result[i]); } uint8_t decrypted[block_length] = {0}; DES_decrypt(encrypted, decrypted, key, block_length); for (size_t i = 0; i < block_length; ++i) { assert(decrypted[i] == data[i]); } } printf("All tests passed\n"); return 0; }