invisal / god_crypto

Pure Javascript/Typescript Crypto Implementation for Deno. AES, RSA, HMAC, and TOTP
MIT License
93 stars 16 forks source link

Unable to decrypt ciphertext encrypted with PKCS#1 v1.5 padding #35

Open zackradisic opened 3 years ago

zackradisic commented 3 years ago

Here is the code that I am using:

import { RSA } from "./src/deps.ts";

const privateKey = new RSA(RSA.parseKey(
  new TextDecoder().decode(await Deno.readFile("./secrets/private")),
));
const encrypted = await Deno.readFile("./encrypted");
const decrypted = await privateKey.decrypt(encrypted, {
  padding: "pkcs1",
});
console.log(new TextDecoder().decode(decrypted));

And I get error: Uncaught (in promise) Decryption error. The ciphertext is encrypted with the public key using PKCS#1 v1.5 padding. To test this out for yourself, here are the public/private keys and the hex encoded cipher text (which is the encrypted string hey!):

Private key ``` -----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQChfTv2d4ghAmu21WpJApJkAzZ+8JISS7eEFq7AHVuwn1e2UKMi GKLV+T3oVe0hHcrHq6sGJmATm8sXicRBRZ5WvgMpT3CkDfLQoBxFfW+/3WVGAdCo Aq0XH2dwhHmnXlguqaq24NprBscOSz3BnQEWgGARnANFhx6ad7xhkgptyQIDAQAB AoGAYWh0Ad/p+ddG97rVaIKnoLIbbghYY7bOVFuGPxW2V9xVgQA01n07w4ua3wjt hxs8VKJk/8moDEvd/i24dpKyxbR/Ehsn1M15/VPMeBeBQ/a14/pSQt2fYkLelgax Soqu8Jz99bIO92BL6/zzbFmGh8dtd/c30t4TNCFm8iCegV0CQQDKp6pKPJTnwzP9 FdRHn0kALbsmLTrTzNnJPiURBPyRp5UoX9yaQLxi63HGMDTAfbQNV6su3+Y2+Vf2 fa4uS0tPAkEAy/+HD9z28/4Nhs+UL9dYXn81AvwOC8SvbYivlHzGBkidGynLh6wz Vi8BsAL3dJ06YGZjoyEePlsPe8qsrvKPZwJAdi20ZrpRc54oESzLsEC5R3tjqe2y YpURZAPzhatMxnvdxb+xW9rogR8gKB0whhhICYfYcetryx85JqtACgsqjQJAJW4w YQfBREm0P60s2o5fx479sht/lEc1jRqMZOl0CEkgkpBbqPw+uyyK2bnhn8MNqAyB cbgs7XIV6Dfq8XrONwJBAKntA3OCnBlz0magQmETXP2NBLihhdja4LOfYEUKBxNR 2WJ52wQPz5LNtQbLhwI0msdVutT4tGMFvoLEUQhrkHI= -----END RSA PRIVATE KEY----- ```
Public key ``` -----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChfTv2d4ghAmu21WpJApJkAzZ+ 8JISS7eEFq7AHVuwn1e2UKMiGKLV+T3oVe0hHcrHq6sGJmATm8sXicRBRZ5WvgMp T3CkDfLQoBxFfW+/3WVGAdCoAq0XH2dwhHmnXlguqaq24NprBscOSz3BnQEWgGAR nANFhx6ad7xhkgptyQIDAQAB -----END PUBLIC KEY----- ```
Hex encoded ciphertext ``` 2a9654a198ce822303c415a2d97cbabf438ce1c10f22ccefbc76bb311f03861dbbe146c990cc3fef6ba08703c71fb6265ea3aa9c9fa8100eb06c7f6c2a8c4781f7b8cb874c30ed908b5c7ed05c4598752a976c99cd482adbe502f4f968bc3e48ba1a9e6f32098cd93bf17929df45297f5c1453be3972ea0edc9f260fe2bb021d ```

I have written code in Go to decrypt it and it seems to be working. Any ideas on what could be the problem? I'm not too knowledgeable on RSA encryption so there could be something I'm missing 🤷‍♀️