Encrypt text using cryptojs library in angular2

I use the cryptojs library to encrypt the password text and send data to the server. But I get the following error message:

Error TS2304: Cannot find name 'CryptoJS'. 

I installed the library as follows: sudo npm install crypto-js

import 'crypto-js/crypto-js';
@Page({
  templateUrl: 'build/pages/login/login.html'
})
export class LoginPage {
  constructor(private nav: NavController, public http: Http) {

  }    

  validateUser(fieldval, data){
    let params = "{'ignoreAuthModule': 'ignoreAuthModule'}";
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Parameter',  params);
    let url = 'app/authentication';
    let encrypted_password = Cryptojs.AES.encrypt(fieldval.password, CryptoJS.enc.Base64.parse(data.seed),{mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }).toString().replace(/\+/g, "-").replace(/\//g, '_');

    let requestoptions = new RequestOptions({
      method: RequestMethod.Post,
      url: url,
      headers: this.headers,
      body: JSON.stringify(data)
    })  

    let body = JSON.stringify(params); 

    this.http.post(url, body, headers)
      .subscribe(
        response => {          
        },
        error => {
          console.log(error.text());
        }
      );        
  }            
  logError(err) {
    console.error('There was an error: ' + err);
  }
}
+4
source share
1 answer

I do not know where this compilation error occurs, but you must set the typification for the crypto-js library:

typings install --ambient crypto-js

See this question for more information:

+2
source

All Articles