Openssl 学习—3.RSA

Openssl 学习—3.RSA

一、介绍

非对称加密,区块加密

RSA是1977年由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的。当时他们三人都在麻省理工学院工作。RSA就是他们三人姓氏开头字母拼在一起组成的。

RSA算法是一个广泛使用的公钥算法。其密钥包括公钥和私钥。它能用于数字签名、身份认证以及密钥交换。RSA密钥长度一般使用1024位或者更高。

1. openssl中的RSA

具体的简介参考:http://zh.wikipedia.org/wiki/RSA加密演算法(!重要)

主要用到三个参数:n,e,d。其中公钥(n,e),私钥(n,d)(有关私钥的操作中用到e,所以可理解为私钥:n,e,d

RSA实现源码在crypto/rsa目录下。

2. RSA的有关数据结构

struct rsa_st

{

/* 其他 */

const RSA_METHOD *meth;

ENGINE *engine;

BIGNUM *n;                //参数n

BIGNUM *e;                //参数e

BIGNUM *d;                //参数d

BIGNUM *p;

BIGNUM *q;

BIGNUM *dmp1;

BIGNUM *dmq1;

BIGNUM *iqmp;

CRYPTO_EX_DATA ex_data;

int references;

/* 其他数据项 */

}RSA;

其中,meth字段是本RSA密钥各种运算函数的地址,若自己定义了算法,可在此指定,默认为(不重要):

struct rsa_meth_st

{

const char *name;

int (*rsa_pub_enc)(int flen,const unsigned char *from,unsigned char *to,RSA *rsa,int padding);

int (*rsa_pub_dec)(int flen,const unsigned char *from,unsigned char *to,RSA *rsa,int padding);

int (*rsa_priv_enc)(int flen,const unsigned char *from,unsigned char *to,RSA *rsa,int padding);

int (*rsa_priv_dec)(int flen,const unsigned char *from,unsigned char *to,RSA *rsa,int padding);

/* 其他函数 */

int (*rsa_sign)(int type,const unsigned char *m, unsigned int m_length,unsigned char *sigret, unsigned int *siglen, const RSA *rsa);

int (*rsa_verify)(int dtype,const unsigned char *m, unsigned int m_length,unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);

int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);

};

3. 编程示例

#define KEY_LEN 128 //bytes 密钥长度

void print_buf_hex( unsigned char *buf, int len )//用于打印字符

{

    int i;

    for( i = 0; i < len; i++ )

    {

        printf( "%02x", buf[i] );

    }

}

int _tmain(int argc, _TCHAR* argv[])

{

    int val;

    RSA *rsa;

    //RSA *rsa=RSA_new();

    //生密钥的参数

    rsa=RSA_generate_key(KEY_LEN * 8,RSA_F4,NULL,NULL);

    //到密钥的公钥,私钥

    //除原来的rsa(可选,原rsa可直接用于加密、解密)

    RSA *pub=RSAPublicKey_dup(rsa);

    RSA *pri=RSAPrivateKey_dup(rsa);

    RSA_free(rsa);

    unsigned char text[KEY_LEN]="hello world!";

    unsigned char from[KEY_LEN]="";

    unsigned char to[KEY_LEN]="";

    //------------------钥加密-----------------

    val=RSA_public_encrypt(KEY_LEN,text,to,pub,RSA_NO_PADDING);

    if(val<=0)

        return -1;

    cout<<"密后"<<endl;

    print_buf_hex(to,KEY_LEN);

    //-------------------钥解密--------------

    val=RSA_private_decrypt(KEY_LEN,to,from,pri,RSA_NO_PADDING);

    if(val<=0)

        return -1;

    cout<<"密后:"<<endl;

    print_buf_hex(from,KEY_LEN);

    cout<<endl;

    cout<<from<<endl;

    //------------------钥签名-----------------

    val=RSA_private_encrypt(KEY_LEN,text,to,pri,RSA_NO_PADDING);

    if(val<=0)

        return -1;

    cout<<"名后:"<<endl;

    print_buf_hex(to,KEY_LEN);

    cout<<endl;

    //------------------钥验证--------------

    val=RSA_public_decrypt(KEY_LEN,to,from,pub,RSA_NO_PADDING);

    if(val<=0)

        return -1;

    cout<<"证后:"<<endl;

    print_buf_hex(from,KEY_LEN);

    cout<<endl;

    cout<<from<<endl;

    return 0;

}

4. 主要函数

1) RSA_new

生成一个RSA密钥结构

2) RSA_free

释放RSA结构。

3) RSA *RSA_generate_key(int bits, unsigned long e_value,

void (*callback)(int,int,void *), void *cb_arg)

生成RSA密钥,

bits是模数比特数,

e_value是公钥指数e,可指定为RSA_F4,即0x10001,即65537

callback回调函数由用户实现,用于干预密钥生成过程中的一些运算,可为空。

4) RSA_check_key

检查RSA密钥。

5)int RSA_print(BIO *bp, const RSA *x, int off)

RSA信息输出到BIO中,off为输出信息在BIO中的偏移量,比如是屏幕BIO,则表示打印信息的位置离左边屏幕边缘的距离。

6)RSA_public_decrypt

RSA公钥解密。

7)RSA_public_encrypt

RSA公钥加密。

8)RSA_size

获取RSA密钥长度字节数。

9)RSAPrivateKey_dup

复制RSA私钥。

10)RSAPublicKey_dup

复制RSA公钥。

原文地址:https://www.cnblogs.com/mutou3221/p/3132034.html