C 言語の実装: RSA PEM ファイルから秘密キー n/e/d/p/q/dp/dq/qp 要素を抽出します

openssl コマンド ライン: n/e/d/p/q/dp/dq/qp を使用してopenssl rsa -in test_priv.pem -text、秘密キー要素を秘密キー PEM ファイルから抽出できることがわかっています。

では、それをC言語で実装するにはどうすればよいでしょうか? コードで実装するにはどうすればよいでしょうか?

#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

int main() {
    
    
    const char *filename = "test_priv.pem"; // 替换为你的PEM文件路径

    // 打开PEM文件
    FILE *file = fopen(filename, "r");
    if (!file) {
    
    
        perror("Error opening file");
        return 1;
    }

    // 从PEM文件中读取RSA私钥
    RSA *rsa_private_key = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
    fclose(file);

    if (!rsa_private_key) {
    
    
        ERR_print_errors_fp(stderr);
        return 1;
    }

    // 输出私钥信息
    printf("RSA Private Key:\n");
    RSA_print_fp(stdout, rsa_private_key, 0);

    // 释放私钥内存
    RSA_free(rsa_private_key);

    return 0;
}

コンパイルコマンド:gcc main.c -o x -lssl -lcrypto

ここに画像の説明を挿入

おすすめ

転載: blog.csdn.net/weixin_42135087/article/details/132465802