RSA Encrypt/Decrypt in TypeScript

23,098

Finally found a way, after installed some.

npm install buffer
npm install crypto-browserify

Then use it

import {config} from "../app.config";
import {Buffer} from 'buffer/';
import * as crypto from "crypto-browserify";

export class RsaService {
  private privateKey: string;
  private publicKey: string;
  private enabled: boolean;

  constructor() {
    this.privateKey = config.authentication.rsa.privateKey;
    this.publicKey = config.authentication.rsa.publicKey;
    this.enabled = config.authentication.rsa.enabled;
  }

  isEnabled(): boolean {
    return this.enabled;
  }

  encrypt(plaintext: string): string {
    if (!this.enabled)
      return plaintext;

    let buffer = new Buffer(plaintext);
    let encrypted = crypto.privateEncrypt(this.privateKey, buffer);

    return encrypted.toString('base64');
  }

  decrypt(cypher: string): string {
    if (!this.enabled)
      return cypher;

    let buffer = Buffer.from(cypher, 'base64');
    let plaintext = crypto.publicDecrypt(this.publicKey, buffer);

    return plaintext.toString('utf8')
  }
}
Share:
23,098
Romeortec
Author by

Romeortec

Updated on July 09, 2022

Comments

  • Romeortec
    Romeortec almost 2 years

    I'm using Angular 4 to make the front end of my application. I have implemented OAuth2 on my backend (developed with Spring in Java), so people using my application must be authenticated.

    The thing is that we can see clearly the passwords from the backend server logs and it could be caught by a MITM until I add a SSL.

    That's why I decided to encrypt the sent password with RSA. My backend is already ready, but I don't find any up-to-date libraries that provide a decent API for encrypt/decrypt from a RSA key-pair.

    Also seen crypto module, but no longer usable on ECMAS6. The crypto-js one only provides AES and some hashing such as MD5/SHA.

  • jonrsharpe
    jonrsharpe over 6 years
    "create your own encryption method" - that is awful advice. See e.g. security.stackexchange.com/q/18197/72084
  • Selva
    Selva about 6 years
    ,I am trying to use your solution in my project. I am getting cannot read property of 2 null in below line let encrypted = crypto.privateEncrypt(this.privateKey, buffer).can you please help on this?
  • Romeortec
    Romeortec about 6 years
    crypto is null, you didnt npm install buffer npm install crypto-browserify
  • Devaffair
    Devaffair about 6 years
    No point in creating your own encryption when so many are out there and doing fantastic work. Creating your own is for lazy developers who don't wish to learn a new langauge/library/feature
  • Devaffair
    Devaffair about 6 years
    @Romeortec I'm experiencing the same issue. Both libraries were installed properly.
  • Devaffair
    Devaffair about 6 years
    @Madhesh Did you figure this out?
  • Romeortec
    Romeortec about 6 years
    You have to install buffer and crypto-browser as I wrote
  • Selva
    Selva about 6 years
    @Devaffair, I used the CryptoJS and it worked for me.