· 7 years ago · Jun 25, 2018, 02:34 PM
1//
2// Source code recreated from a .class file by IntelliJ IDEA
3// (powered by Fernflower decompiler)
4//
5
6package com.wallet.pwd.trustapp;
7
8import android.content.Context;
9import android.content.SharedPreferences;
10import android.content.SharedPreferences.Editor;
11import android.preference.PreferenceManager;
12import android.util.Base64;
13import android.util.Log;
14import java.io.UnsupportedEncodingException;
15import java.security.InvalidAlgorithmParameterException;
16import java.security.InvalidKeyException;
17import java.security.NoSuchAlgorithmException;
18import java.security.SecureRandom;
19import java.security.spec.InvalidKeySpecException;
20import java.security.spec.KeySpec;
21import javax.crypto.BadPaddingException;
22import javax.crypto.Cipher;
23import javax.crypto.IllegalBlockSizeException;
24import javax.crypto.KeyGenerator;
25import javax.crypto.NoSuchPaddingException;
26import javax.crypto.SecretKey;
27import javax.crypto.SecretKeyFactory;
28import javax.crypto.spec.IvParameterSpec;
29import javax.crypto.spec.PBEKeySpec;
30import javax.crypto.spec.SecretKeySpec;
31
32public final class PasswordManager {
33 private static final String legacyKey = "35TheTru5tWa11ets3cr3tK3y377123!";
34 private static final String legacyIv = "8201va0184a0md8i";
35
36 public PasswordManager() {
37 }
38
39 public static native String getKeyStringFromNative();
40
41 public static native String getIvStringFromNative();
42
43 public static void setPasswordLegacy(String address, String password, Context context) throws NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, InvalidKeySpecException, InvalidAlgorithmParameterException {
44 SecretKey key = new SecretKeySpec("35TheTru5tWa11ets3cr3tK3y377123!".getBytes("UTF-8"), "AES");
45 IvParameterSpec iv = new IvParameterSpec("8201va0184a0md8i".getBytes("UTF-8"));
46 byte[] encryptedPassword = encrypt(password, key, iv);
47 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
48 Editor editor = sharedPreferences.edit();
49 editor.putString(address + "-pwd", Base64.encodeToString(encryptedPassword, 0));
50 editor.commit();
51 }
52
53 public static void setPassword(String address, String password, Context context) throws NoSuchPaddingException, BadPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, InvalidKeySpecException, InvalidAlgorithmParameterException {
54 SecretKey key = new SecretKeySpec(getKeyStringFromNative().getBytes("UTF-8"), "AES");
55 IvParameterSpec iv = new IvParameterSpec(getIvStringFromNative().getBytes("UTF-8"));
56 byte[] encryptedPassword = encrypt(password, key, iv);
57 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
58 Editor editor = sharedPreferences.edit();
59 editor.putString(address + "-pwd", Base64.encodeToString(encryptedPassword, 0));
60 editor.commit();
61 }
62
63 public static String getPassword(String address, Context context) throws NoSuchPaddingException, UnsupportedEncodingException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, InvalidKeySpecException, InvalidAlgorithmParameterException {
64 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
65 byte[] encryptedPassword = Base64.decode(sharedPreferences.getString(address + "-pwd", (String)null), 0);
66 SecretKey oldKey = new SecretKeySpec("35TheTru5tWa11ets3cr3tK3y377123!".getBytes("UTF-8"), "AES");
67 IvParameterSpec oldIv = new IvParameterSpec("8201va0184a0md8i".getBytes("UTF-8"));
68
69 try {
70 String decryptedPassword = decrypt(encryptedPassword, oldKey, oldIv);
71 return decryptedPassword;
72 } catch (Exception var9) {
73 Log.e("PASSMAN", var9.getMessage());
74 SecretKey key = new SecretKeySpec(getKeyStringFromNative().getBytes("UTF-8"), "AES");
75 IvParameterSpec iv = new IvParameterSpec(getIvStringFromNative().getBytes("UTF-8"));
76 String decryptedPassword = decrypt(encryptedPassword, key, iv);
77 return decryptedPassword;
78 }
79 }
80
81 private static byte[] encrypt(String plainText, SecretKey key, IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {
82 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
83 cipher.init(1, key, iv);
84 return cipher.doFinal(plainText.getBytes("UTF-8"));
85 }
86
87 private static String decrypt(byte[] cipherText, SecretKey key, IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidAlgorithmParameterException {
88 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
89 cipher.init(2, key, iv);
90 return new String(cipher.doFinal(cipherText), "UTF-8");
91 }
92
93 private static IvParameterSpec generateRandomIv() {
94 SecureRandom random = new SecureRandom();
95 byte[] ivBytes = new byte[16];
96 random.nextBytes(ivBytes);
97 IvParameterSpec iv = new IvParameterSpec(ivBytes);
98 return iv;
99 }
100
101 private static SecretKey generateKey() throws NoSuchAlgorithmException {
102 int outputKeyLength = true;
103 SecureRandom secureRandom = new SecureRandom();
104 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
105 keyGenerator.init(256, secureRandom);
106 SecretKey key = keyGenerator.generateKey();
107 return key;
108 }
109
110 private static SecretKey generateKey(String keyPhrase) throws NoSuchAlgorithmException, InvalidKeySpecException {
111 SecureRandom random = new SecureRandom();
112 byte[] salt = new byte[16];
113 random.nextBytes(salt);
114 KeySpec spec = new PBEKeySpec(keyPhrase.toCharArray(), salt, 65536, 256);
115 SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
116 SecretKey key = keyFac.generateSecret(spec);
117 return key;
118 }
119
120 static {
121 System.loadLibrary("native-lib");
122 }
123}