RSA - 基于内存代码实现
2017-07-26 21:40
148 查看
基于:openssl-1.0.1g
#define OPENSSL_AES_BITS_128 (128)
#define OPENSSL_AES_BITS_256 (256)
#define OPENSSL_AES_LEN_16 (16) // 16 = 128 / 8
#define OPENSSL_AES_LEN_32 (32) // 32 = 256 / 8
typedef enum
{
OPENSSL_RSA_PUBKEY = 1,
OPENSSL_RSA_PUBLICKEY,
OPENSSL_BULT
}OPENSSL_RSA_KEY_TYPE_E;
int openssl_rsa_generate_key(uint8_t *publicKey, uint8_t *privateKey, uint32_t keyLen, uint8_t key_type)
{
RSA *rsa = NULL;
BIGNUM *bne = NULL;
rsa = RSA_new();
bne = BN_new();
BN_set_word(bne, RSA_F4);
if (1 != RSA_generate_key_ex(rsa, keyLen, bne, NULL))
{
printf("RSA_generate_key err!\n");
return -1;
}
//start generate private key
BIO *bp = BIO_new(BIO_s_mem());
//BIO *bp = BIO_new_file("private.key", "w+");
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (PEM_write_bio_RSAPrivateKey(bp, rsa, NULL, NULL, 0, NULL, NULL) != 1)
{
printf("PEM_write_bio_RSAPrivateKey err!\n");
return -1;
}
//printf("create private key ok!\n");
BIO_read(bp, privateKey, keyLen);
BIO_free_all(bp);
bp = NULL;
//start generate public key
bp = BIO_new(BIO_s_mem());
//bp = BIO_new_file("public.key", "w+");
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (OPENSSL_RSA_PUBKEY == key_type)
{
if (PEM_write_bio_RSA_PUBKEY(bp, rsa) != 1)
{
printf("PEM_write_bio_RSAPublicKey err!\n");
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if (PEM_write_bio_RSAPublicKey(bp, rsa) != 1)
{
printf("PEM_write_bio_RSAPublicKey err!\n");
return -1;
}
}
//printf("create public key ok!\n");
BIO_read(bp, publicKey, keyLen);
BIO_free_all(bp);
bp = NULL;
RSA_free(rsa);
rsa = NULL;
return 0;
}
int openssl_rsa_public_key_encrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (OPENSSL_RSA_PUBKEY == key_type)
{
if ((rsa = PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSA_PUBKEY!\n", __FUNCTION__, __LINE__);
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if ((rsa = PEM_read_bio_RSAPublicKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPublicKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
if (RSA_PKCS1_PADDING == encrypt_mode)
{
if (in_len > (uint32_t)(RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE))
{
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
return -1;
}
}
if ((*out_len = RSA_public_encrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_public_encrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
int openssl_rsa_private_key_encrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((rsa = PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPrivateKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
if (RSA_PKCS1_PADDING == encrypt_mode)
{
if (in_len > (uint32_t)(RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE))
{
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
return -1;
}
}
if ((*out_len = RSA_private_encrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_private_encrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
int openssl_rsa_private_key_decrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((rsa = PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPrivateKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((*out_len = RSA_private_decrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_private_decrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
int openssl_rsa_public_key_decrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (OPENSSL_RSA_PUBKEY == key_type)
{
if ((rsa = PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSA_PUBKEY failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if ((rsa = PEM_read_bio_RSAPublicKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPublicKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
if ((*out_len = RSA_public_decrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_public_decrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
#define OPENSSL_AES_BITS_128 (128)
#define OPENSSL_AES_BITS_256 (256)
#define OPENSSL_AES_LEN_16 (16) // 16 = 128 / 8
#define OPENSSL_AES_LEN_32 (32) // 32 = 256 / 8
typedef enum
{
OPENSSL_RSA_PUBKEY = 1,
OPENSSL_RSA_PUBLICKEY,
OPENSSL_BULT
}OPENSSL_RSA_KEY_TYPE_E;
int openssl_rsa_generate_key(uint8_t *publicKey, uint8_t *privateKey, uint32_t keyLen, uint8_t key_type)
{
RSA *rsa = NULL;
BIGNUM *bne = NULL;
rsa = RSA_new();
bne = BN_new();
BN_set_word(bne, RSA_F4);
if (1 != RSA_generate_key_ex(rsa, keyLen, bne, NULL))
{
printf("RSA_generate_key err!\n");
return -1;
}
//start generate private key
BIO *bp = BIO_new(BIO_s_mem());
//BIO *bp = BIO_new_file("private.key", "w+");
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (PEM_write_bio_RSAPrivateKey(bp, rsa, NULL, NULL, 0, NULL, NULL) != 1)
{
printf("PEM_write_bio_RSAPrivateKey err!\n");
return -1;
}
//printf("create private key ok!\n");
BIO_read(bp, privateKey, keyLen);
BIO_free_all(bp);
bp = NULL;
//start generate public key
bp = BIO_new(BIO_s_mem());
//bp = BIO_new_file("public.key", "w+");
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (OPENSSL_RSA_PUBKEY == key_type)
{
if (PEM_write_bio_RSA_PUBKEY(bp, rsa) != 1)
{
printf("PEM_write_bio_RSAPublicKey err!\n");
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if (PEM_write_bio_RSAPublicKey(bp, rsa) != 1)
{
printf("PEM_write_bio_RSAPublicKey err!\n");
return -1;
}
}
//printf("create public key ok!\n");
BIO_read(bp, publicKey, keyLen);
BIO_free_all(bp);
bp = NULL;
RSA_free(rsa);
rsa = NULL;
return 0;
}
int openssl_rsa_public_key_encrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (OPENSSL_RSA_PUBKEY == key_type)
{
if ((rsa = PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSA_PUBKEY!\n", __FUNCTION__, __LINE__);
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if ((rsa = PEM_read_bio_RSAPublicKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPublicKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
if (RSA_PKCS1_PADDING == encrypt_mode)
{
if (in_len > (uint32_t)(RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE))
{
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
return -1;
}
}
if ((*out_len = RSA_public_encrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_public_encrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
int openssl_rsa_private_key_encrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((rsa = PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPrivateKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
if (RSA_PKCS1_PADDING == encrypt_mode)
{
if (in_len > (uint32_t)(RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE))
{
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
return -1;
}
}
if ((*out_len = RSA_private_encrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_private_encrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
int openssl_rsa_private_key_decrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((rsa = PEM_read_bio_RSAPrivateKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPrivateKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((*out_len = RSA_private_decrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_private_decrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
int openssl_rsa_public_key_decrypt(uint8_t *key, uint8_t key_type, uint8_t encrypt_mode, const uint8_t *in, uint32_t in_len, uint8_t *out, uint32_t *out_len)
{
BIO *bp = NULL;
RSA *rsa = NULL;
if (NULL == key)
{
printf("%s-%d:input key information error!\n", __FUNCTION__, __LINE__);
return -1;
}
if ((NULL == in) || (0 == in_len) || (NULL == out) || (NULL == out_len))
{
printf("%s-%d:input paramentes error!\n", __FUNCTION__, __LINE__);
return -1;
}
bp = BIO_new_mem_buf(key, -1);
if (NULL == bp)
{
printf("%s-%d:BIO_new_mem_buf failed!\n", __FUNCTION__, __LINE__);
return -1;
}
if (OPENSSL_RSA_PUBKEY == key_type)
{
if ((rsa = PEM_read_bio_RSA_PUBKEY(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSA_PUBKEY failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
else if (OPENSSL_RSA_PUBLICKEY == key_type)
{
if ((rsa = PEM_read_bio_RSAPublicKey(bp, &rsa, NULL, NULL)) == NULL)
{
printf("%s %d:PEM_read_bio_RSAPublicKey failure!\n", __FUNCTION__, __LINE__);
return -1;
}
}
if ((*out_len = RSA_public_decrypt(in_len, in, out, rsa, encrypt_mode)) < 0)
{
printf("%s %d:RSA_public_decrypt err!\n", __FUNCTION__, __LINE__);
return -1;
}
RSA_free(rsa);
rsa = NULL;
BIO_free_all(bp);
bp = NULL;
return 0;
}
相关文章推荐
- 基于crc32实现的内存的代码校验
- 撸代码--类QQ聊天实现(基于linux 管道 信号 共享内存)
- 撸代码--类QQ聊天实现(基于linux 管道 信号 共享内存)
- RSA - 基于文件代码实现
- 基于python实现jenkins自动发布代码平台 推荐
- 基于jquery的loading 加载提示效果实现代码
- 基于ring0层的内存搜索,实现枚举线程.
- 基于jquery的让页面控件不可用的实现代码
- 基于Spark实现随机森林代码
- 基于jQuery实现点击弹出层实例代码
- 基于visual c++之windows核心编程代码分析(47)实现交换网络的QQ号嗅探
- 基于物品的协同过滤算法:理论说明,代码实现及应用
- 基于MeanShift的视频目标跟踪算法及代码实现
- 基于jquery的从一个页面跳转到另一个页面的指定位置的实现代码
- 基于wpa_supplicant库的WIFI连接功能实现--wpa_cli命令代码改写
- Linux进程通信---共享内存 代码实现
- 100行代码实现最简单的基于FFMPEG+SDL的视频播放器(SDL1.x)
- 基于Android中dp和px之间进行转换的实现代码
- 基于HTML+CSS+JS实现增加删除修改tab导航特效代码
- 基于JS2Image实现圣诞树代码