將常見的一些加密庫都測試一下,再根據情況選擇一個應用到項目中去.crypto++國內用得蠻多的,資料還算比較齊全,但是讓我討厭的是源文件太亂,把所有的算法都包括進去了,我目前不能辨別哪些文件是我需要的,所以編譯crypto++的源代碼生成的靜態(tài)鏈接庫居然達到了34M,很恐怖啊,軟件發(fā)布時光這個算法庫就得34M,比軟件本身還大了,正在想辦法提取自己需要的部分.
#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include
using namespace std;
using namespace CryptoPP;
// 函數(shù)聲明
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);
string RSADecryptString(const char *privFilename, const char *ciphertext);
RandomPool & GlobalRNG();
// 主程序
int main()
{
char priKey[128] = {0};
char pubKey[128] = {0};
char seed[1024] = {0};
// 生成 RSA 密鑰對
strcpy(priKey, "private.ilcd"); // 生成的私鑰文件名
strcpy(pubKey, "public.ilcd"); // 生成的公鑰文件名
strcpy(seed, "seed");
//創(chuàng)建公鑰,考試,大提示私鑰配對
GenerateRSAKey(1024, priKey, pubKey, seed);
// RSA 加解密
char message[1024] = {0};
strcpy(message, "www.ilcd.tv");
cout<<"原始字符串:\t"< string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密
cout<<"加密后字符串:\t"< string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密
cout<<"解密后字符串:\t"< return 0;
}
// 生成RSA密鑰對
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();
}
// RSA加密
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(pubFile);
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
string result;
StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));
return result;
}
// RSA解密
string RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}
// 定義全局的隨機數(shù)
RandomPool & GlobalRNG()
{
static RandomPool randomPool;
return randomPool;
}
#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include
using namespace std;
using namespace CryptoPP;
// 函數(shù)聲明
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);
string RSADecryptString(const char *privFilename, const char *ciphertext);
RandomPool & GlobalRNG();
// 主程序
int main()
{
char priKey[128] = {0};
char pubKey[128] = {0};
char seed[1024] = {0};
// 生成 RSA 密鑰對
strcpy(priKey, "private.ilcd"); // 生成的私鑰文件名
strcpy(pubKey, "public.ilcd"); // 生成的公鑰文件名
strcpy(seed, "seed");
//創(chuàng)建公鑰,考試,大提示私鑰配對
GenerateRSAKey(1024, priKey, pubKey, seed);
// RSA 加解密
char message[1024] = {0};
strcpy(message, "www.ilcd.tv");
cout<<"原始字符串:\t"<
cout<<"加密后字符串:\t"<
cout<<"解密后字符串:\t"<
}
// 生成RSA密鑰對
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();
}
// RSA加密
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(pubFile);
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
string result;
StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));
return result;
}
// RSA解密
string RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}
// 定義全局的隨機數(shù)
RandomPool & GlobalRNG()
{
static RandomPool randomPool;
return randomPool;
}

