#include "stdio.h"
#include "iostream.h"
#include "string"
#include "math.h"
using namespace std;
enum classification
{
universal,
application,
context_specific,
privateclass
};
enum constructed
{
simplecon,
constructed
};
//程序中未使用
enum type
{
boolean=1 ,//[有两个值:false或true]
interger, //[整型值]
BitString, //[0位或多位]
OctetString,//[0字节或多字节]
null,
ObejectIdentifier, //[相应于一个对象的独特标识数字]
ObjectDescriptor, //[一个对象的简称]
External,// [ASN.1没有定义的数据类型]
Real,//[实数值]
Enumerated,//[数值列表,这些数据每个都有独特的标识符,作为ASN.1定义数据类型的一部分]
UTF8String,
Relative_oid,
Sequence,//[有序数列,SEQUENCE里面的每个数值都可以是不同类型的,而SEQUENCE OF里是0个或多个类型相同的数据]
Set, // [无序数列,SET里面的每个数值都可以是不同类型的,而SET OF里是0个或多个类型相同的数据]
NumericString,//[0-9以及空格]
PrintableString,//[A-Z、a-z、0-9、空格以及符号'()+,-./:=?]
TeletexString,//T61String
VideotexString,
IA5String,
UTCTime, //[统一全球时间格式]
GeneralizedTime,
GraphicString,
VisibleString, //ISO646String
GeneralString,
UniversalString,
CharaterString,
BMPString,
reserved ,
version,
issuerUniqueID,
subjectUniqueID,
external
};
int gettype(FILE* cfile )
{
//取byte第8,7位数值判断 classification
unsigned char byte =fgetc(cfile);
int theclass=0,thetype=0;
theclass=byte>>6;
thetype=(byte&31);
if(context_specific==theclass)
{
return 32+thetype;
}
else
{
return thetype;
}
}
bool IsMulti(unsigned char byte)
{
return (bool)(byte>>7);
}
int getSignlength(unsigned char byte)
{
return byte&127;
}
int ConvertByteToNum(unsigned char* pByte,int len)
{
int i=0, tmp, result=0;
for(i=0;i<len;i++)
{
tmp=pByte[len-i-1]*pow(256,i);
result+=tmp;
}
return result;
}
int getLength(FILE* cfile)
{
unsigned char byte =fgetc(cfile);
int len=getSignlength(byte);
if(IsMulti(byte))
{
unsigned char* pByte=new unsigned char[len];
fread(pByte,1,len,cfile);
len=ConvertByteToNum(pByte,len);
delete pByte;
return len;
}
else
{
return len;
}
}
void getTypeandLen(FILE* cfile,int& thetype,int& len)
{
thetype=gettype(cfile);
len=getLength(cfile);
}
void getversion(int ver)
{
if(0x00==ver)
{cout<<"证书版本为:v1"<<endl;}
else if(0x01==ver)
{cout<<"证书版本为:v2"<<endl;}
else if(0x02==ver)
{cout<<"证书版本为:v3"<<endl;}
}
#define MAX_LEN 10000
#define MAX_PATH 256
void main()
{
FILE* cfile;
char strpath[MAX_PATH];
cin>>strpath;
cfile=fopen(strpath,"rb+");
if(cfile==NULL)
{
cout<<"open cert fail"<<endl;
return;
}
int thetype=0,tmpint=-1,len=0;
int namelen=0;
unsigned char* pByte=NULL;
//获取证书长度字节
getTypeandLen(cfile,thetype,len);
cout<<"证书长度为:"<<len<<"字节"<<endl;
//获取证书主体长度字节
getTypeandLen(cfile,thetype,len);
cout<<"证书主体长度为:"<<len<<"字节"<<endl;
//获取版本号
getTypeandLen(cfile,thetype,len);
if(32==thetype)
{
cout<<"证书版本长度为:"<<len<<"字节"<<endl;
getTypeandLen(cfile,thetype,len);
pByte=new unsigned char[len];
fread(pByte,1,len,cfile);
tmpint=ConvertByteToNum(pByte,len);
getversion(tmpint);
delete[] pByte;
}
//获取版本序列号
getTypeandLen(cfile,thetype,len);
pByte=new unsigned char[len];
fread(pByte,1,len,cfile);
tmpint=ConvertByteToNum(pByte,len);
delete[] pByte;
cout<<"证书序列号为"<<tmpint<<endl;
//跳过算法签名
getTypeandLen(cfile,thetype,len);
namelen=len; //保留算法签名
fseek(cfile,len,SEEK_CUR);
//跳过 issuer name
getTypeandLen(cfile,thetype,len);
fseek(cfile,len,SEEK_CUR);
//跳过 证书有效期
getTypeandLen(cfile,thetype,len);
fseek(cfile,len,SEEK_CUR);
//跳过 证书主体名称
getTypeandLen(cfile,thetype,len);
fseek(cfile,len,SEEK_CUR);
//获取公钥
getTypeandLen(cfile,thetype,len);
fseek(cfile,namelen+2,SEEK_CUR);
getTypeandLen(cfile,thetype,len);
pByte=new unsigned char[len];
fread(pByte,1,len,cfile);
fclose(cfile);
for(int i=0;i<len-1;i++)
{
unsigned char tmpchar=pByte[i];
printf("ox%X ",tmpchar);
}
cout<<endl;
delete[] pByte;
}
评论22
最新资源