📄 test.cpp
字号:
// test.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "md5.h"
#include "sha.h"
#include "ripemd.h"
#include "files.h"
#include "rng.h"
#include "hex.h"
#include "gzip.h"
#include "default.h"
#include "rsa.h"
#include "randpool.h"
#include "ida.h"
#include "socketft.h"
#include "validate.h"
#include "bench.h"
#include <iostream>
#if (_MSC_VER >= 1000)
#include <crtdbg.h> // for the debug heap
#endif
#if defined(__MWERKS__) && defined(macintosh)
#include <console.h>
#endif
USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)
const int MAX_PHRASE_LENGTH=250;
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);
char *RSAEncryptString(const char *pubFilename, const char *seed, const char *message);
char *RSADecryptString(const char *privFilename, const char *ciphertext);
void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename);
bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename);
void DigestFile(const char *file);
string EncryptString(const char *plaintext, const char *passPhrase);
string DecryptString(const char *ciphertext, const char *passPhrase);
void EncryptFile(const char *in, const char *out, const char *passPhrase);
void DecryptFile(const char *in, const char *out, const char *passPhrase);
void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed);
void SecretRecoverFile(int threshold, const char *outFilename, char *const *inFilenames);
void InformationDisperseFile(int threshold, int nShares, const char *filename);
void InformationRecoverFile(int threshold, const char *outFilename, char *const *inFilenames);
void GzipFile(const char *in, const char *out, int deflate_level);
void GunzipFile(const char *in, const char *out);
void ForwardTcpPort(const char *sourcePort, const char *destinationHost, const char *destinationPort);
bool Validate(int);
#ifdef __BCPLUSPLUS__
int cmain(int argc, char *argv[])
#else
int main(int argc, char *argv[])
#endif
{
#ifdef _CRTDBG_LEAK_CHECK_DF
// Turn on leak-checking
int tempflag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
tempflag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag( tempflag );
#endif
#if defined(__MWERKS__) && defined(macintosh)
argc = ccommand(&argv);
#endif
try
{
char command;
if (argc < 2)
command = 'h';
else
command = argv[1][0];
switch (command)
{
case 'g':
{
char seed[1024], privFilename[128], pubFilename[128];
unsigned int keyLength;
cout << "Key length in bits: ";
cin >> keyLength;
cout << "\nSave private key to file: ";
cin >> privFilename;
cout << "\nSave public key to file: ";
cin >> pubFilename;
cout << "\nRandom Seed: ";
ws(cin);
cin.getline(seed, 1024);
GenerateRSAKey(keyLength, privFilename, pubFilename, seed);
return 0;
}
case 'r':
{
switch (argv[1][1])
{
case 's':
RSASignFile(argv[2], argv[3], argv[4]);
return 0;
case 'v':
{
bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]);
cout << (verified ? "valid signature" : "invalid signature") << endl;
return 0;
}
default:
{
char privFilename[128], pubFilename[128];
char seed[1024], message[1024];
cout << "Private key file: ";
cin >> privFilename;
cout << "\nPublic key file: ";
cin >> pubFilename;
cout << "\nRandom Seed: ";
ws(cin);
cin.getline(seed, 1024);
cout << "\nMessage: ";
cin.getline(message, 1024);
char *ciphertext = RSAEncryptString(pubFilename, seed, message);
cout << "\nCiphertext: " << ciphertext << endl;
char *decrypted = RSADecryptString(privFilename, ciphertext);
cout << "\nDecrypted: " << decrypted << endl;
delete [] ciphertext;
return 0;
}
}
}
case 'm':
DigestFile(argv[2]);
return 0;
case 't':
{
// VC60 workaround: use char array instead of std::string to workaround MSVC's getline bug
char passPhrase[MAX_PHRASE_LENGTH], plaintext[1024];
cout << "Passphrase: ";
cin.getline(passPhrase, MAX_PHRASE_LENGTH);
cout << "\nPlaintext: ";
cin.getline(plaintext, 1024);
string ciphertext = EncryptString(plaintext, passPhrase);
cout << "\nCiphertext: " << ciphertext << endl;
string decrypted = DecryptString(ciphertext.c_str(), passPhrase);
cout << "\nDecrypted: " << decrypted << endl;
return 0;
}
case 'e':
case 'd':
{
char passPhrase[MAX_PHRASE_LENGTH];
cout << "Passphrase: ";
cin.getline(passPhrase, MAX_PHRASE_LENGTH);
if (command == 'e')
EncryptFile(argv[2], argv[3], passPhrase);
else
DecryptFile(argv[2], argv[3], passPhrase);
return 0;
}
case 's':
if (argv[1][1] == 's')
{
char seed[1024];
cout << "\nRandom Seed: ";
ws(cin);
cin.getline(seed, 1024);
SecretShareFile(atoi(argv[2]), atoi(argv[3]), argv[4], seed);
}
else
SecretRecoverFile(argc-3, argv[2], argv+3);
return 0;
case 'i':
if (argv[1][1] == 'd')
InformationDisperseFile(atoi(argv[2]), atoi(argv[3]), argv[4]);
else
InformationRecoverFile(argc-3, argv[2], argv+3);
return 0;
case 'v':
return !Validate(argc>2 ? atoi(argv[2]) : 0);
case 'b':
if (argc<3)
BenchMarkAll();
else
BenchMarkAll((float)atof(argv[2]));
return 0;
case 'z':
GzipFile(argv[3], argv[4], argv[2][0]-'0');
return 0;
case 'u':
GunzipFile(argv[2], argv[3]);
return 0;
case 'f':
ForwardTcpPort(argv[2], argv[3], argv[4]);
return 0;
default:
FileSource usage("usage.dat", true, new FileSink(cout));
return 1;
}
}
catch(CryptoPP::Exception &e)
{
cout << "CryptoPP::Exception caught: " << e.what() << endl;
return -1;
}
catch(std::exception &e)
{
cout << "std::exception caught: " << e.what() << endl;
return -2;
}
catch(...)
{
cout << "unknown exception caught" << endl;
return -3;
}
}
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)
{
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
HexEncoder privFile(new FileSink(privFilename));
priv.DEREncode(privFile);
privFile.MessageEnd();
RSAES_OAEP_SHA_Encryptor pub(priv);
HexEncoder pubFile(new FileSink(pubFilename));
pub.DEREncode(pubFile);
pubFile.MessageEnd();
}
char *RSAEncryptString(const char *pubFilename, const char *seed, const char *message)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(pubFile);
if (strlen(message) > pub.MaxPlainTextLength())
{
cerr << "message too long for this key\n";
abort();
}
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
char *outstr = new char[2*pub.CipherTextLength()+1];
pub.Encrypt(randPool, (byte *)message, strlen(message), (byte *)outstr);
HexEncoder hexEncoder;
hexEncoder.Put((byte *)outstr, pub.CipherTextLength());
hexEncoder.MessageEnd();
hexEncoder.Get((byte *)outstr, 2*pub.CipherTextLength());
outstr[2*pub.CipherTextLength()] = 0;
return outstr;
}
char *RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
HexDecoder hexDecoder;
hexDecoder.Put((byte *)ciphertext, strlen(ciphertext));
hexDecoder.MessageEnd();
SecByteBlock buf(priv.CipherTextLength());
hexDecoder.Get(buf, priv.CipherTextLength());
char *outstr = new char[priv.MaxPlainTextLength()+1];
unsigned messageLength = priv.Decrypt(buf, (byte *)outstr);
outstr[messageLength] = 0;
return outstr;
}
void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSASSA_PKCS1v15_SHA_Signer priv(privFile);
NullRNG rng; // RSASSA_PKCS1v15_SHA_Signer ignores the rng. Use a real RNG for other signature schemes!
FileSource f(messageFilename, true, new SignerFilter(rng, priv, new HexEncoder(new FileSink(signatureFilename))));
}
bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSASSA_PKCS1v15_SHA_Verifier pub(pubFile);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -