1

I am trouble, how to do encryption/decyption using crypto js in ionic4?

I have written code for encryption decryption in java

public static String encrypt(String str) 
{
    String encryptedString = str;

    try {
        byte[] utf8 = str.getBytes("UTF8");
        byte[] enc = encryptor.doFinal(utf8);
        encryptedString = Base64.encodeBase64URLSafeString(enc);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encryptedString;
}

For decryption

public static String decrypt(String str) 
{
    String decryptedString = "";        
    try {
        byte[] dec = Base64.decodeBase64(str);
        byte[] utf8 = decryptor.doFinal(dec);
        decryptedString = new String(utf8, "UTF8");
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return decryptedString;
}

Secrete key generation

public static void genKeyPair(int i)    {
    try {
        // generates DES key from string //
        key = new SecretKeySpec(Skey.getBytes(), "DES");

        // initialize the cipher with key //
        encryptor = Cipher.getInstance("DES");
        decryptor = Cipher.getInstance("DES");
        encryptor.init(Cipher.ENCRYPT_MODE, key);
        decryptor.init(Cipher.DECRYPT_MODE, key);

        System.err.println(java.util.Base64.getEncoder().encodeToString(key.getEncoded()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I want to write same thing in ionic4, how to do plz help me. I am new in ionic4(Hybrid application)

Nitin Karale
  • 789
  • 3
  • 12
  • 34

2 Answers2

0

Your code looks like custom encryption/decryption,You can use one of the many encryption methods like from crypto-js like:

Its pretty simple, here is sample code to go-ahead :

import * as utf8 from 'crypto-js/enc-utf8';
import * as AES from 'crypto-js/aes';

// Encryption
AES.encrypt(JSON.stringify(data),your_encKey).toString();

// Decryption
AES.decrypt(userdata, your_encKey).toString(utf8);

For More : READ HERE

I think this code snippet is enough reference to go ahead and will help you to convert your code with ionic4/angular.

Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
0

I found solution in angular (IONIC 4) , but sign characters are differs from java code

let key="AND$VBOA";
var keyHex = CryptoJS.enc.Utf8.parse(key);
var message = CryptoJS.enc.Utf8.parse(message);
var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
console.log("Encrypted:="+encrypted.toString());

for decryped

var decrypted = CryptoJS.DES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(encrypted)
}, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
console.log("Derypted:="+decrypted .toString());

In ionic

Ecrypted: 6qPw1YkOopA=
Decrypted: Nitin

In Java

Ecrypted: 6qPw1YkOopA
Decrypted: Nitin

In Ionic

Ecrypted: oOmH6uQ4R/Q=
Decrypted: Message

In Java

Ecrypted: oOmH6uQ4R_Q
Decrypted: Message

In Ionic

Ecrypted: f+eTKAqlUwXnznPVyjTXWA==
Decrypted: engineer

In Java

Ecrypted: f-eTKAqlUwXnznPVyjTXWA
Decrypted: engineer

So many example tested in Java "-" & in IONIC "+" & in ionic "/" & java "_", In ionic extra = or == are comming. Then whats wrong in this sample code, Plz suggest me.

Nitin Karale
  • 789
  • 3
  • 12
  • 34