当前位置:   article > 正文

openssl 对自定义结构体 按ASN.1的der编码规则进行编码_openssl sm2加密结果转换asn1

openssl sm2加密结果转换asn1

        数据序列化,为将高级语言代码转变成二进制流的过程称为数据的序列号,数据序列化的目的是物理存储或网络传输。在计算机高级程序编程语言里有许多结构化的结构,比如:数、图和结构体等,如何在网络中传输这些结构化的结构数据呢?我们知道高级语言里面的任何代码最终在网络中都是以二进制流的形式进行传输的,但是对于结构化的数据除了编成二进制码外,他们之间还存有一定的关系,为了在对方仍然知道这些关系以用来还原之前的结构,因此除了对结构化的数据进行编码外还得需要保存好他们的结构关系。

为了解决高级语言中结构化数据在网络传输中的结构关系能送达目的地进行还原,历史上出现了以下几种数据序列化的方法:ASN.1,XML,Json等(可参考:在维基百科中搜索数据序列号格式)。

        上面我们分析了ASN.1的作用(或者说为什么要引入ASN.1),下面看看ASN.1是什么东西?

        ASN.1本身只定义了表示信息的抽象句法,但是没有限定其编码的方法。各种ASN.1编码规则提供了由ASN.1描述其抽象句法的数据的值的传送语法(具体表达)。标准的ASN.1编码规则有基本编码规则(BER,Basic Encoding Rules)、规范编码规则(CER,Canonical Encoding Rules)、唯一编码规则(DER,Distinguished Encoding Rules)、压缩编码规则(PER,Packed Encoding Rules)和XML编码规则(XER,XML Encoding Rules)。为了使ASN.1能够描述一些原先没有使用ASN.1定义,因此不适用上述任一编码规则的数据传输和表示的应用和协议,另外制订了ECN来扩展ASN.1的编码形式。ECN可以提供非常灵活的表明方法,但还没有得到普遍应用。

        ASN.1与特定的ASN.1编码规则一起通过使用独立于计算机架构和编程语言的方法来描述数据结构,为结构化数据的交互提供了手段,特别是在网络环境的应用程序。

        openssl中SM2加密产生的密文为der编码的密文,因此解密接口需要的密文也是der编码的密文。而别的设备或者库产生的密文可能只是裸密文。因此要将裸密文进行der编码。
        以下为将一个自定义结构体进行der编码示例代码

  1. #include <openssl/bn.h>
  2. #include <openssl/ossl_typ.h>
  3. #include <openssl/ec.h>
  4. #include <openssl/opensslconf.h>
  5. #include <iostream>
  6. #include <sm4decryption.h>
  7. int i2dSm2EnStr(char* enStr,int enStrLen,unsigned char* ASNEnStr);
  8. //注册i2d结构体与转化函数
  9. typedef struct SM2_Ciphertext_st SM2_Ciphertext;
  10. DECLARE_ASN1_FUNCTIONS(SM2_Ciphertext)
  11. struct SM2_Ciphertext_st {
  12. BIGNUM *C1x;
  13. BIGNUM *C1y;
  14. ASN1_OCTET_STRING *C3;
  15. ASN1_OCTET_STRING *C2;
  16. };
  17. ASN1_SEQUENCE(SM2_Ciphertext) = {
  18. ASN1_SIMPLE(SM2_Ciphertext, C1x, BIGNUM),
  19. ASN1_SIMPLE(SM2_Ciphertext, C1y, BIGNUM),
  20. ASN1_SIMPLE(SM2_Ciphertext, C3, ASN1_OCTET_STRING),
  21. ASN1_SIMPLE(SM2_Ciphertext, C2, ASN1_OCTET_STRING),
  22. } ASN1_SEQUENCE_END(SM2_Ciphertext)
  23. IMPLEMENT_ASN1_FUNCTIONS(SM2_Ciphertext)
  24. //sm2密文格式问C1x(32字节)+c1y(32字节)+c3(32字节hash杂凑值)+c2(密文,长度等于明文)
  25. int i2dSm2EnStr(char* enStr,int enStrLen,unsigned char* ASNEnStr)
  26. {
  27. SM2_Ciphertext* sm2En=NULL;
  28. sm2En=SM2_Ciphertext_new();
  29. int c2Len=enStrLen-96;
  30. unsigned char C1x[32];
  31. unsigned char C1y[32];
  32. unsigned char C3[32];
  33. unsigned char C2[512];
  34. memcpy(C1x,(unsigned char*)enStr,32);
  35. memcpy(C1y,(unsigned char*)enStr+32,32);
  36. memcpy(C3,(unsigned char*)enStr+64,32);
  37. memcpy(C2,(unsigned char*)enStr+96,c2Len);
  38. //print
  39. std::string resC1x;
  40. std::string resC1y;
  41. char buf2[3];
  42. for (int i = 0; i < 32; i++)
  43. {
  44. sprintf(buf2,"%02x",C1x[i]);
  45. resC1x+=buf2;
  46. }
  47. printf("\n");
  48. std::cout<<"resC1x:"<<resC1x;
  49. for (int i = 0; i < 32; i++)
  50. {
  51. sprintf(buf2,"%02x",C1y[i]);
  52. resC1y+=buf2;
  53. }
  54. printf("\n");
  55. std::cout<<"resC1y:"<<resC1y;
  56. BIGNUM* bC1x=BN_new();
  57. BIGNUM* bC1y=BN_new();
  58. BN_hex2bn(&bC1x,resC1x.c_str()))
  59. BN_hex2bn(&bC1y,resC1y.c_str()));
  60. sm2En->C1x=bC1x;
  61. sm2En->C1y=bC1y;
  62. if(!ASN1_OCTET_STRING_set(sm2En->C3,C3,32))
  63. {
  64. printf("C3 ASN1_OCTET_STRING_set fail");
  65. }
  66. if(!ASN1_OCTET_STRING_set(sm2En->C2,C2,c2Len))
  67. {
  68. printf("C2 ASN1_OCTET_STRING_set fail");
  69. }
  70. unsigned char * tem=ASNEnStr;
  71. int nLen=i2d_SM2_Ciphertext(sm2En,&tem);
  72. printf("asnLen:%d \n",nLen);
  73. for(int i=0;i<nLen;i++)
  74. {
  75. printf("0x%02x ",ASNEnStr[i]);
  76. }
  77. return nLen;
  78. }
  79. int main(int argc, char *argv[])
  80. {
  81. unsigned char enStr2[116]={0xa0,0x3c,0x0b,0x75,0x4e,0x67,0x43,0xdd,0x4a,0x0a,0xd9,0xbb,0xf9,0xb3,0x73,0x87,0x17,0xb0,0xf8,0x49,0xea,0x8c,0x90,0xec,0x20,0x41,0xe6,0x77,0xb5,0xe6,0xab,0xbd,0xb0,0x18,0xc6,0xb6,0x76,0x22,0xb1,0x04,0x50,0xe7,0x65,0xac,0x26,0xcc,0x4d,0x61,0xa4,0x4a,0xf2,0x4a,0xcb,0x5b,0xe5,0xd4,0x40,0xa2,0x7c,0x9c,0x5b,0x57,0x2c,0xcd,0x67,0x74,0x24,0xfa,0x94,0x91,0x15,0x12,0xa2,0x35,0xdc,0x1e,0xfd,0xbe,0xba,0x40,0xf5,0x62,0x76,0x2b,0x88,0xfb,0xed,0xc8,0xbb,0xae,0x01,0x26,0xe3,0xf3,0xfb,0x4e,0xf1,0x20,0xdd,0x79,0x37,0x1a,0x5d,0x50,0xa9,0x24,0x91,0xe4,0x7a,0x26,0x5f,0xe7,0x41,0x8f,0x0a,0x5a};
  82. printf("deStr:\n");
  83. for(int i=0;i<116;i++)
  84. {
  85. printf("0x%02x ",enStr2[i]);
  86. }
  87. printf("\n");
  88. unsigned char ASNenStr[512];
  89. int ASNStrLen=i2dSm2EnStr((char*)enStr2,116,ASNenStr);
  90. printf("ASNStrLen:%d \n",ASNStrLen);
  91. printf("ASNenStr:\n");
  92. for(int i=0;i<ASNStrLen;i++)
  93. {
  94. printf("0x%02x ",ASNenStr[i]);
  95. }
  96. printf("\n");
  97. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号