diff --git a/src/rsa/import_key.ts b/src/rsa/import_key.ts index 7021fed..377f8ee 100644 --- a/src/rsa/import_key.ts +++ b/src/rsa/import_key.ts @@ -57,7 +57,7 @@ function rsa_import_jwk(key: JSONWebKey): RSAKeyParams { * @param key */ function rsa_import_pem_cert(key: string): RSAKeyParams { - const trimmedKey = key.substr(27, key.length - 53); + const trimmedKey = key.substr(27, key.length - 52); const parseKey = ber_simple( ber_decode(base64_to_binary(trimmedKey)), ) as RSACertKeyFormat; @@ -76,7 +76,7 @@ function rsa_import_pem_cert(key: string): RSAKeyParams { * @param key PEM encoded key format */ function rsa_import_pem_private(key: string): RSAKeyParams { - const trimmedKey = key.substr(31, key.length - 61); + const trimmedKey = key.substr(31, key.length - 60); const parseKey = ber_simple( ber_decode(base64_to_binary(trimmedKey)), ) as bigint[]; @@ -101,7 +101,7 @@ function rsa_import_pem_private(key: string): RSAKeyParams { * @param key PEM encoded key format */ function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams { - const trimmedKey = key.substr(27, key.length - 57); + const trimmedKey = key.substr(27, key.length - 52); const parseWrappedKey = ber_simple( ber_decode(base64_to_binary(trimmedKey)), ) as [number, unknown, Uint8Array]; @@ -128,7 +128,7 @@ function rsa_import_pem_private_pkcs8(key: string): RSAKeyParams { * @param key PEM encoded key format */ function rsa_import_pem_public(key: string): RSAKeyParams { - const trimmedKey = key.substr(26, key.length - 51); + const trimmedKey = key.substr(26, key.length - 50); const parseKey = ber_simple( ber_decode(base64_to_binary(trimmedKey)), ) as RSAPublicKeyFormat; diff --git a/tests/rsa/rsa.import_key.test.ts b/tests/rsa/rsa.import_key.test.ts index e8d5915..2d9452c 100644 --- a/tests/rsa/rsa.import_key.test.ts +++ b/tests/rsa/rsa.import_key.test.ts @@ -86,9 +86,12 @@ Deno.test("RSA - Import Private Key (PKCS8)", () => { const e = 65537n; const d = 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n; + const qi = + 74289828655732991195558689791424564707132679819891037249694826356870666658448134417270855397604232230569020096065966539928979441113025455296436655960522783579897346034371763410999278044050901737497683356751868229820180356905837023964589114523481066796348442538273820591008174072287844578937802915493303718997n; assertEquals(key.e, e); assertEquals(key.n, n); assertEquals(key.d, d); + assertEquals(key.qi, qi); assertEquals(key.length, 256); // Testing key with trailing newline @@ -97,3 +100,47 @@ Deno.test("RSA - Import Private Key (PKCS8)", () => { assertEquals(key2.n, n); assertEquals(key2.d, d); }); + +Deno.test("RSA - Import RSA Private Key (PEM)", () => { + const raw = `-----BEGIN RSA PRIVATE KEY----- + MIIEowIBAAKCAQEAlWeqIOFxsSwBx2cB5tuwLVDD+vvMBapZb7qdE69FdtWL8cz8 + Vo2r9vpiziDPLqWu6c73KXORIgh+fHXp5iybMoKqNK9gfFocx2PqwiFiX1dmEmcY + 86b0gB38yyaw6YV4KoJwjcRI0bLsgmAIhiyDCtIbmPdAOTDDAK4FM5MfJDHf36vx + nfV7eYiPhezZcpSfmq5yXN9OqYBhytzTdhgV9b50rggdvNElF5GX0wXspMg2emKw + KtF6ZwjVfSch6i0OBJoxIXNslKN2jUclzTAXXWcdxwixXMcGQlQKdLUJAHexK7FA + +T+Q0fESCd365JUlFGQehJA8PBuyi1ilXDUNhQIDAQABAoIBAAVapJjkOLNj85YJ + Aw3aH8SRS/ioVzMjxpaPrjXd9oQP7+GE2k0iwL/Le3N4xFv0VuHs8g64+Ww/YthZ + zxLvKcpOru6k6J+u3gWnjGqTu4dVpeQxGs6Oft1sKwU/uCAiPEBvvLyuhPqI1Ntw + 5BVPg+Gvzl/gtRs85rFqoGfs/4pyi2jMEuq7FkTlJYANceczUqKgt0iRZZVV13sO + VMgDU7AY70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); + const n = + 18860626341786571281488823024986320858450594409825894653370543036132043241142573896042059032222842653925236802058387508300831766517210547599910735316414515960981281788934868325895562621846910598716519620622800765932541992515497122274302135166053720433521886017494719475895327062855822141735315456613329660043977602681873027347568355824008701177948209784908095092287571982104446342906805480285143028837509076748632094733769028381835899214426100089454654381383340292544225149266603150745375542089871264998183586873913491071542891188124185517417658034185267204900711933432911892469528812455677450346478816563776340692357n; + + const e = 65537n; + const d = + 675889233296421535959584534071325460876697008626327098961835826056666939623318583538247287416696388446697983317835135300723690022117970081796395234018582764098060974026823771341271141202058228435371968592820841644512299636165137501280011132877728106486853881053264668544941537620562475421678333757690909726581512579603702598150422421865490996288244127528474781858718770957987603681469839575489191876915053252047160475432784849252000744700939988583801783684487872676506769116621572018367654125765915215495461243190782913460732307866572233187464540482209385816659449697777403383908159076257721991916366849321145825505n; + const qi = + 74289828655732991195558689791424564707132679819891037249694826356870666658448134417270855397604232230569020096065966539928979441113025455296436655960522783579897346034371763410999278044050901737497683356751868229820180356905837023964589114523481066796348442538273820591008174072287844578937802915493303718997n; + assertEquals(key.e, e); + assertEquals(key.n, n); + assertEquals(key.d, d); + assertEquals(key.qi, qi); + assertEquals(key.length, 256); +});