- 论坛徽章:
- 0
|
我写的代码
//---------------------------------------------------------------------------
#include <vcl.h>
#include <iostream>
#include <openssl/evp.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
#include <vcl.h>
#include <stdio.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
using namespace std;
int main(int argc, char* argv[])
{
X509 *x = 0;
int id,len,ret;
FILE *fp,*fp2;
ASN1_BIT_STRING *dd;
unsigned char buf[50000],*p;
char buffer[72];
char out[5000];
int outl;
EVP_ENCODE_CTX ectx;
EVP_EncodeInit(&ectx);
fp=fopen("key.cer","rb");
len=fread(buf,1,50000,fp);
fclose(fp);
fp2 = fopen("aa.dat","wb");
p=buf;
d2i_X509(&x,(const unsigned char **)&p,len);
X509_NAME *subject = X509_get_subject_name(x);
int pos= String(subject-> bytes-> data).Pos("370");
String strSubJect = String(subject-> bytes-> data).SubString(pos,15);
cout < <"SubJect:" < <strSubJect.c_str() < <endl; //打印subject
ASN1_BIT_STRING *pubkey = X509_get0_pubkey_bitstr(x); //得到pubkey
sprintf(buffer,pubkey-> data); //pubkey-> data 放到buffer里
EVP_EncodeUpdate(&ectx,out,&outl,pubkey-> data,pubkey-> length); //base64加密 pubkey-> data;
cout < <"PublicKeyLength:" < <pubkey-> length < <endl; //长度72
cout < <"PublicKey:" < <buffer < <endl;
X509_free(x);
char c = getchar();
return 0;
}
|
打印的结果
SubJect:370212753771600
PublicKeyLength:72
PublicKey:0F!
//---------------------------------------------------------------------------
原始的key.cer内容
openssl x509 -inform der -in key.cer -text -pubkey
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
32:30:30:35:31:32:31:36:31:30:31:37:35:31:40:41:42:43
Signature Algorithm: md5WithRSAEncryption
Issuer: CN=137020000
Validity
Not Before: Dec 16 10:17:51 2005 GMT
Not After : Dec 31 23:59:59 2009 GMT
Subject: CN=370212753771600
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
RSA Public Key: (253 bit)
Modulus (253 bit):
16:1b:cc:a7:13:48:65:5d:a5:47:34:2e:63:c8:95:
b0:a2:c6:81:76:b5:9e:d7:15:d9:0e:e5:ec:d1:42:
37:e1
Exponent:
08:d7:eb:76:07:b6:8e:f2:42:1c:7b:45:c1:83:6f:
13:38:1f:f6:75:97:88:a6:d8:98:7e:8f:61:8b:39:
7c:d5
Signature Algorithm: md5WithRSAEncryption
06:cb:0e:74:df:89:5b:9d:c0:1e:35:e0:b2:9e:80:18:b6:b9:
ae:32:b9:16:93:5f:4f:d6:7c:de:a6:83:45:9d
-----BEGIN PUBLIC KEY-----
MFgwDQYJKoZIhvcNAQEBBQADRwAwRAIgFhvMpxNIZV2lRzQuY8iVsKLGgXa1ntcV
2Q7l7NFCN+ECIAjX63YHto7yQhx7RcGDbxM4H/Z1l4im2Jh+j2GLOXzV
-----END PUBLIC KEY-----
-----BEGIN CERTIFICATE-----
MIIBCzCB1qADAgECAhIyMDA1MTIxNjEwMTc1MUBBQkMwDQYJKoZIhvcNAQEEBQAw
FDESMBAGA1UEAxMJMTM3MDIwMDAwMB4XDTA1MTIxNjEwMTc1MVoXDTA5MTIzMTIz
NTk1OVowGjEYMBYGA1UEAxMPMzcwMjEyNzUzNzcxNjAwMFowDQYJKoZIhvcNAQEB
BQADSQAwRgIhABYbzKcTSGVdpUc0LmPIlbCixoF2tZ7XFdkO5ezRQjfhAiEACNfr
dge2jvJCHHtFwYNvEzgf9nWXiKbYmH6PYYs5fNUwDQYJKoZIhvcNAQEEBQADIQAG
yw5034lbncAeNeCynoAYtrmuMrkWk19P1nzepoNFnQ==
-----END CERTIFICATE-----
//---------------------------------------------------------------------------
在windows里打开key.cer 看到的publickey内容
b0 a2 c6 81 76 b5 9e d7 15 d9 0e e5 ec d1 42 37 e1 02 21 00 08 d7 eb 76 07 b6 8e f2 42 1c 7b 45 c1 83 6f 13 38 1f f6 75 97 88 a6 d8 98 7e 8f 61 8b 39 7c d5
我想要在程序里得到的就是这个在windows里打开key.cer后看到的publickey内容,请问怎么操作? |
|