首頁  >  文章  >  後端開發  >  openssl rsa金鑰格式的問題,解決了php和c++協同開發的金鑰格式問題

openssl rsa金鑰格式的問題,解決了php和c++協同開發的金鑰格式問題

WBOY
WBOY原創
2016-08-08 09:24:222916瀏覽

OpenSSL程式設計-RSA程式設計詳解
 本文由大佟發表於2014年06月26日, 瀏覽: 1,954次, 評論: 0條

一. RSA PEM檔案格式
格式 
1.PEM1. ---BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
 
2. PEM公鑰格式檔案
 -----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
 
3. PEM RSAPublicKey公鑰格式檔案
 -----BEGIN RSA PUBLIC KEY-----
-----END RSA PUBLIC KEY- ----
 
二. OpenSSL金鑰相關指令
 
1. 產生金鑰
 openssl genrsa -out key.pem 1024
    -out 指定產生文件,此文件包含公鑰與公鑰兩鑰部分,因此可以私鑰加密,也可以解密
    1024 產生金鑰的長度
 
2. 提取PEM格式公鑰
 openssl rsa -in key.pem -pubout -out pubkey.pem
-   產生公鑰的檔案(PEM公鑰格式)
 
3. 提取PEM RSAPublicKey格式公鑰
 openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem
    -in 指定輸入的密鑰檔案指定輸入的密鑰產生公鑰的檔案(PEM RSAPublicKey格式)
 
4. 公鑰加密檔案
 openssl rsautl -encrypt -in input.file -inkey pubkey.pem -pubin -out output.file
-   inkey 指定加密公鑰檔案
    -pubin 表面是使用純公鑰檔案加密
    -out 指定加密後的檔案
 
5. 私鑰解密檔案
 openssl rsautl -decrypt -inkey output.file
    -in 指定要解密的檔案
    -inkey 指定私密金鑰檔案
    -out 指定解密後的檔案
 
三.RSA           // public modulus
    BIGNUM *e;              // public exponent
    BIGNUM *d;                // secret prime factor
    BIGNUM *q;             // d mod ( p-1)
    BIGNUM *dmq1;           // d mod (q-1)
    BIGNUM *iqmp     
 
2. BN大數係列函數
 //新產生一個BIGNUM結構
BIGNUM *BN_new(void);

//釋放一個BIGNUM結構,釋放完後a=NULL;
void BN_free(BIGNUM *a);

/初始化所有項為0,一般為BN_ init(&c)
void BN_init(BIGNUM *);

//將a中所有項目均賦值為0,但是內存並沒有釋放

void BN_clear(BIGNUM *a);


//相當與將BN_free和BN_clear綜合,要不就賦值0,要不就釋放空間。
void BN_clear_free(BIGNUM *a);

//設定大數a為整數w

int BN_set_word(BIGNUM *a, unsigned long w);

//假如大數a long型數

unsigned long BN_get_word(BIGNUM *a);

//產生一個加密用的強bits的偽隨機數

//若top=-1,最高位元為0,top=0,最高位元為1 ,top=1,最高位元與次高位元為1,bottom為真,隨機數為偶數
int BN_rand(BIGNUM *rnd, int bits, int top, int bottom);

//將a 轉換為字串入to,to的空間必須大於BN_num_bytes(a)
int BN_bn2bin(const BIGNUM *a, unsigned char *to);

//將s中的len位的正整數轉換為大數
BIGNUM *BN_bin2bn(const_bin2bn(const_bin unsigned char *s, int len, BIGNUM *ret);

//將大數轉換為16進位字串
char *BN_bn2hex(const BIGNUM *a);

 

//將大數轉換為10進位字串
char *BN_bn2dec(const BIGNUM *a);

//將16進位字串轉成大數

int BN_hex2bn(BIGNUM **a, const char *str);

//將10進位字串傳成大數

int BN_dec2bn(BIGNUM **a, const char *str);
 

3. RSA系列函數

 //初始化一個RSA結構
RSA * RSA_new(void);
 //初始化一個RSA結構
RSA * RSA_new(void);
 //初始化一個RSA結構

RSA結構

void RSA_free(RSA *rsa);

//RSA私鑰產生函數

//產生一個模為num位元的金鑰對,e為公開的加密指數,一般為65537(0x10001)
RSA *RSA_generate_key (int num, unsigned long e,void (*callback)(int,int,void *), void *cb_arg);

//判斷位數函數, 傳回RSA模的位數
int RSA_size(const RSA *rsa);

//測試p、q是否為素數
int RSA_check_key(RSA *rsa);
 
4. PEM系列函數
 //從檔案載入RSAPublicKey格式公鑰憑證
RSA *PEM_read_RSAPublicKey(FILE *fp, RSA **x, pem_password_cb *cb, void *u);

//從BIO重載入RSAPublicKeyKey RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x, pem_password_cb *cb, void *u);

//輸出RSAPublicKeyKey實體憑證到檔案

int PEM_write_RSAPublicKey(FILE *fp, 公鑰憑證到檔案
int PEM_write_RSAPublicKey(FILE *fp, RSAx);輸出RSAPublicKey公鑰憑證到BIO

int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);

 
5. RSA加密API
 int RSA_public_encrypt(int flen, unsigned char *from, signed charf * );

參數說明:

    flen: 要加密訊息長度

    from: 要加密訊息
    to: 加密後的訊息
    padding: 所採取的加密方案ING, RSA_NO_PADDING
 
6. RSA解密API
 int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to, RSA *rsa, int padding);

參數說明:

 解密後的資訊

    padding: 所採取的解密方案
 
四.RSA程式設計範例
 
1. 資料加、密解密範例
 #include
#include
#include
#include
#include

#define PRIKEY "prikey.pem"
#define PUBKEY "pub.pem "
#define BUFFSIZE 4096

/**************************************************** ********************

 * RSA加密解密函數
 *
 * file: test_rsa_encdec.c

 * gcc -Wall -O2 -o test_rsa_encdec test_rsa_encdec.c -lcrypto -lssl

 *
 * author: tonglulin@gmail.com by www.qmailer.net
 ******************************************** ****************************/

char *my_encrypt(char *str, char *pubkey_path)
{
    RSArsa = NULL; = NULL;
    int len = 0;

    int rsa_len = 0;


    if ((fp = fopen(pubkey_path, "r 
    /* 讀取公鑰PEM,PUBKEY格式PEM使用PEM_read_RSA_PUBKEY函數*/
    if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) == NULL) {  _RSA fp(stdout, rsa, 0);

    len = strlen(str);
    rsa_len = RSA_size(rsa);

    en = (char *)malloc(rsa_len + 1);5); (RSA_public_encrypt(rsa_len , (unsigned char *)str, (unsigned char*)en, rsa, RSA_NO_PADDING)         return NULL;
    }    return en;

}


char *my_decrypt(char *str, char *prikey_path)
{
    RSA *rsa = NULL;

    FILE *fp = NULL; if ((fp = fopen (prikey_path, "r")) == NULL) {

        return NULL;

    }


    if ((rsa = PEM_read_RSAPrivateKey ULL;

    }


RSA_print_fp(stdout, rsa, 0);

    rsa_len = RSA_size(rsa);
    de = (char *)malloc(rsa_len + 1);
. private_decrypt( rsa_len, (unsigned char *)str, (unsigned char*)de, rsa, RSA_NO_PADDING)         return NULL;

    }
🜠🠎);

    return de;

}

int main(int argc, char *argv[])

{
    char *src = "hello, world!";
    char *en = NULL %sn", src);

    en = my_encrypt(src, PUBKEY);
    printf("enc is: %sn", en);

    de= my_decrypt(en, PRIKEY);
    printf("dec is: %sn", de);

    if (en != NULL) {
        free(en);
    }

   
    return 0;
}

 

2. PEM/BIGNUM 頭部轉換範例
#include
#include

#include
#include


#include

#&*/


const char *n = "C7301B330C4E123E4FA9F54F49121E8CE07974D8BFEF1D39EC9245D573D66E7FAC258F86E2B029245D573D66E7FAC258F86E2B029245D573D66E7FAC258F86E2B0FD E253ED5A4A0FBAD50D68E91D0459F9F2377BB8CA1583E3F83C06343A5A1177C903F498A6D14015CC975522BE44466871383866F 63BE3";

const char *pubkey = "-----開始RSA-----nMIGJAoGBAMcwGzMMThI+T6n1T0kSHozgeXTYv+8dOeySRdVz1m5/rCWPhuKwgWxrnrn +61Q1o6R0EWfnyN3u4yhWD4/g8BjQ6WhF3yQnP0mKbRQBXMl1UivkRGzR64fojvBahjrw3XxNQTz2A+30iT7sBjvjAgAEAE=n-----END RSA 燈泡-----";

int main(int argc, char *argv[])
{
    RSA    *rsa = NULL;

    BIO    *bio = NULL;

    BIGNUM *bne = NULL;

    BIGNUM * bnn = NULL;

    FILE *fp = NULL;

    unsigned long e = 65537;


    if (argc          return -1 ;
}

    /* 將PEM轉換成大數字字串*/
    if (strcasecmp(argv[1], "bignum") == 0) {
     文件讀取*/

            fp = fopen(argv[2], "r");

           
            }

            rsa = P EM_read_RSAPublicKey(fp, &rsa, NULL, NULL); 如果(rsa == NULL) {

                return -1;

                      /* 讀取記憶體資料*/
            bio_new(BIO_new(BIO_) s_mem());
rsa = PEM_read_bio_RSAPublicKey(bio, &rsa, NULL, NULL);
           if (rsa == NULL) {      }
        }

       RSA_print_ fp(stdout, rsa, 0); ", BN_bn2hex (rsa->n));
        printf("%sn", BN_bn2hex(rsa->e));

        if (argc == 3) {
            fclose(fp);
         BIO_free(bio);
        }
        RSA_free(rsa);
   
    else if (strcasecmp(argv[1], "pem") == 0) {

        bne = BN_new();    return -1;
        }

        bnn = BN_new( );
        if (bnn == NULL) {
            BN_free(bne);    
        rsa = RSA_new();

        if (rsa == NULL) {

       bne);
            return -1;
        }

          /* 設定模數*/

        BN_set_word(bne, e);

        if (argc == 3) {
            BN_hex2bn(&bnn, argv[2]);
        } 2bn(&bnn, n);
        }

        PEM_write_RSAPublicKey(stdout, rsa); se {

        return -1;

    }

    return 0;

}
 
3. 密鑰產生範例
.>clude.Sclude.
#include
#include
#include

/**************************************************** ********************

 * RSA金鑰產生函數

 *

 * file: test_rsa_genkey.c
 * gcc -Wall -O2 -o test_rsa_genkey test_rsa_genkey.c - lcrypto
 *
 * author: tonglulin@gmail.com by www.qmailer.net
 ******************************************** ****************************/

int main(int argc, char *argv[])

{
    /* 產生RSA密鑰*/
    RSA *rsa = RSA_generate_key(1024, 65537, NULL, NULL);

    printf("BULL, NULL);

    printf("BULL, NULL);

  /*提取私鑰*/
    printf("PRIKEY:n");

    PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0, NULL, NULL);

char *)calloc(RSA_size(rsa), sizeof(unsigned char));
    unsigned char *e_b = (unsigned char *)calloc(RSA_size(rsa), sizeof(unsigned char)); ->n, n_b);
    int b_size = BN_bn2bin(rsa->e, e_b);

    RSA *pubrsa = RSA_new(nŠ); pubrsa-> e = BN_bin2bn(e_b, b_size, NULL);

    printf("PUBKEY: n");
    PEM_write_RSA RSA); ;

    return 0;
}

以上就介紹了openssl rsa密鑰格式的問題,解決了php和c++協同開發的密鑰格式問題,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:CentOS65搭建LNMP下一篇:CentOS65搭建LNMP