• libopenssl 实现私钥加密公钥解密


    在需要验证可信来源时,需要用到签名验签。因此,需要使用私钥加密,公钥解密,取得被加密的信息。这就会使用到私钥加密,公钥解密的场景了。

    参考:
    https://github.com/openssl/openssl/issues/20493
    https://blog.csdn.net/wq897387/article/details/114129820

    使用 openssl-1.1.1 版本,3.x版本API不一样。

    #include 
    #include 
    #include 
    #include "openssl/ssl.h"
    #include "openssl/pem.h"
    #include "openssl/rsa.h"
    #include "openssl/bio.h"
    
    #define ENC_PADDING_TYPE  RSA_PKCS1_PADDING
    
    int encrypt(uint8_t *data, int len, uint8_t *out)
    {
        int ret = 0;
        uint8_t buff[8192] = {0};
        RSA *rsa = NULL;
        BIO *bio = NULL;
        int len1 = 0;
    
        FILE *f = fopen("private.pem", "r");
        if (!f) {
            printf("Open private.pem error\n");
            return -1;
        }
    
        len1 = fread(buff, 1, sizeof(buff), f);
        fclose(f);
    
        bio = BIO_new_mem_buf(buff, len1);
        if (!bio) {
            printf("BIO_new_mem_buf error\n");
            return -1;
        }
    
        rsa = PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL);
        if (!rsa) {
            printf("PEM_read_bio_PrivateKey error\n");
            return -1;
        }
    
        ret = RSA_private_encrypt(len, data, out, rsa, ENC_PADDING_TYPE);
        if (!ret) {
            printf("RSA_private_encrypt error\n");
            RSA_free(rsa);
            BIO_free_all(bio);
            return -1;
        }
    
        RSA_free(rsa);
        BIO_free_all(bio);
        return 0;
    }
    
    int decrypt(uint8_t *data, uint8_t *out)
    {
        int ret = 0;
        uint8_t buff[8192] = {0};
        RSA *rsa = NULL;
        BIO *bio = NULL;
    
        FILE *f = fopen("public.pem", "r");
        if (!f) {
            printf("Open public.pem error\n");
            return -1;
        }
    
        fread(buff, 1, sizeof(buff), f);
        fclose(f);
    
        bio = BIO_new_mem_buf(buff, -1);
        if (!bio) {
            printf("BIO_new_mem_buf error\n");
            return -1;
        }
    
        rsa = PEM_read_bio_RSA_PUBKEY(bio, &rsa, NULL, NULL);
        if (!rsa) {
            printf("PEM_read_bio_RSA_PUBKEY error\n");
            return -1;
        }
    
        ret = RSA_public_decrypt(256, data, out, rsa, ENC_PADDING_TYPE);
        if (!ret) {
            printf("RSA_public_decrypt error\n");
            RSA_free(rsa);
            BIO_free_all(bio);
            return -1;
        }
    
        RSA_free(rsa);
        BIO_free_all(bio);
        return 0;
    }
    
    int main(int argc, char **argv)
    {
        const char text[8192] = "Hello world11111111111111111";
        uint8_t enc[8192] = {0};
        uint8_t dec[8192] = {0};
    
        printf("Text: %s\n", text);
        encrypt(text, strlen(text), enc);
    
        decrypt(enc, dec);
        printf("Decrypt: %s\n", dec);
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    经测试,私钥加密,公钥解密,支持的 padding 方式只有 RSA_PKCS1_PADDINGRSA_X931_PADDING公钥加密,私钥解密,各 padding 方式都是支持的。

    运行结果:
    在这里插入图片描述

  • 相关阅读:
    Linux文件信息,drwxr-xr-x. 2 root root 6 Jan 30 17:42 Desktop
    51单片机DAC数模转换
    nvm管理多个node版本,快速来回切换node版本
    读写分离的利器——MySQL Proxy
    免安装免配置环境的免费 ios 调试工具 sib 来啦
    酷开科技以酷开系统的力量让电视机“活”起来
    Net Core 3.1 实现SqlSugar多库操作
    如今的入职背调到底有多刺激?
    react antd -Form表单 (单个对象合并成整体对象) 合并对象
    Happens-Before原则
  • 原文地址:https://blog.csdn.net/duapple/article/details/133150984