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

[#172078979] Add encrypt/decrypt AES/RSA utility #319

Merged
merged 3 commits into from
Apr 16, 2020

Conversation

AleDore
Copy link
Contributor

@AleDore AleDore commented Apr 16, 2020

Added utility functions to provide hybrid AES/RSA encrypt/decrypt.

@gunzip gunzip changed the title [#172078979] - add encrypt/decrypt AES/RSA utility [#172078979] Add encrypt/decrypt AES/RSA utility Apr 16, 2020
Copy link
Contributor

@gunzip gunzip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm apart minor changes

src/encrypt.ts Show resolved Hide resolved
src/encrypt.ts Outdated Show resolved Hide resolved
src/encrypt.ts Outdated Show resolved Hide resolved
src/encrypt.ts Outdated Show resolved Hide resolved
src/encrypt.ts Outdated Show resolved Hide resolved
describe("encrypt", () => {
it("should encrypt and decrypt a string with combination AES/RSA", async () => {
const encryptedPayload = toEncryptedPayload(rsaPublicKey, aTextToEncrypt);
expect(encryptedPayload).toHaveProperty("encryptedOutput");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(encryptedPayload).toHaveProperty("encryptedOutput");
expect(encryptedPayload).toHaveProperty("encryptedPayload");

src/encrypt.ts Outdated
} as EncryptTuple;
}

export function hybridDecrypt(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I use ecrypt or decrypt function with wrong keys pair, I will get an exception.
For example this code raise an exception:

const encryptedPayload = toEncryptedPayload(rsaPublicKey, aTextToEncrypt);
    expect(encryptedPayload).toHaveProperty("encryptedPayload");
    const decryptedString = fromEncryptedPayload(
      rsaPrivateKey + "x", //wrong private key
      encryptedPayload
    );

Could this implementation be more error friendly?

export function fromEncryptedPayload(
  rsaPrivateKey: string,
  encryptedPayload: EncryptedPayload
): Either<Error, string> {
  return tryCatch(
    () => {
      const iv = Buffer.from(encryptedPayload.iv, "base64");
      const aesKey = crypto.privateDecrypt(
        rsaPrivateKey,
        Buffer.from(encryptedPayload.encryptedKey, "base64")
      );
      const decipher = crypto.createDecipheriv(
        "aes-128-cbc",
        Buffer.from(aesKey),
        iv
      );
      const decrypted = decipher.update(
        Buffer.from(encryptedPayload.encryptedPayload, "base64")
      );
      const output = Buffer.concat([decrypted, decipher.final()]);
      return output.toString("utf-8");
    },
    (e: unknown) => (e instanceof Error ? e : new Error(String(e)))
  );
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a very good point, probably yes give me a moment :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@gunzip gunzip merged commit 3bc18a5 into master Apr 16, 2020
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

Successfully merging this pull request may close these issues.

3 participants