自己封装Crypto++的RSA类

 //头文件

#pragma once
//#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
//#include <afx.h>
#include <iostream>
//#include <winioctl.h>
#include <Windows.h>
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include "rng.h"
#include "default.h"
#include "randpool.h"
#include "ida.h"
#include "base64.h"
#include "des.h"
#include "modes.h"
//#include "socketft.h"
//#include "wait.h"
#include "factory.h"
#include "whrlpool.h"
#include "validate.h"
#include "osrng.h"

//#include "computerInfo.h"
USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)

#pragma comment(lib, "cryptlib.lib")

class RSAInterface
{
public:

 RSA::PrivateKey m_privateKey;
 RSA::PublicKey m_publicKey;
 Integer n;
 Integer e;
 Integer d; 

public:
 RSAInterface(void);
 ~RSAInterface(void);

 //n,d,e必须是10进制数,当用RSA-TOOL生成N,D,E时Number Base要选10
 RSA::PrivateKey  GenerateRSAPrivateKey(const Integer &n, const Integer &d,const Integer &e);
 RSA::PublicKey   GenerateRSAPublicKey(const Integer &n, const Integer &e);//n,d,e必须是10进制数
 string RSAEncryptString(string plain);
 string RSADecryptString(string cipher);
 bool RSASet_n(Integer &nn);
 bool RSASet_e(Integer &ee);
 bool RSASet_d(Integer &dd);

 
};

//C++文件


#include "RsaInterface.h"
using namespace std;
RSAInterface::RSAInterface(void)
: n("")
, d("")
, e("")
{

 // 初始化公钥和私钥
 m_privateKey=GenerateRSAPrivateKey(n, d, e);
 m_publicKey=GenerateRSAPublicKey(n, e);

}

RSAInterface::~RSAInterface(void)
{
}

RSA::PrivateKey  RSAInterface::GenerateRSAPrivateKey(const Integer &n, const Integer &d, const Integer &e)
{

 InvertibleRSAFunction params;
 params.Initialize(n,e,d);
 RSA::PrivateKey m_privateKey( params );
 return m_privateKey;

}
RSA::PublicKey  RSAInterface::GenerateRSAPublicKey(const Integer &n, const Integer &e)
{
 RSAFunction params;
 params.Initialize(n,e);
 RSA::PublicKey m_publicKey( params );
 return m_publicKey;

}

string RSAInterface::RSAEncryptString(string plain)
{

 AutoSeededRandomPool rng;
 // Encryption

 string cipher;

 RSAES_OAEP_SHA_Encryptor en( m_publicKey );

 StringSource(  plain, true,
  new PK_EncryptorFilter( rng, en,
  new HexEncoder(new StringSink(cipher ))

  ) // PK_EncryptorFilter
  ); // StringSource

 return cipher;
}
string RSAInterface::RSADecryptString(string cipher)
{
 AutoSeededRandomPool rng;

 // Decryption

 string plain;

 RSAES_OAEP_SHA_Decryptor de( m_privateKey );

 StringSource( cipher, true,
  new HexDecoder(new PK_DecryptorFilter( rng, de,
  new StringSink( plain ))
  ) // PK_DecryptorFilter
  ); // StringSource

 return plain;

}

bool RSAInterface::RSASet_n(Integer &nn)
{
 n=nn;
 return true;
}
bool RSAInterface::RSASet_e(Integer &ee)
{
 e=ee;
 return true;
}
bool RSAInterface::RSASet_d(Integer &dd)
{
 d=dd;
 return true;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值