?? tdes.cpp
字號:
// tdes.cc// test DES functions// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include "des.h" // TripleDES_{Encryption,Decryption}#include "modes.h" // CBC{Encryption,Decryption}#include "datablok.h" // DataBlock#include <ctype.h> // isprint#include <string.h> // memcpy#include <stdio.h> // printf#include <iostream.h> // cout// simple utilvoid printBlock(byte const *data, int len, char const *label = NULL){ DataBlock db(data, len); db.print(label);}// test a single encryption and decryptionbool testEncDec(BlockTransformation &enc, BlockTransformation &dec){ byte const *data = (byte const*)"MS Sucks"; // 8 (block size) bytes of data byte output[8]; // output buffer // encrypt enc.ProcessBlock(data /*in*/, output /*out*/); printBlock(output, 8, "ciphertext"); // decrypt dec.ProcessBlock(output /*in, out*/); // compare printBlock(output, 8, "output"); bool ret = 0==memcmp(data, output, 8); if (ret) { printf("(success)\n"); } else { printf("(failure)\n"); } printf("\n"); return ret;}bool test(bool fail){ byte const *key = (byte const*)"abcdefghijklmnopqrstuvwx"; // 24 bytes - 192 raw bits, of which only 168 will be used // (I don't set or check the unused, e.g. as parity) // test TripleDES in ECB mode TripleDES_Encryption enc(key+fail); TripleDES_Decryption dec(key); testEncDec(enc, dec); // test Triple-DES in CBC mode byte const *IV = (byte const*)"ivector!"; // 8 byte initialization vector CBCEncryption enc2(enc, IV); CBCDecryption dec2(dec, IV); bool ok = true; ok &= testEncDec(enc2, dec2); ok &= testEncDec(enc2, dec2); // ciphertext should be different return ok;}bool doit(){ bool ok = true; printf("------ should succeed --------\n"); ok &= test(false /*fail*/); printf("------ should fail ---------\n"); ok &= !test(true /*fail*/); return ok;}int main(){ try { if (doit()) { cout << "all tests succeeded\n"; return 0; } else { cout << "at least one test failed\n"; return 2; } } catch (xBase &x) { cout << "exception caught: " << x << endl; return 4; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -