tg_rsa.h

 

// 注意: 编译mbedtls时, 添加宏 MBEDTLS_RSA_NO_CRT (基于 mbedtls 2.16.1)
#ifndef _BVR_OPENSSL_H_
#define _BVR_OPENSSL_H_

#include <iostream>
#include <string>

typedef struct mbedtls_rsa_context RSA;

bool tg_rsa_init();
bool tg_rsa_deinit();

// 生成密钥 bits >= 512
bool tg_rsa_key_generate(RSA** rsa, int bits);
// 密钥转为字符串
bool tg_rsa_key_string(RSA* rsa, std::string& n, std::string& e, std::string& d);
// 字符串转换为密钥
bool tg_rsa_key_get(RSA** rsa, const std::string& n, const std::string& e, const std::string& d = "");
// 释放rsa
bool tg_rsa_key_free(RSA* rsa);

// 密钥加密 usePubKey: 是否使用公钥加密
bool tg_rsa_encrypt(bool usePubKey, RSA* encrypt, std::string src, std::string& dst);

// 密钥解密 usePriKey: 是否使用私钥加密
bool tg_rsa_decrypt(bool usePriKey, RSA* decrypt, std::string src, std::string& dst);

/** base64 编码 */
std::string tg_base64_encode(const std::string& str_data);
/** base64 解码 */
std::string tg_base64_decode(const std::string& str_encoded);

/** aes cbc 加密 */
int tg_aes_cbc_encrypt(const std::string& plaintext, const std::string& key, const std::string& iv, std::string& ciphertext);
/** aes cbc 解密 */
int tg_aes_cbc_decrypt(const std::string& ciphertext, const std::string& key, const std::string& iv, std::string& plaintext);

/** 计算数据MD5值*/
std::string tg_md5_encode(const std::string& data);

#endif //_BVR_OPENSSL_H_
View Code

相关文章: