关闭

基于Crypto++/Cryptopp的rsa密钥生成,rsa加密、解密,rsa签名、验签

898人阅读 评论(0) 收藏 举报
分类:

4.生成rsa公钥、私钥,经过base64编码后保存到文件
需要导入一些头文件:

#include "iterhash.h"
#include "files.h"
#include "rsa.h"
#include "randpool.h"
#include "hex.h"
#include "base64.h"
#include "osrng.h"
void CKeyController::GenerateRSAKey(unsigned int keyLength, CString decFilename, CString encFilename, CString seed)
{
    RandomPool randPool;
    randPool.Put((byte *)seed.GetBuffer(seed.GetLength()), seed.GetLength());

    RSAES_OAEP_SHA_Decryptor decrypt(randPool, keyLength);
    HexEncoder decFile(new Base64Encoder(new FileSink(decFilename.GetBuffer(decFilename.GetLength()))));

    decrypt.DEREncode(decFile);
    decFile.MessageEnd();

    RSAES_OAEP_SHA_Encryptor encrypt(decrypt);
    HexEncoder encFile(new Base64Encoder(new FileSink(encFilename.GetBuffer(encFilename.GetLength()))));
    encrypt.DEREncode(encFile);

    encFile.MessageEnd();

    return;
}
RandomPool & CKeyController::GlobalRNG()
{
    static RandomPool randomPool;
    return randomPool;
}

5.通过生成的公钥文件加密字符串

CString CKeyController::RSAEncryptString( CString encFilename, CString seed, CString message )
{
    string encString;
    FileSource encFile( encFilename.GetBuffer(encFilename.GetLength())truenew Base64Decoder(new StringSink(encString)) );
    HexDecoder decoder;
    decoder.Put( (byte*)encString.c_str(), encString.size() );
    decoder.MessageEnd();

    RSAES_OAEP_SHA_Encryptor enc;
    enc.AccessKey().Load(decoder);

    RandomPool randPool;
    randPool.Put( (byte *)seed.GetBuffer(seed.GetLength()), seed.GetLength() );

    string result;
    StringSource( c2s(message)truenew PK_EncryptorFilter(randPool, enc, new HexEncoder(new StringSink(result))) );

    return CString(result.c_str());
}

6.通过生成的私钥文件解密字符串

CString CKeyController::RSADecryptString( CString decFilename, CString ciphertext )
{
    string decString;
    FileSource decFile( decFilename.GetBuffer(decFilename.GetLength())truenew Base64Decoder(new StringSink(decString)) );
    HexDecoder decoder;
    decoder.Put( (byte*)decString.c_str(), decString.size() );
    decoder.MessageEnd();

    RSAES_OAEP_SHA_Decryptor dec;
    dec.AccessKey().Load(decoder);

    string result;
    StringSource( c2s(ciphertext)truenew HexDecoder(new PK_DecryptorFilter(GlobalRNG(), dec, new StringSink(result))) );

    return CString(result.c_str());
}

7.使用私钥签名

CString CKeyController::SignMessage( const std::string& privateKeyFileName, const std::string& message )
{
    std::string signedMessage = "";
    string encString;
    FileSource privFile( privateKeyFileName.c_str()truenew Base64Decoder(new StringSink(encString)));
    RSASSA_PKCS1v15_SHA_Signer priv;

    HexDecoder decoder;
    decoder.Put( (byte*)encString.c_str(), encString.size() );
    decoder.MessageEnd();

    priv.AccessKey().Load(decoder);

    AutoSeededRandomPool rng;
    StringSource s1(message, truenew SignerFilter(rng, priv, new HexEncoder(new StringSink(signedMessage))));
    
    return CString(signedMessage.c_str());
}

8.使用公钥验证签名

bool CKeyController::VerifySignature( const std::string& publicKeyFileName, const std::string& message, const std::string& signedMessage )
{
    string decString;
    FileSource pubFile( publicKeyFileName.c_str()truenew Base64Decoder(new StringSink(decString)) );
    RSASSA_PKCS1v15_SHA_Verifier pub;

    HexDecoder decoder;
    decoder.Put( (byte*)decString.c_str(), decString.size() );
    decoder.MessageEnd();

    pub.AccessKey().Load(decoder);

    StringSource signatureFile( signedMessage, truenew HexDecoder);
    if (signatureFile.MaxRetrievable() != pub.SignatureLength())
    { throw std::string( "Signature Size Problem" ); }

    SecByteBlock signature(pub.SignatureLength());
    signatureFile.Get(signature, signature.size());

    VerifierFilter *verifierFilter = new VerifierFilter(pub);
    verifierFilter->Put(signature, pub.SignatureLength());
    StringSource s(message, true, verifierFilter);

    return verifierFilter->GetLastResult();
}

9.测试函数调用

void CKeyController::testRSA()
{
    CString encryptKey = _T("key.pub");
    CString decryptKey = _T("key.pri");
    CString seed = _T("seed");

    //GenerateRSAKey( 1024, decryptKey, encryptKey, seed );

    CString message = _T("X3BA-9NSF-8N9Q-UWQC-U7FX-AZZF-JAJW");

    CString encryptedText = RSAEncryptString( encryptKey, seed, message );

    CString decryptedText = RSADecryptString( decryptKey, encryptedText );
    
    CString signedMessage = SignMessage("key.pri", c2s(decryptedText));

    bool verified = VerifySignature("key.pub", c2s(message), c2s(signedMessage));
}

10.工具函数如base64转码,字符串转换

std::string CKeyController::EncodeBase64( string message )
{
    string encode;
    StringSource(message, truenew Base64Encoder(new StringSink(encode)));
    return encode;
}

std::string CKeyController::DecodeBase64( string message )
{
    string decode;
    StringSource(message, truenew Base64Decoder(new StringSink(decode)));
    return decode;
}

CString CKeyController::hashString( CString message )
{
    string digest;
    SHA256 hash;
    StringSource foo(c2s(message)truenew HashFilter(hash, new HexEncoder(new StringSink(digest))));
    return CString(digest.c_str());
}

CString CKeyController::hashFile( CString fileName )
{
    string digest;
    SHA256 hash;
    FileSource(fileName, truenew HashFilter(hash, new HexEncoder(new StringSink(digest))));
    return CString(digest.c_str());
}

std::string c2s( CString &cs )
{
    CT2CA pszConvertedAnsiString(cs);
    std::string strStd(pszConvertedAnsiString);
    return strStd;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:237562次
    • 积分:3220
    • 等级:
    • 排名:第11002名
    • 原创:5篇
    • 转载:500篇
    • 译文:0篇
    • 评论:8条
    最新评论