无法用NDK Android解码RSA公钥

时间:2016-10-19 02:05:35

标签: android android-ndk rsa crypto++

使用Crypto ++保存RSA公钥字符串时遇到问题。解码密钥时,我总是得到BERDecodeErr异常。

代码:

string RsaEncryptor::encryptor(string plaintext, string publicKey)
{
    std::string cipher;
    AutoSeededRandomPool prng;

    try
    {
        ByteQueue queue;
        Base64Decoder decoder(new Redirector(queue));
        decoder.Put((const byte *) publicKey.data(), publicKey.size());
        decoder.MessageEnd();

        RSA::PublicKey rsaPublick;
        rsaPublick.BERDecodePublicKey(queue, false, (size_t) queue.MaxRetrievable());

        // BERDecodePrivateKey is a void function. Here's the only check
        // we have regarding the DER bytes consumed.
        CRYPTOPP_ASSERT(queue.IsEmpty());

        bool valid = rsaPublick.Validate(prng, 3);
        if (!valid)
            cipher = "RSA private key is not valid";

        RSAES_OAEP_SHA_Encryptor e(rsaPublick);
        StringSource ss(plaintext, true,
                     new PK_EncryptorFilter(prng, e,
                                            new StringSink(cipher)
                     ) // PK_EncryptorFilter
        ); // StringSource
    }
    catch (CryptoPP::Exception &e) {
        cipher = e.what();
    }
    return cipher;
}

公钥是:

MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDs648aASMAR9VprkzNVS7b‌​36N1hiYvbBG0c
dE0QkS3‌​H/sc3+Ej92lGBQErpBu9‌​LVhwN/beBX4QnbCn1eNS‌​rKoOzS4yqWlwOaCe0WLm‌​FDHCn1
cMTkX89cT4A0pc‌​jBbY+0W7htxWcqHxEQH9‌​x/AjQ9/4blerh1i6/lLI‌​o6hn2hB8kQIB

1 个答案:

答案 0 :(得分:1)

MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDs648aASMAR9VprkzNVS7b‌​36N1hiYvbBG0c
dE0QkS3‌​H/sc3+Ej92lGBQErpBu9‌​LVhwN/beBX4QnbCn1eNS‌​rKoOzS4yqWlwOaCe0WLm‌​FDHCn1
cMTkX89cT4A0pc‌​jBbY+0W7htxWcqHxEQH9‌​x/AjQ9/4blerh1i6/lLI‌​o6hn2hB8kQIB

密钥格式不正确。

剥离Base64编码:

FileSource fs1("key.b64", true, new Base64Decoder);
FileSink fs2("key.der");        
fs1.CopyTo(fs2);

然后在Gutmann的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 EC EB 8F 1A 01 23 00 47 D5 69 AE 4C CD 55 2E
       :         DB DF A3 75 86 26 2F 6C 11 B4 71 D1 34 42 44 B7
       :         1F FB 1C DF E1 23 F7 69 46 05 01 2B A4 1B BD 2D
       :         58 70 37 F6 DE 05 7E 10 9D B0 A7 D5 E3 52 AC AA
       :         0E CD 2E 32 A9 69 70 39 A0 9E D1 62 E6 14 31 C2
       :         9F 57 0C 4E 45 FC F5 C4 F8 03 4A 5C 8C 16 D8 FB
       :         45 BB 86 DC 56 72 A1 F1 11 01 FD C7 F0 23 43 DF
       :         F8 6E 57 AB 87 58 BA FE 52 C8 A3 A8 67 DA 10 7C
       :         91
157   1:       INTEGER -1
       :         Error: Integer has a negative value.
       :       }
       :     }
       :   }

从上面看,问题ASN.1位于第157位。查看文件大小:

-rw-r--r--  1 jwalton  staff  261 Oct 18 23:09 key.b64
-rw-r--r--  1 jwalton  staff  159 Oct 18 23:18 key.der

看起来剩下两个字节需要处理。 02INTEGER的ASN.1标记。它后面应该跟一个长度和一个值,但只有八位字节长度(01)。缺少价值。

$ xxd -g 1 key.der
0000000: 30 81 9d 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01  0..0...*.H......
0000010: 05 00 03 81 8b 00 30 81 87 02 81 81 00 ec eb 8f  ......0.........
0000020: 1a 01 23 00 47 d5 69 ae 4c cd 55 2e db df a3 75  ..#.G.i.L.U....u
0000030: 86 26 2f 6c 11 b4 71 d1 34 42 44 b7 1f fb 1c df  .&/l..q.4BD.....
0000040: e1 23 f7 69 46 05 01 2b a4 1b bd 2d 58 70 37 f6  .#.iF..+...-Xp7.
0000050: de 05 7e 10 9d b0 a7 d5 e3 52 ac aa 0e cd 2e 32  ..~......R.....2
0000060: a9 69 70 39 a0 9e d1 62 e6 14 31 c2 9f 57 0c 4e  .ip9...b..1..W.N
0000070: 45 fc f5 c4 f8 03 4a 5c 8c 16 d8 fb 45 bb 86 dc  E.....J\....E...
0000080: 56 72 a1 f1 11 01 fd c7 f0 23 43 df f8 6e 57 ab  Vr.......#C..nW.
0000090: 87 58 ba fe 52 c8 a3 a8 67 da 10 7c 91 02 01     .X..R...g..|...

这是问题的另一种观点。 02是ASN.1 INTEGER01是整数的长度,但缺少值:

0000090: 87 58 ba fe 52 c8 a3 a8 67 da 10 7c 91 ***02 01 ??***

这是一个好的1024位密钥。它与你的不同:

$ 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 D0 52 BA 4B 3F 44 AA 7B C9 C2 84 46 17 D6 17
       :         A0 43 03 69 7B 18 06 5B D4 EA 29 E2 74 24 40 62
       :         58 16 52 9D 73 82 77 D7 9D 13 97 53 71 76 9F B2
       :         99 CA 36 1E D4 E1 DF 0D BE C4 07 1E A0 F2 F2 E9
       :         EF 32 FA 00 4B 0B 8E C2 91 BA 8B 1D 1C 4D F0 98
       :         6C 64 C5 9E 4D EE 58 17 06 20 C9 3C 9A F0 33 BB
       :         A8 FC 7B 7B 6C F9 C6 FD A0 17 76 3A 3D 1D 7E E7
       :         42 C2 49 AD 4C 26 AE B6 F6 DC 99 A3 24 99 1A 30
       :         D9
157   1:       INTEGER 17
       :       }
       :     }
       :   }

0 warnings, 0 errors.

请注意,0201 11下方)后面有两个八位字节,而不是一个八位字节(与您的一样):

$ xxd -g 1 key.der
0000000: 30 81 9d 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01  0..0...*.H......
0000010: 05 00 03 81 8b 00 30 81 87 02 81 81 00 d0 52 ba  ......0.......R.
0000020: 4b 3f 44 aa 7b c9 c2 84 46 17 d6 17 a0 43 03 69  K?D.{...F....C.i
0000030: 7b 18 06 5b d4 ea 29 e2 74 24 40 62 58 16 52 9d  {..[..).t$@bX.R.
0000040: 73 82 77 d7 9d 13 97 53 71 76 9f b2 99 ca 36 1e  s.w....Sqv....6.
0000050: d4 e1 df 0d be c4 07 1e a0 f2 f2 e9 ef 32 fa 00  .............2..
0000060: 4b 0b 8e c2 91 ba 8b 1d 1c 4d f0 98 6c 64 c5 9e  K........M..ld..
0000070: 4d ee 58 17 06 20 c9 3c 9a f0 33 bb a8 fc 7b 7b  M.X.. .<..3...{{
0000080: 6c f9 c6 fd a0 17 76 3a 3d 1d 7e e7 42 c2 49 ad  l.....v:=.~.B.I.
0000090: 4c 26 ae b6 f6 dc 99 a3 24 99 1a 30 d9 02 01 11  L&......$..0....