aksa-encryptory
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

AksaEncryptory

This library was generated with Angular CLI version 13.0.0. The core purpose of this library is to make encryption easier to use in any angular application.

Code scaffolding

Run npm install aksa-encryptory to install library.

Methodolgy

We are using Crypto Js, Node-Forge and Uuid as of third party libraries. For understanding of code please go through our github repository.

How to Use

In interceptor.ts file apply the following code snippet.

in constructor:

constructor(private _authService: AuthService,
                private _catchError:ErrorHandling,
                private EncryptoryService: AksaEncryptoryService
    )
    {
        this.EncryptoryService.publicKey = environment.RSA_PUBLIC_KEY
    }

In the code above, you have to declare a service object of Aksa Encryptory Service, later in constructor set value of public key basically RSA key. In our case we have declared it in Environment file you can use object of any file in which you have stored key.

this.setUser(this._authService.accessToken);
        if(environment.IsEncryption){
            if (!this._authService.accessToken) {
                var key = this.EncryptoryService.RandomKeyGenerator()
            } else {
                var key = this._authService.key;
            }
            newBody = this.EncryptoryService.AESRSAEncryption(newReq.body, key)
        }
        newReq = newReq.clone({
            body: {...newBody?newBody:newReq.body},
            headers: req.headers.set('Authorization', 'Bearer ' + this._authService.accessToken)
        });

At first, before login, it will assign a random key to our request body, when there is no token. Then in AESRSA method, will send request body and that randomly generated key. Once it will pass the login phase, then it will always use key which is returned from response we got from login request. like this:

return next.handle(newReq).pipe(
            map((data:any)=> this._catchError.transformData(data,key)),
            catchError((error) => this._catchError.onCatch(error))
        );

While Decrypting the request this will how you use AES Dcrypt method:

transformData(data:any,key:any=null){
        debugger
        if(environment.IsEncryption) {
            if (data && data.body && data.body.data) {
                if (this._authService.key) {
                    data.body = this.EncryptoryService.AESDcrypt(data.body.data, this._authService.key);
                } else {
                    data.body = this.EncryptoryService.AESDcrypt(data.body.data, key);
                }
                return data;
            }
            return data;
        }
        else{
            return data;
        }

How It will work

In Aksa Encryptory Service it will first target this method:

AESRSAEncryption(data:any,aesKey:any){
    try{
      let aesData = this.AESEncrpyt(data,aesKey);
      let publicKey = forge.pki.publicKeyFromPem(this.publicKey);
      let rsaEncrpyted = publicKey.encrypt(aesKey);
      rsaEncrpyted = btoa(rsaEncrpyted);
      return {
        key : rsaEncrpyted,
        data : aesData
      }
    }
    catch (e) {
      console.log(e)
    }
  }

When a request body enters the service, it will first target AESEncrypt Function, this function will encrypt the request. Note: RandomKeyGenerator() method will be called once only. The public key which we have defined in interceptor.ts file, will be forged through Node Forge pkg, then this key will be encrypted. later it will return in interceptor two things:

  1. An Encrypted Request
  2. An Encrypted Key

When a key is returned in response after first call, make sure that you will set that key in the value of object you assigned to public key. That key will be AES key and will be used in most cases through Crypto Js.

Further help

To get more help on this library or suggestion go check out the Github page or contact us at theapppotion@gmail.com.

Readme

Keywords

none

Package Sidebar

Install

npm i aksa-encryptory

Weekly Downloads

1

Version

0.0.1

License

none

Unpacked Size

41.6 kB

Total Files

16

Last publish

Collaborators

  • abdulrehmanaxa1