#include <openssl/asn1.h>



这个问题遇到过吧?  我想说很恶心, 又很好解决 ,下面是我的工程, 我把支付宝能用到的东西 放到了一个叫Tools的文件夹里面 , 然后add 添加到工程了


3.



$(PROJECT_DIR)/工程名/文件名



大功告成


在Linux C编程中,使用OpenSSL添加自定义ASN.1 OID并对数据进行编码和解码可以通过以下步骤实现: 1. 定义自定义OID:在OpenSSL中,可以通过OBJ_create函数定义自定义OID。例如,定义一个自定义OID为1.2.3.4.5,可以使用以下代码: ```c #include <openssl/obj_mac.h> #include <openssl/objects.h> #define MY_OID "1.2.3.4.5" int main() { int nid = OBJ_create(MY_OID, "myOID", "My OID"); if (nid == NID_undef) { printf("Error creating OID\n"); return 1; } printf("Custom OID created with NID=%d\n", nid); return 0; } ``` 2. 编码数据:使用ASN.1编码器对数据进行编码。在OpenSSL中,可以使用ASN1_item_pack函数实现。例如,编码一个字符串"Hello World",可以使用以下代码: ```c #include <openssl/asn1.h> int main() { ASN1_OCTET_STRING *str = ASN1_OCTET_STRING_new(); ASN1_OCTET_STRING_set(str, "Hello World", strlen("Hello World")); unsigned char *buf = NULL; int len = ASN1_item_pack(str, ASN1_ITEM_rptr(ASN1_OCTET_STRING), &buf); if (len < 0) { printf("Error encoding data\n"); return 1; } printf("Encoded data: "); for (int i = 0; i < len; i++) { printf("%02x ", buf[i]); } printf("\n"); OPENSSL_free(buf); ASN1_OCTET_STRING_free(str); return 0; } ``` 3. 解码数据:使用ASN.1解码器对数据进行解码。在OpenSSL中,可以使用ASN1_item_unpack函数实现。例如,解码上一步中编码的数据,可以使用以下代码: ```c #include <openssl/asn1.h> int main() { unsigned char buf[] = {0x04, 0x0b, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64}; ASN1_OCTET_STRING *str = ASN1_item_unpack(buf, sizeof(buf), ASN1_ITEM_rptr(ASN1_OCTET_STRING)); if (!str) { printf("Error decoding data\n"); return 1; } printf("Decoded data: %s\n", str->data); ASN1_OCTET_STRING_free(str); return 0; } ``` 在实际使用中,需要根据具体的需求进行调整和优化。同时,需要注意安全问题,防止因为错误或恶意数据导致的安全漏洞。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值