您的位置:首页 > 运维架构

openssl 非对称加密 RSA 加密解密以及签名验证签名

2016-12-02 15:58 471 查看
1. 简介

openssl rsa.h 提供了密码学中公钥加密体系的一些接口,

本文主要讨论利用rsa.h接口开发以下功能

公钥私钥的生成

公钥加密,私钥解密

私钥加密,公钥解密

签名:私钥签名

验证签名:公钥验签

2. 生成公钥私钥对

主要接口,

/* Deprecated version */
DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
(*callback) (int, int, void *),
void *cb_arg))

/* New version */
int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);


接口调用需要先生成一个大数,如下生成密钥对示例

//生成密钥对
RSA *r = RSA_new();
int bits = 512;
BIGNUM *e = BN_new();
BN_set_word(e, 65537);
RSA_generate_key_ex(r, bits, e, NULL);
//打印密钥
RSA_print_fp(stdout, r, 0);


打印的密钥对结果:

test sign and verify
sign_verify=1


View Code
6. 总结

上述RSA分组加密中使用了RSA_PKCS1_PADDING 的补位方式;当然还有如下

不同的补位方式,在进行分组加密时,需要注意分组块的处理

# define RSA_PKCS1_PADDING       1
# define RSA_SSLV23_PADDING      2
# define RSA_NO_PADDING          3
# define RSA_PKCS1_OAEP_PADDING  4
# define RSA_X931_PADDING        5
/* EVP_PKEY_ only */
# define RSA_PKCS1_PSS_PADDING   6

# define RSA_PKCS1_PADDING_SIZE  11


测试使用 openssl 1.1.0c

参考:https://www.openssl.org/docs/man1.0.2/crypto/RSA_public_encrypt.html
https://www.openssl.org/docs/manmaster/man3/RSA_verify.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: