【代码】Android开发与PHP进行数据加密传输

2013年08月16日 16:01    发布者:reggae
本文介绍在Android开发中,Android开发应用如何与PHP之间进行加密传输数据,详细的代码请参考本文。
(PS:新建的QQ群,有兴趣可以加入一起讨论:Android学习交流群278744577,验证:eec
java代码:
1 mcrypt = new MCrypt();  
2 /* Encrypt */  
3 String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );  
4 /* Decrypt */  
5 String decrypted = new String( mcrypt.decrypt( encrypted ) );
PHP代码:
1 $mcrypt = new MCrypt();  
2 #Encrypt  
3 $encrypted = $mcrypt->encrypt("Text to encrypt");  
4 #Decrypt  
5 $decrypted = $mcrypt->decrypt($encrypted);
MCrypt.java代码:
1 public class MCrypt {   
2         private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)  
3         private IvParameterSpec ivspec;  
4         private SecretKeySpec keyspec;  
5         private Cipher cipher;  
6         private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)  
7         public MCrypt()  
8          {  
9             ivspec = new IvParameterSpec(iv.getBytes());  
10             keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");  
11             cipher = Cipher.getInstance("AES/CBC/NoPadding");  
12          }  
13         public byte[] encrypt(String text) throws Exception  
14          {  
15             if(text == null || text.length() == 0)  
16                  throw new Exception("Empty string");  
17              byte[] encrypted = null;  
18                  cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);  
19                  encrypted = cipher.doFinal(padString(text).getBytes());
20              return encrypted;  
21          }  
22         public byte[] decrypt(String code) throws Exception  
23         {  
24              if(code == null || code.length() == 0)  
25                  throw new Exception("Empty string");  
26              byte[] decrypted = null;  
27                  cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);  
28                 decrypted = cipher.doFinal(hexToBytes(code));
29              return decrypted;  
30          }  
31          public static String bytesToHex(byte[] data)  
32          {  
33              if (data==null)  
34              {  
35                  return null;  
36             }               
37             int len = data.length;  
38              String str = "";  
39              for (int i=0; i 40                 if ((data&0xFF)<16)  
41                     str = str + "0" + java.lang.Integer.toHexString(data&0xFF);  
42                  else  
43                      str = str + java.lang.Integer.toHexString(data&0xFF);  
44              }  
45              return str;  
46         }  
47          public static byte[] hexToBytes(String str) {  
48             if (str==null) {  
49                  return null;  
50              } else if (str.length() < 2) {  
51                 return null;  
52              } else {  
53                 int len = str.length() / 2;  
54                  byte[] buffer = new byte;  
55                  for (int i=0; i 56                      buffer = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);  
57                 }  
58                  return buffer;  
59              }  
60          }     
61
62          private static String padString(String source)  
63          {  
64            char paddingChar = ' ';  
65            int size = 16;  
66            int x = source.length() % size;  
67            int padLength = size - x;  
68           for (int i = 0; i < padLength; i++)  
69           {  
70                source += paddingChar;  
71            }  
72            return source;  
73          }  
74      }
mcrypt.php代码:
1 class MCrypt  
2 {  
3     private $iv = 'fedcba9876543210'; #Same as in JAVA  
4     private $key = '0123456789abcdef'; #Same as in JAVA     
5
6     function __construct()  
7      {  
8      }  
9     function encrypt($str) {  
10       //$key = $this->hex2bin($key);     
11        $iv = $this->iv;  
12        $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);     
13        mcrypt_generic_init($td, $this->key, $iv);  
14        $encrypted = mcrypt_generic($td, $str);  
15        mcrypt_generic_deinit($td);  
16        mcrypt_module_close($td);  
17        return bin2hex($encrypted);  
18     }  
19      function decrypt($code) {  
20        //$key = $this->hex2bin($key);  
21        $code = $this->hex2bin($code);  
22        $iv = $this->iv;  
23        $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);  
24        mcrypt_generic_init($td, $this->key, $iv);  
25        $decrypted = mdecrypt_generic($td, $code);  
26        mcrypt_generic_deinit($td);  
27        mcrypt_module_close($td);  
28        return utf8_encode(trim($decrypted));  
29      }  
30     protected function hex2bin($hexdata) {  
31        $bindata = '';     
32        for ($i = 0; $i < strlen($hexdata); $i += 2) {  
33         $bindata .= chr(hexdec(substr($hexdata, $i, 2)));  
34        }     
35        return $bindata;  
36     }  
37 }