· 9 years ago · Nov 24, 2016, 01:46 PM
1package pl.ready4s.extafreenew.utils;
2
3
4
5import android.util.Log;
6
7
8import java.io.UnsupportedEncodingException;
9import java.math.BigInteger;
10
11import javax.crypto.Cipher;
12import javax.crypto.spec.SecretKeySpec;
13
14/**
15 * Created by belhaver on 24.11.16.
16 */
17
18public class EncryptDecryptUtils {
19
20 final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
21 private static final String KEY = "7E152B16AEA6D228AB99157E4709CF3C";
22
23 public static String encrypt(String password) {
24 byte[] passwordByteArray = getByteArray(password);
25// byte[] passwordByteArray = password.getBytes();
26 try {
27 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
28 byte[] tmp = hexStringToByteArray(KEY);
29 SecretKeySpec secretKeySpec = new SecretKeySpec(hexStringToByteArray(KEY), "AES");
30
31
32 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
33 byte[] cipherText = cipher.doFinal(passwordByteArray);
34 String encryptedString = bytesArrayToHexString(cipherText);
35// String encryptedString = Base64.encodeToString(cipherText, Base64.NO_PADDING);
36
37 Log.d(EncryptDecryptUtils.class.getSimpleName(), "Encrypted password: " + encryptedString);
38 return encryptedString;
39 } catch (Exception e) {
40 e.printStackTrace();
41 }
42 return "";
43 }
44
45 public static String decrypt(String encryptedPassword) {
46 try {
47 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
48 SecretKeySpec secretKey = new SecretKeySpec(hexStringToByteArray(KEY), "AES");
49 cipher.init(Cipher.DECRYPT_MODE, secretKey);
50 byte[] cipherText = hexStringToByteArray(encryptedPassword);
51 String decryptedString = new String(cipher.doFinal(cipherText),"UTF-8");
52 Log.d(EncryptDecryptUtils.class.getSimpleName(), "Before decryption: " + encryptedPassword);
53 Log.d(EncryptDecryptUtils.class.getSimpleName(), "Decrypted password: " + decryptedString);
54 return decryptedString;
55 } catch (Exception e) {
56 e.printStackTrace();
57 }
58 return null;
59 }
60
61 private static byte[] getByteArray(String password) {
62 byte[] passwordByteArray = new byte[32];
63 for (int i = 0; i < password.length(); i++) {
64 try {
65 passwordByteArray[i] = password.getBytes("UTF-8")[i];
66 } catch (UnsupportedEncodingException e) {
67 e.printStackTrace();
68 }
69 }
70 return passwordByteArray;
71 }
72
73 private static byte[] hexStringToByteArray(String hexString) {
74 byte[] b = new BigInteger(hexString,16).toByteArray();
75// int len = hexString.length();
76// byte[] data = new byte[len / 2];
77// for (int i = 0; i < len; i += 2) {
78// try {
79// data[i / 2] = ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i+1), 16));
80// } catch (IndexOutOfBoundsException e) {
81// break;
82// }
83// }
84 return b;
85 }
86
87 private static String bytesArrayToHexString(byte[] byteArray) {
88 char[] hexChars = new char[byteArray.length * 2];
89 for ( int j = 0; j < byteArray.length; j++ ) {
90 try {
91 int v = byteArray[j] & 0xFF;
92 hexChars[j * 2] = hexArray[v >>> 4];
93 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
94 } catch (IndexOutOfBoundsException e) {
95 break;
96 }
97 }
98 return new String(hexChars);
99 }
100}