无法解码RSA公钥

时间:2023-01-26 12:09:22

I'm having a problem similar to the one asked here. I have what I believe to be a DER encoded RSA PKCS#1 public key, and I want to use it to verify some other data/signatures I have, but I can't even get the decoding to work.

我有一个和这里问的问题相似的问题。我有一个我认为是DER编码的RSA PKCS#1公钥,我想用它来验证我有的其他数据/签名,但我甚至不能让解码工作。

I'm using the same code that was proposed in the solutions to that question.

我使用的代码和那个问题的解决方案中提出的代码是一样的。

ByteQueue queue;
StringSource ss(key, true, new HexDecoder(new Redirector(queue)));

RSASSA_PKCS1v15_SHA_Verifier verifier;
verifier.AccessKey().BERDecodePublicKey(queue, false, 0);

AutoSeededRandomPool prng;
if (!verifier.AccessKey().Validate(prng, 3))
  throw Exception(Exception::OTHER_ERROR, "Failed to validate public key");

When I use the key that is posted in that question the code works, however when I try to use mine it fails. Do I have the format wrong? Something else? I'm pretty new to crypto so it's probably something dumb/obvious...

当我使用这个问题中发布的密钥时,代码就会工作,但是当我尝试使用我的密钥时,代码就会失败。我的格式错了吗?别的吗?我对密码很陌生,所以可能是一些愚蠢/明显的东西……

Here's the key I'm trying to use.

这是我要用的钥匙。

30819D300D06092A864886F70D010101050003818B0030818702818100B126088
1BDFE84463D88C6AB8DB914A2E593893C10508B8A5ABDF692E9A5419A3EDBAE86
A052849983B75E3B425C18178B260003D857DF0B6505C6CF9C84F5859FCE3B63F
1FB2D4818501F6C5FA4AD1430EEB081A74ABD74CD1F4AA1FCCA3B88DD0548AED3
4443CEB52444EAE9099AA4FE66B2E6224D02381C248025C7044079020111

EDIT:

编辑:

Forgot to mention, here's the error I get:

忘了说,这里有一个错误:

Error!
Dynamic exception type: class CryptoPP::BERDecodeErr
std::exception::what: BER decode error

1 个解决方案

#1


5  

Its a SubjectPublicKeyInfo (SPKI). You need to call Load on it after HexDecoding.

这是一个SubjectPublicKeyInfo(SPKI)。您需要在解码后调用Load on它。

First, save it to a file in ASN.1/DER to see what it is:

首先,将它保存到ASN.1/DER的一个文件中,查看它是什么:

string dek("30819D300D06092A864886F70D010101050003818B0030818702818100B126088"
       "1BDFE84463D88C6AB8DB914A2E593893C10508B8A5ABDF692E9A5419A3EDBAE86"
       "A052849983B75E3B425C18178B260003D857DF0B6505C6CF9C84F5859FCE3B63F"
       "1FB2D4818501F6C5FA4AD1430EEB081A74ABD74CD1F4AA1FCCA3B88DD0548AED3"
       "4443CEB52444EAE9099AA4FE66B2E6224D02381C248025C7044079020111");

HexDecoder decoder(new FileSink("key.der", true));
decoder.Put((const byte*)dek.data(), dek.size());
decoder.MessageEnd();

Then, see what it is with Gutmann's dumpasn1:

然后,看看古特曼的《傻瓜》是怎么回事:

$ dumpasn1 key.der 
  0 157: SEQUENCE {
  3  13:   SEQUENCE {
  5   9:     OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
 16   0:     NULL
       :     }
 18 139:   BIT STRING, encapsulates {
 22 135:     SEQUENCE {
 25 129:       INTEGER
       :         00 B1 26 08 81 BD FE 84 46 3D 88 C6 AB 8D B9 14
       :         A2 E5 93 89 3C 10 50 8B 8A 5A BD F6 92 E9 A5 41
       :         9A 3E DB AE 86 A0 52 84 99 83 B7 5E 3B 42 5C 18
       :         17 8B 26 00 03 D8 57 DF 0B 65 05 C6 CF 9C 84 F5
       :         85 9F CE 3B 63 F1 FB 2D 48 18 50 1F 6C 5F A4 AD
       :         14 30 EE B0 81 A7 4A BD 74 CD 1F 4A A1 FC CA 3B
       :         88 DD 05 48 AE D3 44 43 CE B5 24 44 EA E9 09 9A
       :         A4 FE 66 B2 E6 22 4D 02 38 1C 24 80 25 C7 04 40
       :         79
157   1:       INTEGER 17
       :       }
       :     }
       :   }

rsaEncryption (1 2 840 113549 1 1 1) tells you its a RSA key and SPKI.

rsaEncryption(1 2 840 113549 1)告诉您它是RSA密钥和SPKI。


So here's how I might do it:

我可以这样做:

AutoSeededRandomPool prng;
string dek("30819D300D06092A864886F70D010101050003818B0030818702818100B126088"
           "1BDFE84463D88C6AB8DB914A2E593893C10508B8A5ABDF692E9A5419A3EDBAE86"
           "A052849983B75E3B425C18178B260003D857DF0B6505C6CF9C84F5859FCE3B63F"
           "1FB2D4818501F6C5FA4AD1430EEB081A74ABD74CD1F4AA1FCCA3B88DD0548AED3"
           "4443CEB52444EAE9099AA4FE66B2E6224D02381C248025C7044079020111");

try {

    ByteQueue queue;
    HexDecoder decoder(new Redirector(queue));

    decoder.Put((const byte*)dek.data(), dek.size());
    decoder.MessageEnd();

    RSASSA_PKCS1v15_SHA_Verifier verifier;
    verifier.AccessKey().Load(queue);

    if (!verifier.AccessKey().Validate(prng, 3))
        throw Exception(Exception::OTHER_ERROR, "Failed to validate public key");

    cout << "Verified key" << endl;
}
catch(CryptoPP::Exception& ex)
{
    cerr << ex.what() << endl;
}

The program results in (as expected):

项目结果如下(如预期):

$ ./cryptopp-test.exe
Verified key

Based on you Pastebin with the following (some rather poor formatting added by me):

基于你的Pastebin和以下(一些相当糟糕的格式加上我):

text:1002712F                 
mov
     [ebp+a1], offset ??_7?$TF_VerifierImpl@U?$TF_SignatureSchemeOptions@V?
     $TF_SS@UPKCS1v15@CryptoPP@@VSHA1@2@URSA@2@H@CryptoPP@@URSA@2@
     VPKCS1v15_SignatureMessageEncodingMethod@2@VSHA1@2@@CryptoPP@@@CryptoPP@@6B?
     $TF_VerifierImpl@U?$TF_SignatureSchemeOptions@V?
     $TF_SS@UPKCS1v15@CryptoPP@@VSHA1@2@URSA@2@H@CryptoPP@@URSA@2@
     VPKCS1v15_SignatureMessageEncodingMethod@
     2@VSHA1@2@@CryptoPP@@@CryptoPP@@@ ; const
     CryptoPP::TF_VerifierImpl<CryptoPP::TF_SignatureSchemeOptions<CryptoPP::TF_SS<
     CryptoPP::PKCS1v15,CryptoPP::SHA1,CryptoPP::RSA,int>,
     CryptoPP::RSA,CryptoPP::PKCS1v15_SignatureMessageEncodingMethod,
     CryptoPP::SHA1>>::`vftable'{for `CryptoPP::TF_VerifierImpl<
     CryptoPP::TF_SignatureSchemeOptions<CryptoPP::TF_SS<
     CryptoPP::PKCS1v15,CryptoPP::SHA1,CryptoPP::RSA,int>,
     CryptoPP::RSA,CryptoPP::PKCS1v15_SignatureMessageEncodingMethod,CryptoPP::SHA1>>'

I'm guessing RSASSA_PKCS1v15_SHA_Verifier will be OK. But you won't know until you try to consume an encoded signature.

我猜RSASSA_PKCS1v15_SHA_Verifier应该没问题。但在尝试使用编码签名之前,您不会知道。

If RSASSA_PKCS1v15_SHA_Verifier does not work, then try adding a typedef that uses SHA256:

如果RSASSA_PKCS1v15_SHA_Verifier不能工作,那么尝试添加一个使用SHA256的typedef:

typedef RSASS<PKCS1v15, SHA256>::Signer RSASSA_PKCS1v15_SHA256_Signer;
typedef RSASS<PKCS1v15, SHA256>::Verifier RSASSA_PKCS1v15_SHA256_Verifier;

Here are the original typedefs from the library at rsa.h:

这是rsa图书馆的原始版本。h:

00161 typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
00162 typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;

The Crypto++ wiki has a page on Keys and Formats. It appears to be a lot of rambling until you have a concrete case to look at :) It even shows you how to create keys using other libraries, like OpenSSL and GnuTLS.

Crypto++ wiki有一个关于键和格式的页面。在您有具体的案例可以查看之前,它似乎有很多杂乱无章的内容。)它甚至向您展示了如何使用其他库(如OpenSSL和GnuTLS)创建密钥。

#1


5  

Its a SubjectPublicKeyInfo (SPKI). You need to call Load on it after HexDecoding.

这是一个SubjectPublicKeyInfo(SPKI)。您需要在解码后调用Load on它。

First, save it to a file in ASN.1/DER to see what it is:

首先,将它保存到ASN.1/DER的一个文件中,查看它是什么:

string dek("30819D300D06092A864886F70D010101050003818B0030818702818100B126088"
       "1BDFE84463D88C6AB8DB914A2E593893C10508B8A5ABDF692E9A5419A3EDBAE86"
       "A052849983B75E3B425C18178B260003D857DF0B6505C6CF9C84F5859FCE3B63F"
       "1FB2D4818501F6C5FA4AD1430EEB081A74ABD74CD1F4AA1FCCA3B88DD0548AED3"
       "4443CEB52444EAE9099AA4FE66B2E6224D02381C248025C7044079020111");

HexDecoder decoder(new FileSink("key.der", true));
decoder.Put((const byte*)dek.data(), dek.size());
decoder.MessageEnd();

Then, see what it is with Gutmann's dumpasn1:

然后,看看古特曼的《傻瓜》是怎么回事:

$ dumpasn1 key.der 
  0 157: SEQUENCE {
  3  13:   SEQUENCE {
  5   9:     OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
 16   0:     NULL
       :     }
 18 139:   BIT STRING, encapsulates {
 22 135:     SEQUENCE {
 25 129:       INTEGER
       :         00 B1 26 08 81 BD FE 84 46 3D 88 C6 AB 8D B9 14
       :         A2 E5 93 89 3C 10 50 8B 8A 5A BD F6 92 E9 A5 41
       :         9A 3E DB AE 86 A0 52 84 99 83 B7 5E 3B 42 5C 18
       :         17 8B 26 00 03 D8 57 DF 0B 65 05 C6 CF 9C 84 F5
       :         85 9F CE 3B 63 F1 FB 2D 48 18 50 1F 6C 5F A4 AD
       :         14 30 EE B0 81 A7 4A BD 74 CD 1F 4A A1 FC CA 3B
       :         88 DD 05 48 AE D3 44 43 CE B5 24 44 EA E9 09 9A
       :         A4 FE 66 B2 E6 22 4D 02 38 1C 24 80 25 C7 04 40
       :         79
157   1:       INTEGER 17
       :       }
       :     }
       :   }

rsaEncryption (1 2 840 113549 1 1 1) tells you its a RSA key and SPKI.

rsaEncryption(1 2 840 113549 1)告诉您它是RSA密钥和SPKI。


So here's how I might do it:

我可以这样做:

AutoSeededRandomPool prng;
string dek("30819D300D06092A864886F70D010101050003818B0030818702818100B126088"
           "1BDFE84463D88C6AB8DB914A2E593893C10508B8A5ABDF692E9A5419A3EDBAE86"
           "A052849983B75E3B425C18178B260003D857DF0B6505C6CF9C84F5859FCE3B63F"
           "1FB2D4818501F6C5FA4AD1430EEB081A74ABD74CD1F4AA1FCCA3B88DD0548AED3"
           "4443CEB52444EAE9099AA4FE66B2E6224D02381C248025C7044079020111");

try {

    ByteQueue queue;
    HexDecoder decoder(new Redirector(queue));

    decoder.Put((const byte*)dek.data(), dek.size());
    decoder.MessageEnd();

    RSASSA_PKCS1v15_SHA_Verifier verifier;
    verifier.AccessKey().Load(queue);

    if (!verifier.AccessKey().Validate(prng, 3))
        throw Exception(Exception::OTHER_ERROR, "Failed to validate public key");

    cout << "Verified key" << endl;
}
catch(CryptoPP::Exception& ex)
{
    cerr << ex.what() << endl;
}

The program results in (as expected):

项目结果如下(如预期):

$ ./cryptopp-test.exe
Verified key

Based on you Pastebin with the following (some rather poor formatting added by me):

基于你的Pastebin和以下(一些相当糟糕的格式加上我):

text:1002712F                 
mov
     [ebp+a1], offset ??_7?$TF_VerifierImpl@U?$TF_SignatureSchemeOptions@V?
     $TF_SS@UPKCS1v15@CryptoPP@@VSHA1@2@URSA@2@H@CryptoPP@@URSA@2@
     VPKCS1v15_SignatureMessageEncodingMethod@2@VSHA1@2@@CryptoPP@@@CryptoPP@@6B?
     $TF_VerifierImpl@U?$TF_SignatureSchemeOptions@V?
     $TF_SS@UPKCS1v15@CryptoPP@@VSHA1@2@URSA@2@H@CryptoPP@@URSA@2@
     VPKCS1v15_SignatureMessageEncodingMethod@
     2@VSHA1@2@@CryptoPP@@@CryptoPP@@@ ; const
     CryptoPP::TF_VerifierImpl<CryptoPP::TF_SignatureSchemeOptions<CryptoPP::TF_SS<
     CryptoPP::PKCS1v15,CryptoPP::SHA1,CryptoPP::RSA,int>,
     CryptoPP::RSA,CryptoPP::PKCS1v15_SignatureMessageEncodingMethod,
     CryptoPP::SHA1>>::`vftable'{for `CryptoPP::TF_VerifierImpl<
     CryptoPP::TF_SignatureSchemeOptions<CryptoPP::TF_SS<
     CryptoPP::PKCS1v15,CryptoPP::SHA1,CryptoPP::RSA,int>,
     CryptoPP::RSA,CryptoPP::PKCS1v15_SignatureMessageEncodingMethod,CryptoPP::SHA1>>'

I'm guessing RSASSA_PKCS1v15_SHA_Verifier will be OK. But you won't know until you try to consume an encoded signature.

我猜RSASSA_PKCS1v15_SHA_Verifier应该没问题。但在尝试使用编码签名之前,您不会知道。

If RSASSA_PKCS1v15_SHA_Verifier does not work, then try adding a typedef that uses SHA256:

如果RSASSA_PKCS1v15_SHA_Verifier不能工作,那么尝试添加一个使用SHA256的typedef:

typedef RSASS<PKCS1v15, SHA256>::Signer RSASSA_PKCS1v15_SHA256_Signer;
typedef RSASS<PKCS1v15, SHA256>::Verifier RSASSA_PKCS1v15_SHA256_Verifier;

Here are the original typedefs from the library at rsa.h:

这是rsa图书馆的原始版本。h:

00161 typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
00162 typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;

The Crypto++ wiki has a page on Keys and Formats. It appears to be a lot of rambling until you have a concrete case to look at :) It even shows you how to create keys using other libraries, like OpenSSL and GnuTLS.

Crypto++ wiki有一个关于键和格式的页面。在您有具体的案例可以查看之前,它似乎有很多杂乱无章的内容。)它甚至向您展示了如何使用其他库(如OpenSSL和GnuTLS)创建密钥。