本文共 4683 字,大约阅读时间需要 15 分钟。
Android 和 PHP 之间进行数据加密传输
[代码] [Java]代码
3 | String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") ); |
5 | String decrypted = new String( mcrypt.decrypt( encrypted ) ); |
[代码] [PHP]代码
1 | $mcrypt = new MCrypt(); |
3 | $encrypted = $mcrypt->encrypt("Text to encrypt"); |
5 | $decrypted = $mcrypt->decrypt($encrypted); |
[代码] MCrypt.java
004 | import java.security.NoSuchAlgorithmException; |
006 | import javax.crypto.Cipher; |
007 | import javax.crypto.NoSuchPaddingException; |
008 | import javax.crypto.spec.IvParameterSpec; |
009 | import javax.crypto.spec.SecretKeySpec; |
011 | public class MCrypt { |
013 | private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!) |
014 | private IvParameterSpec ivspec; |
015 | private SecretKeySpec keyspec; |
016 | private Cipher cipher; |
018 | private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!) |
022 | ivspec = new IvParameterSpec(iv.getBytes()); |
024 | keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES"); |
027 | cipher = Cipher.getInstance("AES/CBC/NoPadding"); |
028 | } catch (NoSuchAlgorithmException e) { |
029 | // TODO Auto-generated catch block |
031 | } catch (NoSuchPaddingException e) { |
032 | // TODO Auto-generated catch block |
037 | public byte[] encrypt(String text) throws Exception |
039 | if(text == null || text.length() == 0) |
040 | throw new Exception("Empty string"); |
042 | byte[] encrypted = null; |
045 | cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); |
047 | encrypted = cipher.doFinal(padString(text).getBytes()); |
048 | } catch (Exception e) |
050 | throw new Exception("[encrypt] " + e.getMessage()); |
056 | public byte[] decrypt(String code) throws Exception |
058 | if(code == null || code.length() == 0) |
059 | throw new Exception("Empty string"); |
061 | byte[] decrypted = null; |
064 | cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); |
066 | decrypted = cipher.doFinal(hexToBytes(code)); |
067 | } catch (Exception e) |
069 | throw new Exception("[decrypt] " + e.getMessage()); |
076 | public static String bytesToHex(byte[] data) |
083 | int len = data.length; |
085 | for (int i=0; i<len; i++) { |
086 | if ((data[i]&0xFF)<16) |
087 | str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF); |
089 | str = str + java.lang.Integer.toHexString(data[i]&0xFF); |
095 | public static byte[] hexToBytes(String str) { |
098 | } else if (str.length() < 2) { |
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); |
112 | private static String padString(String source) |
114 | char paddingChar = ' '; |
116 | int x = source.length() % size; |
117 | int padLength = size - x; |
119 | for (int i = 0; i < padLength; i++) |
121 | source += paddingChar; |
[代码] mcrypt.php
08 | private $iv = 'fedcba9876543210'; #Same as in JAVA |
09 | private $key = '0123456789abcdef'; #Same as in JAVA |
12 | function __construct() |
16 | function encrypt($str) { |
18 | //$key = $this->hex2bin($key); |
21 | $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv); |
23 | mcrypt_generic_init($td, $this->key, $iv); |
24 | $encrypted = mcrypt_generic($td, $str); |
26 | mcrypt_generic_deinit($td); |
27 | mcrypt_module_close($td); |
29 | return bin2hex($encrypted); |
32 | function decrypt($code) { |
33 | //$key = $this->hex2bin($key); |
34 | $code = $this->hex2bin($code); |
37 | $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv); |
39 | mcrypt_generic_init($td, $this->key, $iv); |
40 | $decrypted = mdecrypt_generic($td, $code); |
42 | mcrypt_generic_deinit($td); |
43 | mcrypt_module_close($td); |
45 | return utf8_encode(trim($decrypted)); |
48 | protected function hex2bin($hexdata) { |
51 | for ($i = 0; $i < strlen($hexdata); $i += 2) { |
52 | $bindata .= chr(hexdec(substr($hexdata, $i, 2))); |
59 | // see http://androidsnippets.com/encrypt-decrypt-between-android-and-php |
转载于:https://www.cnblogs.com/xiaochao1234/p/3792878.html