8 #include <openssl/rsa.h>
9 #include <openssl/pem.h>
10 #include <openssl/err.h>
12 #include <arpa/inet.h>
20 FILE* inputKeyFile = fopen(pKey.c_str(),
"r");
25 std::stringstream msg;
26 msg<<
"Cannot open file: "<<pKey;
27 err.SetDescription(__FILE__,__LINE__,msg.str());
32 if (!PEM_read_RSA_PUBKEY(inputKeyFile, &
fRSA, NULL, NULL)) {
34 err.SetDescription(__FILE__,__LINE__,
"Error loading RSA Public Key File");
38 if (!PEM_read_RSAPrivateKey(inputKeyFile, &
fRSA, NULL, NULL)) {
40 err.SetDescription(__FILE__,__LINE__,
"Error loading RSA Private Key File");
45 err.SetDescription(__FILE__,__LINE__,
"Mode must be encrypt or decrypt");
48 if(inputKeyFile) fclose(inputKeyFile);
49 fOutput = (
unsigned char*)malloc(
sizeof(
char)*RSA_size(
fRSA));
63 err.SetDescription(__FILE__,__LINE__,
"RSA key length too short");
75 std::vector<unsigned char> output(message.size());
76 for(
size_t i = 0; i < message.size(); i++) {
77 output[i] = message[i];
84 std::string output(message.size(),
' ');
85 for(
size_t i = 0; i < message.size(); i++) {
86 output[i] = (char)message[i];
92 size_t size = message.size();
95 err.SetDescription(__FILE__,__LINE__,
"String size is odd, must be even");
98 size_t outsize = size/2;
99 std::vector<unsigned char> output(outsize);
101 for(
size_t i = 0; i < message.size(); i+=2) {
102 byte[0] = message[i];
103 byte[1] = message[i+1];
106 sscanf(
byte,
"%02x", &tmp);
107 output[i/2] = (
unsigned char)tmp;
114 std::string output(message.size()*2,
' ');
116 for(
size_t i = 0; i < message.size(); i++) {
117 snprintf(
byte,3,
"%02x",message[i]);
118 output[i*2] =
byte[0];
119 output[i*2+1] =
byte[1];
126 size_t size =
sizeof(message);
127 std::vector<unsigned char> output(size);
128 const unsigned char* input =
reinterpret_cast<const unsigned char*
>(&message);
129 for(
size_t i = 0; i < size; i++) {
130 output[i] = input[i];
138 size_t size =
sizeof(output);
139 if(message.size() != size) {
141 std::stringstream msg;
142 msg<<
"QCryptoRSA::ConvertDouble: Input message size ("<<message.size()<<
")"
143 <<
" differs from "<<size;
144 err.SetDescription(__FILE__,__LINE__,msg.str());
147 unsigned char* coutput =
reinterpret_cast<unsigned char*
>(&output);
148 for(
size_t i = 0; i < size; i++) {
149 coutput[i] = message[i];
157 len_in = message.size();
161 std::stringstream msg;
162 msg<<
"Input message size ("<<message.size()<<
")"
164 err.SetDescription(__FILE__,__LINE__,msg.str());
168 std::stringstream msg;
169 msg<<
"Input message too long ("<<message.size()<<
")"
170 <<
" must be shorter than "<<
fMsgLength<<
" char";
171 err.SetDescription(__FILE__,__LINE__,msg.str());
178 input[len_in+i] = rand()%256;
182 len_out = RSA_public_encrypt(len_in, input,
185 len_out = RSA_public_encrypt(len_in, input,
191 len_out = RSA_private_decrypt(len_in, input,
194 len_out = RSA_private_decrypt(len_in, input,
200 std::stringstream msg;
202 ERR_error_string(ERR_get_error(), buf);
203 msg<<
"RSA failed with "<<buf;
204 err.SetDescription(__FILE__,__LINE__,msg.str());
219 unsigned char* output = (
unsigned char*) malloc(
sizeof(
unsigned char)*(input.size()+pad+beginpad));
221 for(
size_t i = 0; i < beginpad; i++) {
224 for(
size_t i = beginpad; i < input.size()+beginpad; i++) {
225 output[i] = input[i-beginpad];
227 for(
size_t i = input.size()+beginpad; i < input.size()+pad+beginpad; i++) {
236 std::vector<unsigned char> output(n);
237 for(
size_t i = 0; i < n; i++) {
238 output[i] = input[i];
246 printf(
"%s: ",header);
247 for(
size_t c = 0; c < n; c++) printf(
"%x",data[c]);
ClassImp(QCryptoRSA) QCryptoRSA
fixed-size encryption with the raw RSA algorithm. For envelope see QCryptoRSAEnvelope.
unsigned char * ConvertArray(const std::vector< unsigned char > &input, const size_t pad=0, const size_t beginpad=0) const
static std::vector< unsigned char > ConvertDouble(const double message)
convert from double
std::vector< unsigned char > Process(const std::vector< unsigned char > &message) const
encrypt or decrypt message
virtual ~QCryptoRSA()
destructor
static std::vector< unsigned char > ConvertString(const std::string &message)
convert from string
QCryptoRSA(const std::string &pKey, const Mode mode, bool nopad=true)
constructor: if ENCRYPT provide public key, otherwise provide private key
void Print(const char *header, const unsigned char *data, const size_t n)
static std::vector< unsigned char > ConvertHex(const std::string &message)
convert from hex-string
Mode
encrypt or decrypt mode
error class with error type and description