📄 tcrypto.cpp
字号:
// tcrypto.cc// test suite for all crypto++ functions we use in SafeTP// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt#include <stdlib.h> // atoi#include <time.h> // time#include <iostream.h> // cout#include <strstrea.h> // {i,o}strstream//#include "nbtheory.h" // PrimeAndGenerator//#include "randpool.h" // RandomPool#include "integer.h" // Integer#include "rng.h" // LC_RNG#include "elgamal.h" // ElGamal{Private,Public}Key#include "files.h" // File{Source,Sink}#include "cryptran.h" // various wrappers// verify proper relationship between word and dword; if it's not// right, the compiler will complain about creating an array// of size 0static char dummy_array_for_dword[ sizeof(word)*2 == sizeof(dword) ? 1 : 0 /*illegal array size*/];void preliminaries(){ // suppress warning about not using this dummy_array_for_dword[0] = 1; // occasionally interesting cout << "sizeof(word) is " << sizeof(word) << endl; cout << "sizeof(dword) is " << sizeof(dword) << endl; // test endianness bool isLittleEndian = false; { union { long L; char C[sizeof(long)]; } u; u.L = 1; // set LSB to 1 if (u.C[0] == 1) { isLittleEndian = true; } } // confirm SAFETP_LITTLE_ENDIAN is set, or not, properly #ifdef SAFETP_LITTLE_ENDIAN cout << "SAFETP_LITTLE_ENDIAN defined, "; #else cout << "SAFETP_LITTLE_ENDIAN not defined, "; #endif cout << "isLittleEndian is " << isLittleEndian << endl;}void testInteger(){ // test whether Integer works; possible reasons why not: // - SAFETP_LITTLE_ENDIAN set wrong // - word/dword set wrong cout << "should be 123: " << Integer(123) << endl; cout << "should be 20: " << (Integer(4) * Integer(5)) << endl; cout << "should be 123: " << Integer("123") << endl; #if 0 cout << Integer("12345678901234567890") << endl; cout << Integer("1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890" "1234567890123456789012345678901234567890") << endl; #endif // 0/1}void testElGamal(){ // make up a key pair char privateKey[1000], publicKey[1000]; // use a simple random generator LC_RNG rng(0); cout << "creating keys\n"; ElGamalCryptoPrivateKey dummy(rng, 128 /*bits*/); // store private key in memory buffer { cout << "saving private key: "; ostrstream str(privateKey, 1000); FileSink fs(str); dummy.DEREncode(fs); cout << str.tellp() << " bytes\n"; } // store public key in memory buffer { cout << "saving public key: "; ostrstream str(publicKey, 1000); FileSink fs(str); dummy.ElGamalCryptoPublicKey::DEREncode(fs); cout << str.tellp() << " bytes\n"; } // read keys, construct ElGamal objects cout << "loading keys\n"; istrstream str1(privateKey); FileSource privSrc(str1, true /*pumpAndClose ?? */); ElGamalCryptoPrivateKey priv(privSrc); istrstream str2(publicKey); FileSource pubSrc(str2, true /*pumpAndClose*/); ElGamalCryptoPublicKey pub(pubSrc); // wrap 'em in our stuff PKEncryptTransform enc(pub, rng); PKDecryptTransform dec(priv); PKTwoWay trans(enc, dec); // test it cout << "running tests\n"; trans.test(5 /*iters*/); // iters are expensive w/ElGamal!}int doit(){ preliminaries(); testInteger(); testElGamal(); return 0;}int main(){ try { return doit(); } catch (xBase &x) { cout << "exception caught: " << x << endl; return 4; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -