Muralee
Muralee

Reputation: 1

decoding an asn.1 der octet string with bouncy castle

I want to generate private key from DER encoded octet string.

0x3A:6B:2E:AA:0D:9F:25:A9:E4:55:98:3F:EB:5B:B9:47:52:81:21:91:1B:F3:B7:6B:E5:66:1C:89:DB:F2:4B:26

I have tried to convert to Byte ...

byte[] bytes = DatatypeConverter.parseHexBinary(
    "3A6B2EAA0D9F25A9E455983FEB5BB947528121911BF3B76BE5661C89DBF24B26");
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes);
PrivateKey pkey = fact.generatePrivate(privSpec);

But didnt work. It throws Exception

"DEF length 107 object truncated by 77"

Can you please help?

Upvotes: 0

Views: 1807

Answers (1)

Axel Kemper
Axel Kemper

Reputation: 11322

Your data does not look like valid DER encoded data.

I tried to decode it with Peter Gutman's dumpasn1 and got:

dumpasn1 -ahlt xxx.der    

<3A 6B>
0 107: VisibleString {

Error: Invalid data encountered at position 4: 2E AA.

A DER OCTET-STRING should have byte 0x06 as first tag byte.

Upvotes: 1

Related Questions