系列文章目錄
1.(全網(wǎng)最詳細(xì)攻略)【Crypto++】在Visual studio2022中運(yùn)行Cryptopp
前言
crypto++是一個(gè)開源密碼學(xué)函數(shù)庫(kù),里面含有很多加密函數(shù)的庫(kù)供大家引用,在官網(wǎng)中也能看到許多代碼示范樣例。
本文將記錄如何使用開發(fā)環(huán)境:visual studio,引用crypto++的庫(kù)編寫RSA加密的代碼。
一、RSA加密過程、步驟
RSA的安全性依賴于大數(shù)分解,由于進(jìn)行的都是大數(shù)計(jì)算,使得RSA最快的情況也比DES慢上好幾倍,無論是軟件還是硬件實(shí)現(xiàn)。速度一直是RSA的缺陷。一般來說只用于少量數(shù)據(jù)加密。多用于數(shù)字簽名。下面是數(shù)字簽名的步驟:
- 待發(fā)送消息(message)利用Hash函數(shù),生成信息的摘要
- 私鑰加密摘要,生成”數(shù)字簽名”(signature)
- 發(fā)送message+signature
- 公鑰解密簽名
- message重新生成摘要,與發(fā)送過來的摘要進(jìn)行比較
公鑰和私鑰:公鑰和私鑰是成對(duì)的,它們互相解密。
- 加密:公鑰加密,私鑰解密。
- 數(shù)字簽名:私鑰數(shù)字簽名,公鑰驗(yàn)證。
可學(xué)習(xí)的網(wǎng)址
RSA原理:這個(gè)網(wǎng)址講得詳細(xì),可參考學(xué)習(xí)
crypto++官網(wǎng)中RSA的相關(guān)代碼:RSA Cryptography
這個(gè)博主寫的代碼很詳細(xì):RSA封裝代碼學(xué)習(xí)
二、代碼部分
1.visual studio編程注意
在visual studio中,僅支持一個(gè)main函數(shù),所以使用多個(gè)完整的cpp結(jié)構(gòu)編寫源代碼將無法正確運(yùn)行。
正確做法:封裝。編寫一個(gè)主函數(shù)main。cpp,使用多個(gè)頭文件xxx.h編寫頭文件和函數(shù)聲明部分,使用多個(gè)xxx.cpp編寫函數(shù)定義部分,在main.cpp中集中調(diào)用。一個(gè)標(biāo)準(zhǔn)案例提供給大家
如何在VisualStdio中運(yùn)行一個(gè)包含多個(gè)源文件的程序
2.RSA密鑰生成
void GenerateKeys(AutoSeededRandomPool rng, InvertibleRSAFunction parameters) {
parameters.GenerateRandomWithKeySize(rng, 1024);
RSA::PrivateKey privateKey(parameters);
RSA::PublicKey publicKey(parameters);
}
思考:
此處使用
void
作為函數(shù)返回值是不對(duì)的,因?yàn)槲覀円a(chǎn)生一對(duì)公私鑰密鑰對(duì)。所以我們應(yīng)該使用什么來接收結(jié)果(公鑰,私鑰)?
需要構(gòu)造結(jié)構(gòu)體!
struct key
{
RSA::PrivateKey private_key;
RSA::PublicKey public_key;
};
MyRSA.h
struct key
{
RSA::PrivateKey private_key;
RSA::PublicKey public_key;
};
key generate_key(unsigned int key_size = 2048);
RSA.cpp
#include "MyRSA.h"
/**
* \brief 生成密鑰對(duì)
* \param key_size
* \return
*/
key generate_key(const unsigned int key_size) //鑰匙的長(zhǎng)度,通常是2048以上,越大相對(duì)越安全,但相對(duì)的運(yùn)算越久
{
AutoSeededRandomPool rng; //偽隨機(jī)數(shù)
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, key_size);
key keys;
keys.private_key = params;
keys.public_key = params; //根據(jù)這個(gè)參數(shù)產(chǎn)生配對(duì)的公鑰、密鑰并返回這個(gè)集合
return keys;
}
3.關(guān)于RSA的五個(gè)基本函數(shù)
- 生成密鑰對(duì)
- 加密
- 解密
- 私鑰簽名
- 公鑰驗(yàn)證簽名
完整代碼如下:
initializeCrypto.h
/*
*
* Created on: 2023-8-28
* Author: Chen Jingyan
*/
#include "cryptlib.h"
using namespace CryptoPP;
MyRSA.h
#ifndef CRYPTO_RSA_H
#define CRYPTO_RSA_H
#include"initializeCrypto.h"
#include "pssr.h"
#include "rsa.h"
#include "osrng.h"
struct key
{
RSA::PrivateKey private_key;
RSA::PublicKey public_key;
};
key generate_key(unsigned int key_size = 2048);
bool rsa_encrypt(const std::string& plain, RSA::PublicKey& public_key, std::string& cipher);
bool rsa_decrypt(const std::string& cipher, RSA::PrivateKey& private_key, std::string& recovered);
bool rsa_signature(const std::string& plain, RSA::PrivateKey& private_key, std::string& signature);
bool rsa_verify(const std::string& cipher_sign, RSA::PublicKey& public_key, std::string& recovered);
#endif
RSA.CPP
#include "MyRSA.h"
/**
* \brief 生成密鑰對(duì)
* \param key_size
* \return
*/
key generate_key(const unsigned int key_size) //鑰匙的長(zhǎng)度,通常是2048以上,越大相對(duì)越安全,但相對(duì)的運(yùn)算越久
{
AutoSeededRandomPool rng; //偽隨機(jī)數(shù)
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(rng, key_size);
key keys;
keys.private_key = params;
keys.public_key = params; //根據(jù)這個(gè)參數(shù)產(chǎn)生配對(duì)的公鑰、密鑰并返回這個(gè)集合
return keys;
}
/**
* \brief RSA加密(OAEP方案) 最佳非對(duì)稱加密填充(OAEP)
* \param plain 待加密原文
* \param public_key 公鑰
* \param cipher 密文
* \return 加密成功或失敗
*/
bool rsa_encrypt(const std::string& plain, RSA::PublicKey& public_key, std::string& cipher)
{
try
{
AutoSeededRandomPool rng;
const RSAES_OAEP_SHA_Encryptor encrypt(public_key);
StringSource ss_1(plain, true, new PK_EncryptorFilter(rng, encrypt, new StringSink(cipher)));
}
catch (...)
{
return false;
}
return true;
}
/**
* \brief RSA解密(OAEP方案)
* \param cipher 密文
* \param private_key 密鑰
* \param recovered 恢復(fù)的原文
* \return 恢復(fù)成功或失敗
*/
bool rsa_decrypt(const std::string& cipher, RSA::PrivateKey& private_key, std::string& recovered)
{
try
{
AutoSeededRandomPool rng;
const RSAES_OAEP_SHA_Decryptor decrypt(private_key);
StringSource ss_2(cipher, true, new PK_DecryptorFilter(rng, decrypt, new StringSink(recovered)));
}
catch (...)
{
return false;
}
return true;
}
/**
* \brief:簽名
* \param plain
* \param private_key
* \param signature
* \return
*/
bool rsa_signature(const std::string& plain, RSA::PrivateKey& private_key, std::string& signature)
{
try
{
AutoSeededRandomPool rng;
const RSASS< PSS, SHA256>::Signer signer(private_key);
StringSource ss_1(plain, true, new SignerFilter(rng, signer, new StringSink(signature)));
}
catch (...)
{
return false;
}
return true;
}
/**
* \brief:公鑰驗(yàn)證簽名
* \param cipher_sign
* \param public_key
* \param recovered
* \return true/faluse
*/
bool rsa_verify(const std::string& cipher_sign, RSA::PublicKey& public_key, std::string& recovered)
{
try
{
const RSASS< PSS, SHA256>::Verifier verifier(public_key);
StringSource ss_2(cipher_sign, true,
new SignatureVerificationFilter(verifier,
new StringSink(recovered), SignatureVerificationFilter::THROW_EXCEPTION | SignatureVerificationFilter::PUT_MESSAGE));
}
catch (...)
{
return false;
}
return true;
}
main.cpp
文章來源:http://www.zghlxwxcb.cn/news/detail-679494.html
#include "timeSpan.h"
#include"MyRsa.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
/*RSA加密*/
key keys = generate_key();
const string plain = "HelloWorld";
string cipher, recovered;
if (rsa_encrypt(plain, keys.public_key, cipher))
{
if (rsa_decrypt(cipher, keys.private_key, recovered))
{
cout << "Cipher: " << endl;
for (const auto& x : cipher)
{
cout
<< setfill('0')
<< setw(2)
<< setiosflags(ios::uppercase)
<< hex << static_cast<unsigned int>(static_cast<unsigned char>(x))
<< " ";
}
cout << "Recovered message: " << recovered << endl;
}
}
return 0;
}
VS 使用技巧總結(jié)
1. VS修改每次新建源文件或頭文件時(shí)將自動(dòng)添加寫好的注釋內(nèi)容.
步驟:文章來源地址http://www.zghlxwxcb.cn/news/detail-679494.html
- 根據(jù)下面地址找到文件
newc++file.cpp
:
D:\visual studio\Common7\IDE\VC\VCProjectItems\newc++file.cpp - 將其復(fù)制到桌面,然后用記事本方式打開;
- 修改注釋內(nèi)容,下面給一個(gè)例子:
/*
* file name:
*
* Created on: 2023--
* Author: 宇宙修理員
*/
- 保存后,將桌面文件復(fù)制回去步驟1的地址中,將原來文件覆蓋。
2. 一次性修改所有相關(guān)命令
- 選定要修改的命令,ctrl+H,出現(xiàn)下面這個(gè)搜索框
- 輸入空格,點(diǎn)擊“全部替換”圖標(biāo)。
到了這里,關(guān)于【crypto++使用】使用crypto++庫(kù)函數(shù)運(yùn)行RSA非對(duì)稱加密的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!