· 5 years ago · Apr 23, 2020, 08:30 PM
1/***********/
2/**JAVA**/
3
4 import java.security.NoSuchAlgorithmException;
5
6 import javax.crypto.Cipher;
7 import javax.crypto.NoSuchPaddingException;
8 import javax.crypto.spec.IvParameterSpec;
9 import javax.crypto.spec.SecretKeySpec;
10
11 public class MCrypt {
12
13 private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)
14 private IvParameterSpec ivspec;
15 private SecretKeySpec keyspec;
16 private Cipher cipher;
17
18 private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
19
20 public MCrypt()
21 {
22 ivspec = new IvParameterSpec(iv.getBytes());
23
24 keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
25
26 try {
27 cipher = Cipher.getInstance("AES/CBC/NoPadding");
28 } catch (NoSuchAlgorithmException e) {
29 // TODO Auto-generated catch block
30 e.printStackTrace();
31 } catch (NoSuchPaddingException e) {
32 // TODO Auto-generated catch block
33 e.printStackTrace();
34 }
35 }
36
37 public byte[] encrypt(String text) throws Exception
38 {
39 if(text == null || text.length() == 0)
40 throw new Exception("Empty string");
41
42 byte[] encrypted = null;
43
44 try {
45 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
46
47 encrypted = cipher.doFinal(padString(text).getBytes());
48 } catch (Exception e)
49 {
50 throw new Exception("[encrypt] " + e.getMessage());
51 }
52
53 return encrypted;
54 }
55
56 public byte[] decrypt(String code) throws Exception
57 {
58 if(code == null || code.length() == 0)
59 throw new Exception("Empty string");
60
61 byte[] decrypted = null;
62
63 try {
64 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
65
66 decrypted = cipher.doFinal(hexToBytes(code));
67 } catch (Exception e)
68 {
69 throw new Exception("[decrypt] " + e.getMessage());
70 }
71 return decrypted;
72 }
73
74
75
76 public static String bytesToHex(byte[] data)
77 {
78 if (data==null)
79 {
80 return null;
81 }
82
83 int len = data.length;
84 String str = "";
85 for (int i=0; i<len; i++) {
86 if ((data[i]&0xFF)<16)
87 str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
88 else
89 str = str + java.lang.Integer.toHexString(data[i]&0xFF);
90 }
91 return str;
92 }
93
94
95 public static byte[] hexToBytes(String str) {
96 if (str==null) {
97 return null;
98 } else if (str.length() < 2) {
99 return null;
100 } else {
101 int len = str.length() / 2;
102 byte[] buffer = new byte[len];
103 for (int i=0; i<len; i++) {
104 buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
105 }
106 return buffer;
107 }
108 }
109
110
111
112 private static String padString(String source)
113 {
114 char paddingChar = ' ';
115 int size = 16;
116 int x = source.length() % size;
117 int padLength = size - x;
118
119 for (int i = 0; i < padLength; i++)
120 {
121 source += paddingChar;
122 }
123
124 return source;
125 }
126 }
127
128
129
130====================================================
131
132/**********/
133/**PHP**/
134
135 <?php
136
137 class MCrypt
138 {
139 private $iv = 'fedcba9876543210'; #Same as in JAVA
140 private $key = '0123456789abcdef'; #Same as in JAVA
141
142
143 function __construct()
144 {
145 }
146
147 function encrypt($str) {
148
149 //$key = $this->hex2bin($key);
150 $iv = $this->iv;
151
152 $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
153
154 mcrypt_generic_init($td, $this->key, $iv);
155 $encrypted = mcrypt_generic($td, $str);
156
157 mcrypt_generic_deinit($td);
158 mcrypt_module_close($td);
159
160 return bin2hex($encrypted);
161 }
162
163 function decrypt($code) {
164 //$key = $this->hex2bin($key);
165 $code = $this->hex2bin($code);
166 $iv = $this->iv;
167
168 $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
169
170 mcrypt_generic_init($td, $this->key, $iv);
171 $decrypted = mdecrypt_generic($td, $code);
172
173 mcrypt_generic_deinit($td);
174 mcrypt_module_close($td);
175
176 return utf8_encode(trim($decrypted));
177 }
178
179 protected function hex2bin($hexdata) {
180 $bindata = '';
181
182 for ($i = 0; $i < strlen($hexdata); $i += 2) {
183 $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
184 }
185
186 return $bindata;
187 }
188
189 }