基於Crtpto++的RSA簽名演算法

來源:互聯網
上載者:User

折騰了快1個小時,將常見的一些加密庫都測試一下,再根據情況選擇一個應用到項目 中去.crypto++國內用得蠻多的,資料還算比較齊全,但是讓我討厭的是源檔案太亂,把 所有的演算法都包括進去了,我目前不能辨別哪些檔案是我需要的,所以編譯crypto++的源 代碼產生的靜態連結庫居然達到了34M,很恐怖啊,軟體發布時光這個演算法庫就得34M,比 軟體本身還大了,正在想辦法提取自己需要的部分.

#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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.