· 5 years ago · Mar 17, 2020, 09:48 AM
1import java.util.*;
2import javax.crypto.spec.SecretKeySpec;
3import javax.crypto.spec.IvParameterSpec;
4import javax.crypto.SecretKeyFactory;
5import javax.crypto.Cipher;
6import javax.crypto.spec.PBEKeySpec;
7import java.security.spec.KeySpec;
8import javax.crypto.SecretKey;
9
10 public class Main{
11 public static String secretKey = "HIPPOKEY";
12 public static String salt = "HIPPOSALT";
13
14 public static void main(String []args){
15 String s ="HippoInnovations|8FRx1lUJD8|store";
16 System.out.println(encrypt(s));
17 }
18
19
20
21public static String encrypt(String strToEncrypt)
22{
23 try
24 {
25
26 byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
27 IvParameterSpec ivspec = new IvParameterSpec(iv);
28
29 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
30 KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
31 System.out.println("KEYSPEC spec "+spec);
32 SecretKey tmp = factory.generateSecret(spec);
33 SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
34
35 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
36 cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
37 return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
38 }
39 catch (Exception e)
40 {
41 System.out.println("Error while encrypting: " + e.toString());
42 }
43 return null;
44}
45}