首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Crypto++解码ASN.1

使用Crypto++解码ASN.1
EN

Stack Overflow用户
提问于 2020-06-16 23:30:08
回答 1查看 217关注 0票数 0

我尝试将ASN.1序列"AgER“转换为CryptoPP::Integer

代码语言:javascript
复制
#include <crypto++/asn.h>
#include <iostream>

int main(int, char*[])
{
    std::string base64{"AgER"};

    CryptoPP::StringSource s{base64, true};
    CryptoPP::BERSequenceDecoder d{s};
    CryptoPP::Integer i;
    i.BERDecode(d);

    std::cout << i.ConvertToLong() << std::endl;
}

这抛出了一个带有消息"BER decode error“的CryptoPP::BERDecodeErr类型的异常。

不同的ASN.1工具可以毫无问题地解析字符串:https://lapo.it/asn1js/#AgER

EN

回答 1

Stack Overflow用户

发布于 2020-06-17 17:22:58

我发现Crypto++需要二进制数据,而不是Base64编码的数据。所以我之前必须破解这个。

这里是工作解决方案;

代码语言:javascript
复制
#include <crypto++/asn.h>
#include <crypto++/base64.h>
#include <iostream>

int main(int, char*[])
{
    std::string base64{"AgER"};

    std::string decoded;
    CryptoPP::StringSource{base64, true, new CryptoPP::Base64Decoder{new CryptoPP::StringSink{decoded}}};

    CryptoPP::StringSource s{decoded, true};
    CryptoPP::Integer i;
    i.BERDecode(s);

    std::cout << i.ConvertToLong() << std::endl;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62412128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档