代码片段:Crypto++使用方法汇总
作者:BlogUpdater |
时间:2017-06-26 |
浏览:9455 |
评论已关闭 条评论
今天来说简单汇总一下著名C++编解码库Crypto++的使用方法,以下仅列出代码片段,详细的API解释请大家参考在Crypto++官网文档。
基本上这个库的调用方式比较少见,但是学会了其中一种,其他的算法函数也是可以融会贯通的。
1) AES加密:
string m_buf; CryptoPP::AES::Encryption aes_encryption(m_key, CryptoPP::AES::MAX_KEYLENGTH); CryptoPP::CBC_Mode_ExternalCipher::Encryption cbc_encryption(aes_encryption, m_iv); CryptoPP::StreamTransformationFilter cbc_encryptor(cbc_encryption, new CryptoPP::HexEncoder(new CryptoPP::StringSink(m_buf))); cbc_encryptor.Put(reinterpret_cast<const byte *>(plain_str.c_str()), plain_str.length()); cbc_encryptor.MessageEnd();
2) AES解密:
string m_buf; CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption cbc_decryption(m_key, CryptoPP::AES::MAX_KEYLENGTH, m_iv); CryptoPP::HexDecoder decryptor(new CryptoPP::StreamTransformationFilter(cbc_decryption, new CryptoPP::StringSink(m_buf))); decryptor.Put(reinterpret_cast<const byte *>(cipher_A.GetBuffer()), cipher_A.GetLength()); decryptor.MessageEnd();
3) Base64编码:
string cipher; CryptoPP::StringSource(plain, length_in, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(cipher), false));
4) Base64解码:
string cipher_str; CryptoPP::StringSource(cipher_str, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(m_buf)));
5) 对文件进行MD5:
CryptoPP::Weak1::MD5 md5; CryptoPP::FileSource(path, true, new CryptoPP::HashFilter(md5, new CryptoPP::HexEncoder(new CryptoPP::StringSink(str))));
6) Hex编码:
string encoded("");
CryptoPP::StringSource ss(bin, length, true, new CryptoPP::HexEncoder(new CryptoPP::StringSink(encoded)));
7) Hex解密:
string bin("");
CryptoPP::StringSource ssk(hexA, true, new CryptoPP::HexDecoder(new CryptoPP::StringSink(bin)));
8)RSA加密:
void CRSACore::EncryptString(const string &strPub, const char *Seed, const string &Plaintext, string &Ciphertext)
{
CryptoPP::StringSource PublicKey(strPub, true, new CryptoPP::HexDecoder);
CryptoPP::RSAES_OAEP_SHA_Encryptor Pub(PublicKey);
CryptoPP::RandomPool RandPool;
RandPool.IncorporateEntropy((byte *)Seed, strlen(Seed));
int MaxMsgLength = (int)Pub.FixedMaxPlaintextLength();
for (int i = (int)Plaintext.size(), j=0; i > 0; i -= MaxMsgLength, j += MaxMsgLength)
{
string PartPlaintext = Plaintext.substr(j, MaxMsgLength);
string PartCiphertext;
CryptoPP::StringSource(PartPlaintext, true, new CryptoPP::PK_EncryptorFilter(RandPool, Pub, new CryptoPP::HexEncoder(new CryptoPP::StringSink(PartCiphertext))));
Ciphertext += PartCiphertext;
}
}
9) RSA解密:
CryptoPP::StringSource PrivKey(strPriv, true, new CryptoPP::HexDecoder);
CryptoPP::RSAES_OAEP_SHA_Decryptor Priv(PrivKey);
CryptoPP::RandomPool rand_poll;
// indicate the ciphertext in hexcode
int CiphertextLength = (int)Priv.FixedCiphertextLength() * 2;
for (int i = (int)Ciphertext.size(), j=0; i > 0; i -= CiphertextLength, j += CiphertextLength)
{
string PartCiphertext = Ciphertext.substr(j, CiphertextLength);
string PartPlaintext;
CryptoPP::StringSource(PartCiphertext, true, new CryptoPP::HexDecoder(new CryptoPP::PK_DecryptorFilter(rand_poll, Priv, new CryptoPP::StringSink(PartPlaintext))));
Plaintext += PartPlaintext;
}
如需转载,请注明来自:拓扑梅尔智慧办公平台 | TopomelBox 官方站点
相关推荐
- 第 468 期:微软表示微软 365 Copilot 现在已成为用户的日常习惯
- Posted on 02月03日
- 关于Hollow画刷的解释
- Posted on 10月31日
- 第 228 期:微软推出 Windows 11 24H2 KB5065789 更新
- Posted on 10月04日
- 第 374 期:微软终于对 Windows 11 上混乱的应用程序更新做了些改变
- Posted on 12月10日



评论已关闭。