rzcoder / node-rsa

Node.js RSA library
1.39k stars 205 forks source link

Error [InvalidAsn1Error]: Expected 0x6: got 0xa0 #183

Open zapalalukasz opened 4 years ago

zapalalukasz commented 4 years ago

Hey.

I have input, signature as base64 string and public key in DER format encoded in BASE64 without start mark, end mark, and end line mark. But when I try execute this code I get: Error: [InvalidAsn1Error]: Expected 0x6: got 0xa0.

Can you tell me what I doing wrong?

const key = Buffer.from(publicKey, 'base64');
const rsa = new NodeRSA();
rsa.setOptions({signingScheme: 'pkcs1-sha256'});
const result = rsa
                .importKey(key, 'pkcs8-public-der')
                .verify(Buffer.from(input, 'base64'), Buffer.from(sign, 'base64'));

Sample data:

Input
 eyJhbGciOiJSUzI1NiIsIng1YyI6WyJNSUlGSERDQ0F3U2dBd0lCQWdJVE9nQUFBQWptajFXQlhVNm1PZ0FCQUFBQUNEQU5CZ2txaGtpRzl3MEJBUTBGQURBV01SUXdFZ1lEVlFRREV3dGxTMkZ6ZVMxVGRXSkRRVEFnRncweE56QTRNakl3TmpNek1UTmFHQTh5TURreU1EY3hNREV5TWpjeU9Wb3dnY2d4Q3pBSkJnTlZCQVlUQWxCTU1SUXdFZ1lEVlFRSUV3dE5ZWHB2ZDJsbFkydHBaVEVSTUE4R0ExVUVCeE1JVjJGeWMzcGhkMkV4SHpBZEJnTlZCQW9NRmsxcGJtbHpkR1Z5YzNSM2J5QkdhVzVoYm5QRHMzY3hJekFoQmdOVkJBc1RHa1JsY0dGeWRHRnRaVzUwSUVsdVptOXliV0YwZVhwaFkycHBNUjR3SEFZRFZRUURFeFYwWlhOMExXVXRhMkZ6ZVM1dFppNW5iM1l1Y0d3eEtqQW9CZ2txaGtpRzl3MEJDUUVXRzJsdVptOHVaUzFrWld0c1lYSmhZMnBsUUcxbUxtZHZkaTV3YkRDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTXZ5VlhHajhZTmh5NlAyOGJLajlNbGVBNytRWEtDdFBKWjRNNk1J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RMBAf8EAjAAMA0GCSqGSIb3DQEBDQUAA4ICAQCkdUR2DhgiexUW+y2rgaE6orWBPyMxveH2IPv0rPGzqdgUFcNH816YzDorEnOAvbRLB8BaoH+Wn/eElAQxqE5+47VgScIUF4oNHwXnnflR1XRoYcFZ/fBkIW2nfOK1C8y2vHtZG1QEyyVD/cxv7ubgOiJfOYScsHv5DItStFUBclvg3xrFi2zG5ahblMwqCGrvgPKOxR9+mXGD+eoThBHEP6aJF3Zu41mVwT/4cbSr5m3c77deEQ2CpQPGL874PiHy9omkjev9F5yoBzI7yphalyEIdbASU0UiUErjbS+hnw0RErV1bQQzQfS7qiKMBZTM4pzOv/Ro6f+OcBf7c16XtHrEgli/aNagKo34nFhUscQcUTCh3MsCKuVSZU3dbCdSLIvdoJIS5FLP+gr8LbQW9uR/NgwJhYr/w06k6AOF+TaJw8eakv5ELDOuzhipqB63BuMSCGFZcUQ2bDhdc5gcV9GlNgVEXmToee3fn89OQTC7GrCwFzNxwAM6gJ0MARyWl5Hmgr/pOb1MX5VehgaoHpjoveMAacONbtiOwfMUyhPdCJmnLP67lokvGq7PDJ/DUbespAqVm91TM6QbWjdanKGB6kJY+7H5ESI8sp/nzjHXdZxeIPO7lOTItKdRW82kRcBR9TNDSS6rt5sI16LW0NCJ2zprYt8XrNO728ljyA==
rzcoder commented 4 years ago

Your key is not valid key for this lib.

zapalalukasz commented 4 years ago

Can I somehow convert this key that maybe possible use your library?

rzcoder commented 4 years ago

You can use openssl for it I guess.