如何解码非密钥ASN1数据
How to decode non-key ASN1 data?
是否可以使用crypto++库来解码字节数组中的任意ASN1数据(具有多个序列和整数)。ash.h包含的方法都以BufferedTransformation作为输入,但该类是不同密码和哈希的接口,这似乎与我的简单情况根本无关。我还在cryptlib.h中找到了ASN1Object,但它是另一个接口,我还没有找到任何实现类。
我是不是觉得它对我自己来说太复杂了,还是真的很难解码任意ASN1数据?
事实上,我在Swift/objective-c iOS应用程序中使用了这个,所以如果有人有其他工具的简单解决方案,请告诉我。
编辑:添加数据的示例结构
SEQUENCE
SEQUENCE
INTEGER
INTEGER
SEQUENCE
INTEGER
INTEGER
总有一个父序列包含1到n个序列,每个序列包含2个整数(elgamal加密对(g^r,mh^r))。
SEQUENCE
SEQUENCE
INTEGER
INTEGER
SEQUENCE
INTEGER
INTEGER
为此,您可能需要以下内容:
ArraySource as(data, size);
Integer i1, i2, i3, i4;
BERSequenceDecoder d1(as);
BERSequenceDecoder d2(d1);
i1.BERDecode(d2);
i2.BERDecode(d2);
d2.MessageEnd();
BERSequenceDecoder d3(d2);
i3.BERDecode(d3);
i4.BERDecode(d3);
d3.MessageEnd();
d1.MessageEnd();
,每个包含2个整数(elgamal加密对(g^r,mh^r))。
一旦有了参数(请参阅下文),就应该用这些参数调用其中一个Initialize
函数不要调用那些使用PRNG的,因为它们会创建参数和键。
有关一些相关的类定义,请参阅gfpcrypt.h。另请参阅ElGamal-加密++维基。
下面是一个生成整数对并将它们封装在ASN.1结构中的示例,如您所愿。然后,它将它们读回来,并在没有剩余消耗时停止(即,内部Integer对是可选的)。
您可以像./asn1-test.exe
、./asn1-test.exe 3
、./asn1-test.exe 6
或./asn1-test.exe 128
那样运行它。大小只有48位,所以您不会浪费时间生成不需要的整数。
static const unsigned int BIT_COUNT = 48;
int main(int argc, char* argv[])
{
unsigned int count = 2;
if(argc >= 2 && argv[1] != NULL)
{
istringstream iss(argv[1]);
iss >> count;
if(iss.fail()) count = 2;
}
cout << "Testing " << count << " integer pairs" << endl;
// Count to pairs
count *= 2;
try
{
AutoSeededRandomPool prng;
vector<Integer> vv;
vv.resize(count);
for(unsigned int i = 0; i < count; i += 2)
{
vv[i] = Integer(prng, BIT_COUNT);
vv[i + 1] = Integer(prng, BIT_COUNT);
}
// Scratch for holding ASN.1 encoded structures in Crypto++
ByteQueue queue;
// Encode them
{
DERSequenceEncoder outer(queue);
for(unsigned int i = 0; i < count; i += 2)
{
DERSequenceEncoder inner(outer);
vv[i].DEREncode(inner);
vv[i + 1].DEREncode(inner);
inner.MessageEnd();
}
outer.MessageEnd();
}
// Save it to file (use dumpasn1 to view it)
FileSink fs("sequences.der", true);
queue.CopyTo(fs);
fs.MessageEnd();
// Decode them
{
BERSequenceDecoder outer(queue);
// Ensure we break from the loop based on EndReached()
for( ; ; )
{
if(outer.EndReached()) break;
BERSequenceDecoder inner(outer);
Integer i1, i2;
i1.BERDecode(inner);
i2.BERDecode(inner);
cout << "Pair" << endl;
cout << std::hex << " Integer: " << i1 << endl;
cout << std::hex << " Integer: " << i2 << endl;
inner.MessageEnd();
}
outer.MessageEnd();
}
} catch (const Exception& ex) {
cerr << std::dec << ex.what() << endl;
exit (1);
}
return 0;
}
下面是运行和转储的样子:
$ ./asn1-test.exe 3
Testing 3 integer pairs
Pair
Integer: 301818b3c631h
Integer: 1ff0ebf1ca4bh
Pair
Integer: f97e9d28e9cah
Integer: 94813cab125fh
Pair
Integer: 8a146ea68e7ch
Integer: 60d48ef2462fh
$ dumpasn1 sequences.der
0 57: SEQUENCE {
2 16: SEQUENCE {
4 6: INTEGER 30 18 18 B3 C6 31
12 6: INTEGER 1F F0 EB F1 CA 4B
: }
20 18: SEQUENCE {
22 7: INTEGER 00 F9 7E 9D 28 E9 CA
31 7: INTEGER 00 94 81 3C AB 12 5F
: }
40 17: SEQUENCE {
42 7: INTEGER 00 8A 14 6E A6 8E 7C
51 6: INTEGER 60 D4 8E F2 46 2F
: }
: }
0 warnings, 0 errors.
以下是为您省去查找麻烦的内容:
#include <iostream>
using std::ostream;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <sstream>
using std::istringstream;
#include <cryptopp/cryptlib.h>
using CryptoPP::Exception;
#include <cryptopp/filters.h>
using CryptoPP::StringSource;
using CryptoPP::StringSink;
#include <cryptopp/files.h>
using CryptoPP::FileSink;
#include <cryptopp/integer.h>
using CryptoPP::Integer;
#include <cryptopp/osrng.h>
using CryptoPP::AutoSeededRandomPool;
#include <cryptopp/asn.h>
#include <cryptopp/oids.h>
namespace ASN1 = CryptoPP::ASN1;
using CryptoPP::DERSequenceEncoder;
using CryptoPP::BERSequenceDecoder;
#include <cryptopp/queue.h>
using CryptoPP::ByteQueue;
相关文章:
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 允许从 std::map 的密钥窃取资源?
- TMap::Emplace() 在应用现有密钥时会覆盖吗?
- 在没有密钥的情况下读取密文的剩余噪声预算
- 如何修复无效的API密钥,IP或操作权限错误?
- 所有可能的链接生成器与64位密钥
- 如何在unordered_map中更改密钥?
- 获取当前密钥状态?
- curl_easy_perform() 失败:SSL 对等证书或 SSH 远程密钥不正常
- 将密钥发送到非前台的游戏窗口
- 有哪些方法可以对基于 256 位密钥的矩阵进行加扰?
- 如何在精灵表上的两个不同部分之间来回切换,同时用户仍使用 SFML 持有密钥
- 如何将CNG密钥转换为OpenSSL EVP_PKEY(反之亦然)?
- 运行密钥密码解密知道密钥?
- std::unordered_map 运算符 [] 是否对非现有密钥进行零初始化?
- 封装 std::map 以允许迭代,但没有直接密钥访问?
- 如果我使用同一个密钥推送用户数据两次,会发生什么
- 如何在C++中创建自己的编译密钥
- 重载std::映射不同的密钥类型
- 如何解码非密钥ASN1数据