· 9 years ago · Jan 08, 2017, 02:36 PM
1import java.security.NoSuchAlgorithmException;
2
3import javax.crypto.Cipher;
4import javax.crypto.NoSuchPaddingException;
5import javax.crypto.spec.IvParameterSpec;
6import javax.crypto.spec.SecretKeySpec;
7
8//https://github.com/serpro/Android-PHP-Encrypt-Decrypt
9
10public class MCrypt
11{
12
13 static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
14
15 private String iv = "1234567890abcdef";
16 private String SecretKey = "abcdef1234567890";
17
18 private IvParameterSpec ivspec;
19 private SecretKeySpec keyspec;
20 private Cipher cipher;
21
22 public MCrypt()
23 {
24 ivspec = new IvParameterSpec(iv.getBytes());
25 keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
26
27 try
28 {
29 cipher = Cipher.getInstance("AES/CBC/NoPadding");
30 }
31 catch (NoSuchAlgorithmException e)
32 {
33 //e.printStackTrace();
34 }
35 catch (NoSuchPaddingException e)
36 {
37 //e.printStackTrace();
38 }
39 }
40
41 public byte[] encrypt(String text) throws Exception
42 {
43 if(text == null || text.length() == 0)
44 throw new Exception("Empty string");
45
46 byte[] encrypted = null;
47
48 try
49 {
50 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
51 encrypted = cipher.doFinal(padString(text).getBytes());
52 }
53 catch (Exception e)
54 {
55 throw new Exception("[encrypt] " + e.getMessage());
56 }
57
58 return encrypted;
59 }
60
61 public byte[] decrypt(String code) throws Exception
62 {
63 if(code == null || code.length() == 0)
64 throw new Exception("Empty string");
65
66 byte[] decrypted = null;
67
68 try
69 {
70 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
71 decrypted = cipher.doFinal(hexToBytes(code));
72
73 //Remove trailing zeroes
74 if( decrypted.length > 0)
75 {
76 int trim = 0;
77 for( int i = decrypted.length - 1; i >= 0; i-- ) if( decrypted[i] == 0 ) trim++;
78
79 if( trim > 0 )
80 {
81 byte[] newArray = new byte[decrypted.length - trim];
82 System.arraycopy(decrypted, 0, newArray, 0, decrypted.length - trim);
83 decrypted = newArray;
84 }
85 }
86 }
87 catch (Exception e)
88 {
89 throw new Exception("[decrypt] " + e.getMessage());
90 }
91 return decrypted;
92 }
93
94
95 public static String bytesToHex(byte[] buf)
96 {
97 char[] chars = new char[2 * buf.length];
98 for (int i = 0; i < buf.length; ++i)
99 {
100 chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
101 chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
102 }
103 return new String(chars);
104 }
105
106
107 public static byte[] hexToBytes(String str)
108 {
109 if (str==null)
110 {
111 return null;
112 }
113 else if (str.length() < 2)
114 {
115 return null;
116 }
117 else
118 {
119 int len = str.length() / 2;
120 byte[] buffer = new byte[len];
121 for (int i=0; i<len; i++)
122 {
123 buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
124 }
125 return buffer;
126 }
127 }
128
129 private static String padString(String source)
130 {
131 char paddingChar = 0;
132 int size = 16;
133 int x = source.length() % size;
134 int padLength = size - x;
135
136 for (int i = 0; i < padLength; i++)
137 {
138 source += paddingChar;
139 }
140
141 return source;
142 }
143
144}