· 7 years ago · Apr 18, 2018, 10:22 PM
1package com.noisyz.bittrexclient;
2
3import android.util.Base64;
4
5import java.nio.charset.StandardCharsets;
6import java.security.Key;
7
8import javax.crypto.Cipher;
9import javax.crypto.SecretKeyFactory;
10import javax.crypto.spec.IvParameterSpec;
11import javax.crypto.spec.PBEKeySpec;
12
13public class Encryptor {
14
15 private static final String PASSWORD_HASH = "9h@LLaVR", SALT_KEY = "58Y#@Afc", VI_KEY = "2rsAG$4!74z@P$dH";
16
17 public static String encrypt(String value) {
18 try {
19 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
20 PBEKeySpec pbeKeySpec = new PBEKeySpec(PASSWORD_HASH.toCharArray(), SALT_KEY.getBytes(StandardCharsets.US_ASCII), 1000, 256);
21 Key secretKey = factory.generateSecret(pbeKeySpec);
22
23
24 IvParameterSpec ivSpec = new IvParameterSpec(VI_KEY.getBytes(StandardCharsets.US_ASCII));
25
26 Cipher cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
27 cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);
28 byte[] encrypted = cipher.doFinal(value.getBytes());
29 Log.d("myLogs", "encrypted string: "
30 + Base64.encodeToString(encrypted, 0));
31 return Base64.encodeToString(encrypted, 0);
32 } catch (Exception ex) {
33 ex.printStackTrace();
34 }
35
36 return null;
37 }
38
39 public static String decrypt(String value) {
40 try {
41 byte[] encrypted = Base64.decode(value, 0);
42 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
43 PBEKeySpec pbeKeySpec = new PBEKeySpec(PASSWORD_HASH.toCharArray(), SALT_KEY.getBytes(StandardCharsets.US_ASCII), 1000, 256);
44 Key secretKey = factory.generateSecret(pbeKeySpec);
45
46 IvParameterSpec ivSpec = new IvParameterSpec(VI_KEY.getBytes(StandardCharsets.US_ASCII));
47
48 Cipher cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
49 cipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
50
51 byte[] decrypted = cipher.doFinal(encrypted);
52 return new String(decrypted, StandardCharsets.UTF_8);
53 } catch (Exception ex) {
54 ex.printStackTrace();
55 }
56
57 return null;
58 }
59}