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