Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getting an invalid ASN1 Error #45

Closed
tharrington opened this issue Nov 1, 2015 · 9 comments
Closed

getting an invalid ASN1 Error #45

tharrington opened this issue Nov 1, 2015 · 9 comments

Comments

@tharrington
Copy link

I have a secret which i am attempting to use to generate a new key:

var NodeRSA = require('node-rsa');
var key = new NodeRSA();

var keyData = '-----BEGIN RSA PRIVATE KEY-----PFJTQUtleVZhbHVlPjxNb2R1bHVzPjFvRUl3cmsrY1lDayt4b3BnYTBxM3c5K0l1UWlVZllSZ3dWS1A5dHpOL003WDFnK0NWREJnelExdjVqKzgydGtYYnNpQ2RWc3VoNFdiYWNteUM4Rlgybk51QWVwenhybUtvNEp2OTFYMGc1SnRGZzd5NzNIYlhpcWpVVW1jd1MwZ2dRdGh2bnNZcTBuYkFId0NSSkMxakhIMVRObDFIOVhsVEJpRzVweFVCVT08L01vZHVsdXM+PEV4cG9uZW50PkFRQUI8L0V4cG9uZW50PjwvUlNBS2V5VmFsdWU+-----END RSA PRIVATE KEY-----';
key.importKey(keyData, 'pkcs8');

type=error service=web error="InvalidAsn1Error: encoding too long" stack="InvalidAsn1Error: encoding too long.

I've also gotten the error: expecting x02 but saw x53.

@rzcoder
Copy link
Owner

rzcoder commented Nov 1, 2015

Hey! This key have wrong format, it just base64 encoded XML and not actually private key in pkcs8 format ¯_(ツ)_/¯.

@rzcoder
Copy link
Owner

rzcoder commented Nov 1, 2015

Also, header "-----BEGIN RSA PRIVATE KEY-----" means it pkcs1, pkcs8 should have "-----BEGIN PRIVATE KEY-----" header.

@tharrington
Copy link
Author

This is the secret provided by a third party. How would I make it work?

@tharrington
Copy link
Author

From my secret and exponent, I am generating the encrypted string... But I get a different encrypted string everytime i run:

var NodeRSA = require('node-rsa');
var key = new NodeRSA();

var modulus = new Buffer('1oEIwrk+cYCk+xopga0q3w9+IuQiUfYRgwVKP9tzN/M7X1g+CVDBgzQ1v5j+82tkXbsiCdVsuh4WbacmyC8FX2nNuAepzxrmKo4Jv91X0g5JtFg7y73HbXiqjUUmcwS0ggQthvnsYq0nbAHwCRJC1jHH1TNl1H9XlTBiG5pxUBU=', 'base64');
var exp = new Buffer('AQAB', 'base64');

key = new NodeRSA();
key.keyPair.setPublic(modulus, exp);

var text = 'Id=a064B000000X1UWQA0';
var encrypted = key.encrypt(text, 'base64');
console.log('encrypted: ', encrypted);

@rzcoder
Copy link
Owner

rzcoder commented Nov 2, 2015

It's normal behavior. RSA padded your data with some random numbers for secure.

@rzcoder rzcoder closed this as completed Nov 2, 2015
@rzcoder rzcoder reopened this Nov 2, 2015
@tharrington
Copy link
Author

with this library, is it possible to encrypt using an XML formatted key? The private key provider is using XML formatted private key. They are unable to decrypt an PEM formatted public key with an XML formatted private key.

I send in something encrypted like this:

var NodeRSA = require('node-rsa');

var modulus = new Buffer("4jS47lJICILyu6EzBzumEErfRZUx0aB5VAPf37CMFjWTd69+GhhErsSdwegM77W1uHRTUI0kHL4c07wd+fpPJTWFr5RclFb01+EEZoGHSVE4DdEVRRS9zhxEtvI0dvcwWL3jGkvge8hhdLpC9+MlfKOmD3r7kjpeSRflmnQ0NXM=", 'base64');
var exp = new Buffer("AQAB", 'base64');
var stringToEncode = new Buffer("Id='a064B000000X1aeQAC'");

var key = new NodeRSA();
key.keyPair.setPublic(modulus, exp);

var encrypted = key.encrypt(stringToEncode, 'base64');
console.log('encrypted: ', encrypted);

WHich then logs:
z1l4Tt0/boVGSRg4YHSVmLpn6uu4+v0PoC/Ef5QmpNPzlmBwnw7ANUV5EhnpeH47ntC8YEWJp2aVR1IMjllFfEsKm45YGsCnm+hHwpEMVzLGBgBAxmmFqCI6KV/vsP0Gl2T3NtrU+bN1KHG4DeVA+NShOqU63Cm9IkhgxH2sJA4=

This looks to be correct, but when I send it to their API, i do not get a result. Their team suggested the problem was because I am sending in PEM formated encryption, when I need XML formatted. They do not know how to generate an XML formatted public key for node...

Here is example encrypted output they accept:

VZSb0Zd7U1Ru35vrUgn4neCWkuvbrBuT6eWsmsi3vhRO6o1V2mdzaQ4liqI2q-u32YV69NzbeQIzD0v2sTmUP8l9Rxcxmx1P9md__ApBIOOeQh_df5DTbGEH-Am85-z4ExpNagox03KfdbFgivbhxN0j5a0iIVSqVFbqD2vkRNk1

I don't know enough to know the difference between the two except that the second does not have the special characters (=, +, / etc), indicating a different format.

@rzcoder
Copy link
Owner

rzcoder commented Nov 2, 2015

XML format key doesn't supports currently. You can try something like http://www.platanus.cz/blog/converting-rsa-xml-key-to-pem
Also PR welkome.

@LMAdrien
Copy link

LMAdrien commented Dec 9, 2015

hello,
i have the same problem but with a public key for the verify signature method. i tried all format but i can't resolve this error.

var pub = "-----BEGIN PUBLIC KEY----MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAnUvH+xfbkF0FgbGyIPEJj5mgO+j05iXobZalIJM8hh9o/s3RucKVU4gs0MtFLUQ5TImZ6IrLtSXonCT0hrSUGZuf5bGqds6P8+wb8onDQcG79lT+O1pmFbPb7XcdI1mMwPNO66vmydcC26O/gfduAUxn6CiyPbcSQjBeTneaSOq69/Zy8CWrAl+fp3o1dNQNbwCtcmDDd/55YfkigpPh+EFpu8sp2V+w4koooENjryEHHpjSQFdao4HOjTWKddfeK8JUUGDNViP8a0k2Qw9EQq+fVUi0FE5eRCMaeSJ7pwd5Op7g9x4UY4rtLAEz9XxOtJ26QJyANTLxPVv4HOq2AAjNKXJV4MPBKhwaK8I26oeFP4quQ5CD4CX/Mojg71CFV/IIpSkJO6HntA+/vG2pjQA6jckKaCKqzzV2ECgXIePdqrm+QR6DGmT1L+r2sMo8s5H8gUUrCmnI3Kd6zMQ9kOgk7P0Jh1LJdslWhSikAKbwjCXm7SEfLcrXg/JbspFpHHzZDyViOIuG+KrhZJpq1klo1q8JAPnXloTSM8B3qViQTSVUNLvjJup+nrlTHeP5qMVaA/Ogv7Ielqgk00soFwu8sr3KpTTipU7OuvfI22syLjHcjdjqNKf9uUF4GQ0HdoRjVtfHOj/EDyfu3catMNi7WS33J1mf8X15t+GvD0UCAwEAAQ==-----END PUBLIC KEY-----";
var key = new NodeRSA();
key.importKey(pub, 'pkcs8-public');
key.verify(id, sign,'base64');

I always get output Error: encoding too long

@rzcoder
Copy link
Owner

rzcoder commented Dec 10, 2015

Hey m8, just
-----BEGIN PUBLIC KEY----wrong
-----BEGIN PUBLIC KEY-----right

@rzcoder rzcoder closed this as completed Dec 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants