当前位置:天才代写 > tutorial > C语言/C++ 教程 > 基于Crtpto++的RSA签名算法

基于Crtpto++的RSA签名算法

2017-11-04 08:00 星期六 所属: C语言/C++ 教程 浏览:1167

折腾了快1个小时,将常见的一些加密库都测试一下,再按照环境选择一个应用到项目 中去.crypto++海内用得蛮多的,资料还算较量齐全,可是让我讨厌的是源文件太乱,把 所有的算法都包罗进去了,我今朝不能分辨哪些文件是我需要的,所以编译crypto++的源 代码生成的静态链接库居然到达了34M,很可怕啊,软件宣布年华这个算法库就得34M,比 软件自己还大了,正在想步伐提取本身需要的部门.

基于Crtpto++的RSA签名算法

#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include <iostream>

using namespace std;
using namespace CryptoPP;
//------------------------
// 函数声明
//------------------------
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");
    //建设公钥,私钥配对
    GenerateRSAKey(1024, priKey, pubKey, seed);

    // RSA 加解密
    char message[1024] = {0};
    strcpy(message, "");
    cout<<"原始字符 串:\t"<<message<<endl<<endl;
    string encryptedText = RSAEncryptString(pubKey, seed, message);  // RSA 加密
    cout<<"加密后字符 串:\t"<<encryptedText<<endl<<endl;
    string decryptedText = RSADecryptString(priKey, encryptedText.c_str ());  // RSA 解密
    cout<<"解密后字符 串:\t"<<decryptedText<<endl<<endl;
    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;
}

//------------------------
// 界说全局的随机数
//------------------------
RandomPool & GlobalRNG()
{
    static RandomPool randomPool;
    return randomPool;
}

本文出自 “九黎部落” 博客,请务必保存此出处http://axiii.blog.51cto.com/396236/115

 

    关键字:

天才代写-代写联系方式