首页 > 代码库 > 以Crypto++实现RSA加解密二进制数据
以Crypto++实现RSA加解密二进制数据
网上一大片讲怎么加解密字符串的,找了大半天也没找到讲加解密二进制数据的,于是自己研究了下,分享给大家。
加解密函数:
#include <rsa.h>#include <randpool.h>#include <filters.h>void GenerateRSAKey(byte seed[], size_t seedLen, size_t keyLen, byte privateKey[], size_t &privateKeyLen, byte publicKey[], size_t &publicKeyLen){ CryptoPP::RandomPool randomPool; randomPool.Put(seed, seedLen); CryptoPP::RSAES_OAEP_SHA_Decryptor decryptor(randomPool, 1024); CryptoPP::ArraySink decArr(privateKey, privateKeyLen); decryptor.DEREncode(decArr); decArr.MessageEnd(); privateKeyLen = decArr.TotalPutLength(); CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(decryptor); CryptoPP::ArraySink encArr(publicKey, publicKeyLen); encryptor.DEREncode(encArr); encArr.MessageEnd(); publicKeyLen = encArr.TotalPutLength();}void RSAEncryptData(byte seed[], size_t seedLen, byte publicKey[], size_t publicKeyLen, byte plainData[], size_t plainDataLen, byte cipherData[], size_t &cipherDataLen){ CryptoPP::ArraySource keyArr(publicKey, publicKeyLen, true); CryptoPP::RSAES_OAEP_SHA_Encryptor enc; enc.AccessKey().Load(keyArr); CryptoPP::RandomPool randomPool; randomPool.Put(seed, seedLen); size_t putLen = 0; size_t fixedLen = enc.FixedMaxPlaintextLength(); for (size_t i = 0; i < plainDataLen; i += fixedLen) { size_t len = fixedLen < (plainDataLen - i) ? fixedLen : (plainDataLen - i); CryptoPP::ArraySink *dstArr = new CryptoPP::ArraySink(cipherData + putLen, cipherDataLen - putLen); CryptoPP::ArraySource source(plainData + i, len, true, new CryptoPP::PK_EncryptorFilter(randomPool, enc, dstArr)); putLen += dstArr->TotalPutLength(); } cipherDataLen = putLen;}void RSADecryptData(byte seed[], size_t seedLen, byte privateKey[], size_t privateKeyLen, byte cipherData[], size_t cipherDataLen, byte plainData[], size_t &plainDataLen){ CryptoPP::ArraySource keyArr(privateKey, privateKeyLen, true); CryptoPP::RSAES_OAEP_SHA_Decryptor dec; dec.AccessKey().Load(keyArr); CryptoPP::RandomPool randomPool; randomPool.Put(seed, seedLen); size_t putLen = 0; size_t fixedLen = dec.FixedCiphertextLength(); for (size_t i = 0; i < cipherDataLen; i += fixedLen) { size_t len = fixedLen < (cipherDataLen - i) ? fixedLen : (cipherDataLen - i); CryptoPP::ArraySink *dstArr = new CryptoPP::ArraySink(plainData + putLen, plainDataLen - putLen); CryptoPP::ArraySource source(cipherData + i, len, true, new CryptoPP::PK_DecryptorFilter(randomPool, dec, dstArr)); putLen += dstArr->TotalPutLength(); } plainDataLen = putLen;}
测试函数:
#include <iostream>#include <string>#include "CryptRsa.h"using namespace std;using namespace CryptoPP;int main(int argc, char *argv){ try { string plaintext = "{0FA152B4-D176-4D95-8363-BA9DFC787651}"; cout << plaintext << endl; cout << plaintext.length() << endl; byte seed[] = { 0xCA, 0xDA, 0x63, 0xEC, 0x9B, 0x89, 0x40, 0xDE, 0x8E, 0x64, 0x94, 0xE8, 0x79, 0xEA, 0x32, 0x9E }; size_t privateKeyLen = 2048; byte *privateKey = new byte[privateKeyLen]; size_t publicKeyLen = 2048; byte *publicKey = new byte[publicKeyLen]; GenerateRSAKey(seed, sizeof(seed), 1024, privateKey, privateKeyLen, publicKey, publicKeyLen); size_t cipherDataLen = 2048; byte *cipherData = http://www.mamicode.com/new byte[cipherDataLen];>
以Crypto++实现RSA加解密二进制数据
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。