Openssl X509证书从HexStream中解析

整体思路

hex 转换成字节流

然后从字节流中进行解析

You have access to the raw certificate in memory.

In the case that you have access to the raw encoding of the certificate in memory, you can parse it as follows. This is useful if you have stored raw certificates in a database or similar data store.

#include 
#include 
#include 

const unsigned char *data = ... ;
size_t len = ... ;

X509 *cert = d2i_X509(NULL, &data, len);
if (!cert) {
	fprintf(stderr, "unable to parse certificate in memory\n");
	return EXIT_FAILURE;
}

// any additional processing would go here..

X509_free(cert);

std::string hex_str = "30820317308201ffa003020102021432e1f.....";

// hex -> binaryData
std::vector<uint8_t> binaryData;
for (size_t i = 0; i < hex_str.size(); i += 2) {
    unsigned int byte;
    sscanf(hex_str.substr(i, 2).c_str(), "%02X", &byte);
    binaryData.push_back(byte);
}
 // 将 char* 转换为 unsigned char*
const unsigned char* uchar_ptr = binaryData.data();

size_t len = binaryData.size();
X509 *cert  = d2i_X509(NULL,&uchar_ptr,len);

if (!cert) {
    printf("Failed to load certificate from bytes\n");
    return 1;
}

// 打印证书信息
X509_print_fp(stdout, cert);

// 释放资源
X509_free(cert);

使用CryptoPP 解析

使用 CryptoPP 库进行解析

   std::string hex_str = "30820317308201ffa003020102021.....";

    std::string binaryData;
    CryptoPP::StringSource(hex_str, true,
                 new CryptoPP::HexDecoder(
                         new CryptoPP::StringSink(binaryData)
                 )
    );


    CryptoPP::ByteQueue googleq, thawteq, googletbs, thawtespki;
    CryptoPP::SecByteBlock certSignature;

    googleq.Put((const CryptoPP::byte*)binaryData.data(), binaryData.size());

    CryptoPP::X509Certificate cert;

    cert.Load(googleq);

    // 获取公钥
    const CryptoPP::PublicKey& publicKey = cert.GetSubjectPublicKey();

    // 如果你确定证书的类型是 RSA,你可以将 PublicKey 强制转换为 RSA 公钥
    const CryptoPP::RSA::PublicKey& rsaPublicKey = dynamic_cast<const RSA::PublicKey&>(publicKey);
    // 获取 RSA 模数
    const CryptoPP::Integer& modulus = rsaPublicKey.GetModulus();


// 打印公钥信息
    std::cout << "Public Key: " <<rsaPublicKey.GetPublicExponent()<< std::endl;
    // 打印 RSA 模数
    std::cout << "RSA Modulus: " << modulus<< std::endl;

相关推荐

  1. Openssl X509证书HexStream

    2024-05-10 20:26:06       11 阅读
  2. 关于https证书06 OBJECT_IDENTIFIER标识的

    2024-05-10 20:26:06       33 阅读
  3. openssl生成ssl证书

    2024-05-10 20:26:06       40 阅读
  4. openssl生成免费证书

    2024-05-10 20:26:06       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-10 20:26:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-10 20:26:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-10 20:26:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-10 20:26:06       18 阅读

热门阅读

  1. node.js中 cluster 模块和 worker_threads 模块

    2024-05-10 20:26:06       8 阅读
  2. Git的常见面试题

    2024-05-10 20:26:06       9 阅读
  3. FastDDS编译安装说明

    2024-05-10 20:26:06       8 阅读
  4. Docker

    Docker

    2024-05-10 20:26:06      9 阅读
  5. 使用Python绘制爱心

    2024-05-10 20:26:06       7 阅读
  6. ArrayList数组去重

    2024-05-10 20:26:06       6 阅读
  7. 5.9代码

    5.9代码

    2024-05-10 20:26:06      9 阅读
  8. eslint关闭的方法总结

    2024-05-10 20:26:06       6 阅读
  9. Spark安装教程

    2024-05-10 20:26:06       8 阅读