• 以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 = new byte[cipherDataLen];
    		RSAEncryptData(seed, sizeof(seed), publicKey, publicKeyLen,
    			(byte*)plaintext.c_str(), plaintext.length(), cipherData, cipherDataLen);
    		size_t plainDataLen = plaintext.length() * 2;
    		byte *plainData = new byte[plainDataLen];
    		RSADecryptData(seed, sizeof(seed), privateKey, privateKeyLen,
    			cipherData, cipherDataLen, plainData, plainDataLen);
    		plainData[plainDataLen] = '';
    		cout << (char*)plainData << endl;
    		cout << plainDataLen << endl;
    
    		delete[] plainData;
    		delete[] cipherData;
    		delete[] publicKey;
    		delete[] privateKey;
    	}
    	catch (std::exception &e)
    	{
    		cout << e.what() << endl;
    	}
    	cin.get();
    
    	return 0;
    }
    

      

  • 相关阅读:
    环境变量配置1
    Golang 类型转换,断言和显式强制转换
    Goland could not launch process: decoding dwarf section info at offset 0x0: too short 解决方案
    用puttygen工具把私钥id_rsa转换成公钥id_rsa.ppk
    JetBrains GoLand 2018 激活码/ 注册码(最新破解方法)
    Go学习笔记(只有链接)
    linux中的ftp命令
    Linux的学习之路
    like语句百分号前置会使用到索引吗?
    记录下每月生活开支
  • 原文地址:https://www.cnblogs.com/icedream/p/4165141.html
Copyright © 2020-2023  润新知