• openssl C语言编码实现rsa加密


     非原创, 引用自:
    1
    CC=gcc 2 CPPFLAGS= -I /home/yyx/02/openssl-1.0.1t/include/ 3 CFLAGS=-Wall -g 4 LIBPATH = -L /usr/lib 5 LIBS= -lssl -lcrypto -lhiredis -lm 6 7 8 #找到当前目录下所有的.c文件 9 src = $(wildcard ./src/*.c) 10 11 #将当前目录下所有的.c 转换成.o给obj 12 obj = $(patsubst %.c, %.o, $(src)) 13 14 rsa = test_rsa 15 16 target = $(rsa) 17 18 ALL:$(target) 19 20 #生成所有的.o文件 21 $(obj):%.o:%.c 22 $(CC) -c $< -o $@ $(CPPFLAGS) $(LIBPATH) $(LIBS) $(CFLAGS) 23 24 #test_rsa程序 25 $(rsa):./src/test.o 26 $(CC) $^ -o $@ $(CPPFLAGS) $(LIBPATH) $(LIBS) $(CFLAGS) 27 28 29 #clean指令 30 31 clean: 32 -rm -rf $(obj) $(target) ./test/*.o 33 34 #将clean目标 改成一个虚拟符号
    35 .PHONY: clean ALL

    1.上述makefile; 用来下面编译的 加密程序。

    2.首先介绍下命令台下openssl工具的简单使用:

      1)生成一个密钥:

      openssl genrsa -out test.key 1024

      这里-out指定生成文件的。需要注意的是这个文件包含了公钥和密钥两部分,也就是说这个文件即可用来加密也可以用来解密。后面的1024是生成密钥的长度。

      2)openssl可以将这个文件中的公钥提取出来:

      openssl rsa -in test.key -pubout -out test_pub.key

      -in指定输入文件,-out指定提取生成公钥的文件名。至此,我们手上就有了一个公钥,一个私钥(包含公钥)。现在可以将用公钥来加密文件了。

      

      3)在目录中创建一个hello的文本文件,然后利用此前生成的公钥加密文件

      openssl rsautl -encrypt -in hello -inkey test_pub.key -pubin -out hello.en

       -in指定要加密的文件,-inkey指定密钥,-pubin表明是用纯公钥文件加密,-out为加密后的文件。

      4)解密文件:

      openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de

      -in指定被加密的文件,-inkey指定私钥文件,-out为解密后的文件。

       至此,一次加密解密的过程告终。

    3. 采用 API 进行加密

      1 //  RSA 加密 ///
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 #include <errno.h>
      7 #include <openssl/rsa.h>
      8 #include <openssl/pem.h>
      9 #include <openssl/err.h>
     10 
     11 
     12 
     13 #define OPENSSLKEY "test.key"
     14 #define PUBLICKEY  "test_pub.key"
     15 #define BUFFSIZE   1024
     16 
     17 char *my_encrypt(char *str, char *path_key);    //加密
     18 char *my_decrypt(char *str, char *path_key);        //解密
     19 
     20 int main(void)
     21 {
     22     char *source = "i like dancing !!!";
     23     
     24     char *ptf_en, *ptf_de;
     25     
     26     printf("source is   :%s
    ", source);
     27     
     28     //1.加密
     29     ptf_en = my_encrypt(source, PUBLICKEY);
     30     printf("ptf_en is   :%s
    ", ptf_en);
     31     
     32     //2.解密
     33     ptf_de = my_decrypt(ptf_en, OPENSSLKEY);
     34     printf("ptf_de is   :%s
    ", ptf_de);
     35     
     36     if(ptf_en)            free(ptf_en);
     37     if(ptf_de)            free(ptf_de);
     38         
     39     return 0;
     40     
     41 }
     42 
     43 //加密
     44 char *my_encrypt(char *str, char *path_key)
     45 {
     46     char *p_en = NULL;
     47     RSA  *p_rsa = NULL;
     48     FILE *file = NULL;
     49     
     50     int  rsa_len = 0;    //flen为源文件长度, rsa_len为秘钥长度
     51     
     52     //1.打开秘钥文件
     53     if((file = fopen(path_key, "rb")) == NULL)
     54     {
     55         perror("fopen() error 111111111 ");
     56         goto End;
     57     }        
     58     
     59     //2.从公钥中获取 加密的秘钥
     60     if((p_rsa = PEM_read_RSA_PUBKEY(file, NULL,NULL,NULL )) == NULL)
     61     {
     62         ERR_print_errors_fp(stdout);
     63         goto End;
     64     }
     65     
     66     //3.获取秘钥的长度
     67     rsa_len = RSA_size(p_rsa);
     68     
     69     //4.为加密后的内容 申请空间(根据秘钥的长度+1)
     70     p_en = (char *)malloc(rsa_len + 1);
     71     if(!p_en)
     72     {
     73         perror("malloc() error 2222222222");
     74         goto End;
     75     }    
     76     memset(p_en, 0, rsa_len + 1);
     77     
     78     //5.对内容进行加密
     79     if(RSA_public_encrypt(rsa_len, (unsigned char*)str, (unsigned char*)p_en, p_rsa, RSA_NO_PADDING) < 0)
     80     {
     81         perror("RSA_public_encrypt() error 2222222222");
     82         goto End;
     83     }
     84 
     85 End:
     86     
     87     //6.释放秘钥空间, 关闭文件
     88     if(p_rsa)    RSA_free(p_rsa);
     89     if(file)     fclose(file);
     90         
     91     return p_en;
     92 }   
     93 
     94 //解密
     95 char *my_decrypt(char *str, char *path_key)
     96 {
     97     char *p_de = NULL;
     98     RSA  *p_rsa = NULL;
     99     FILE *file = NULL;
    100     int   rsa_len = 0;
    101     
    102     
    103     //1.打开秘钥文件
    104     file = fopen(path_key, "rb");
    105     if(!file)
    106     {
    107         perror("fopen() error 22222222222");
    108         goto End;
    109     }        
    110     
    111     //2.从私钥中获取 解密的秘钥
    112     if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL)
    113     {
    114         ERR_print_errors_fp(stdout);
    115         goto End;
    116     }
    117     
    118     //3.获取秘钥的长度,
    119     rsa_len = RSA_size(p_rsa);
    120     
    121     //4.为加密后的内容 申请空间(根据秘钥的长度+1)
    122     p_de = (char *)malloc(rsa_len + 1);
    123     if(!p_de)
    124     {
    125         perror("malloc() error ");
    126         goto End;
    127     }    
    128     memset(p_de, 0, rsa_len + 1);
    129     
    130     //5.对内容进行加密
    131     if(RSA_private_decrypt(rsa_len, (unsigned char*)str, (unsigned char*)p_de, p_rsa, RSA_NO_PADDING) < 0)
    132     {
    133         perror("RSA_public_encrypt() error ");
    134         goto End;
    135     }
    136             
    137 End:
    138     //6.释放秘钥空间, 关闭文件
    139     if(p_rsa)    RSA_free(p_rsa);
    140     if(file)     fclose(file);
    141         
    142     return p_de;
    143 }    
  • 相关阅读:
    Nginx + FastCGI 程序(C/C++)搭建高性能web service的demo
    微服务架构
    异常点/离群点检测算法——LOF
    多边形区域填充算法--递归种子填充算法
    Java 关于容器集合等数据结构详情图解,一目了然
    平衡小车项目解读日志
    &lt;LeetCode OJ&gt; 101. Symmetric Tree
    【JVM】模板解释器--字节码的resolve过程
    hexo博客的相关配置
    【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
  • 原文地址:https://www.cnblogs.com/yyx1-1/p/6114858.html
Copyright © 2020-2023  润新知