void encrypt_buffer_with_key(char* buffer, char* pubkey) { if(pubkey[0] == '\0') return; RSA * rsa = createRSA(pubkey,1); char encrypted[BUFFER_SIZE]; RSA_public_encrypt(BUFFER_SIZE, buffer, encrypted, rsa, RSA_PKCS1_PADDING); for(int i=0; i<BUFFER_SIZE; ++i) buffer[i] = encrypted[i]; }
// Decrypts data ecnrypted by RSA algorithm std::vector<uint8_t> Rsa::Decrypt(std::vector<uint8_t> const& data, unsigned char* key, bool isPublic) { if (data.empty()) return data; uint8_t decrypted[4098]; RSA* rsa = createRSA(key, isPublic); if (!rsa) return data; int(*decryptFnc)(int, unsigned char const*, unsigned char*, RSA*, int) = isPublic ? RSA_public_decrypt : RSA_private_decrypt; int decryptedLength = decryptFnc((int)data.size(), data.data(), decrypted, rsa, RSA_PKCS1_PADDING); if (decryptedLength == -1) return data; std::vector<uint8_t> value(decryptedLength); std::memcpy(&value[0], decrypted, decryptedLength); return value; }
int private_decrypt(unsigned char* data, int data_len, unsigned char* key, unsigned char* decrypted, int padding){ RSA* rsa = createRSA(key, 0); int result = RSA_private_decrypt(data_len, data, decrypted, rsa, padding); RSA_free(rsa); return result; }
int public_encrypt(unsigned char* data, int data_len, unsigned char* key, unsigned char* encrypted, int padding){ RSA* rsa = createRSA(key, 1); int result = RSA_public_encrypt(data_len, data, encrypted, rsa, padding); RSA_free(rsa); return result; }
static int public_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted) { RSA * rsa = createRSA(key,1); int result = RSA_public_decrypt(data_len,enc_data,decrypted,rsa,padding); return result; }
static int private_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted) { RSA * rsa = createRSA(key,0); int result = RSA_private_encrypt(data_len,data,encrypted,rsa,padding); return result; }
int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted) { RSA * rsa = createRSA(key,0); int result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,RSA_PKCS1_PADDING); return result; }