title: Encryption in Typescript ( Angular ) and Decryption in Java
body: <p>I am currently using Angular7. 
  I have Java encryption, decryption code implemented with me given by client, same I need to in Angular.</p>
  
  <p>Below is code for Java.</p>
  
  <pre><code>import java.security.SecureRandom;
  import java.security.spec.KeySpec;
  import java.util.Base64;
  
  import javax.crypto.Cipher;
  import javax.crypto.KeyGenerator;
  import javax.crypto.SecretKey;
  import javax.crypto.SecretKeyFactory;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.PBEKeySpec;
  import javax.crypto.spec.SecretKeySpec;
  </code></pre>
  
  <p>Inside Class I have</p>
  
  <pre><code>class EnDc(String passPhrase)
  {
      private static final byte[] SALT = { 8 random numbers between -127 to 127 };
      private static final int ITERATION_COUNT = 1000;
      private static final int KEY_LENGTH = 256;
      private static final int IV_LENGTH = 16;
      private Cipher eCipher;
      private Cipher dCipher;
      private byte[] encrypt;
      private byte[] iv;
  </code></pre>
  
  <p>Generation of hashed key and iv is as below:</p>
  
  <pre><code>    SecretKeyFactory secretKeyFactory = 
              SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
      KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), SALT, 
              1000, 256);
  </code></pre>
  
  <p>They have generated temp secret key with keyspec as follows :</p>
  
  <pre><code>    secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
  </code></pre>
  
  <p>Generated new secret key with temp key and with "AES"</p>
  
  <pre><code>    secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), //encode 
              "AES");
  </code></pre>
  
  <p>Next Steps :</p>
  
  <pre><code>    this.eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
      this.eCipher.init(1, secretKey);
      // 1 - ENCRYPT_MODE, 2 - DECRYPT_MODE
  </code></pre>
  
  <p>IV generation : </p>
  
  <pre><code>    this.iv = ((IvParameterSpec)this.eCipher.getParameters().getParameterSpec(IvParameterSpec.class)).getIV();
  
  }
  </code></pre>
  
  <p>Actual Encrypt function as below</p>
  
  <pre><code>public String encrypt(String encrypt)
        {
          String encStr = null;
          try
          {
            byte[] bytes = encrypt.getBytes("UTF8");
            byte[] encrypted = encrypt(bytes);
            byte[] cipherText = new byte[encrypted.length + this.iv.length];
            System.arraycopy(this.iv, 0, cipherText, 0, this.iv.length);
            System.arraycopy(encrypted, 0, cipherText, this.iv.length, encrypted.length);
            encStr = new String(Base64.getEncoder().encode(cipherText));
          }
          catch (Exception ex)
          {
            ex.printStackTrace();
          }
          return encStr;
        }
  </code></pre>
  
  <p>I am trying to implement this in Service file in Angular, Here as per my understanding here SALT byte array is predefined and IV is random which is appended before encrypted string and extracted while decryption,</p>
  
  <p>So JS way I tried as below</p>
  
  <pre><code>import * as CryptoJS from 'crypto-js';
  
  encrypt (msg, pass) {
    const salt = CryptoJS.lib.WordArray.random(128 / 8);
  
    const key = CryptoJS.PBKDF2( pass, salt, {
        keySize: this.keySize / 32,
        iterations: this.iterations
    });
  
    const iv =  CryptoJS.lib.WordArray.random(128 / 8);
    const encrypted = CryptoJS.AES.encrypt(msg, key, {
      iv: iv,
      padding: CryptoJS.pad.Pkcs7,
      mode: CryptoJS.mode.CBC
    });
  
    const transitmessage = salt.toString() + iv.toString() + encrypted.toString();
    return transitmessage;
  }
  </code></pre>
  
  <p>So, I have done this code from link <a href="http://www.adonespitogo.com/articles/encrypting-data-with-cryptojs-aes/" rel="nofollow noreferrer">http://www.adonespitogo.com/articles/encrypting-data-with-cryptojs-aes/</a> , so now question is how can I implement SALT which is hard coded in JAVA, and also inside JAVA code they have not sent salt key attached with Encrypted String, but IV only as first 16 bits. How can I implement this ?</p>
  
  <p>I have tried to do like this</p>
  
  <pre><code>salt = CryptoJS.lib.WordArray.create([-67, -85, 13, -28, 75, 112, -126, 103]);
  </code></pre>
  
  <p>but key getting generated is object and also I am getting error while decrypting</p>
  
  <p>Edit 1: I want salt to be same each time which I am currently generating as random and attaching to encrypted string. </p>
  
  <p>I don't know if there is some implementation of SecretKeyFactory and Keyspec for Javascript</p>
  
  <p>Help Appreciated, Thanks in Advance. </p>