Android 和 PHP 之间进行数据加密传输

[代码][Java]代码

1 mcrypt =newMCrypt();
2 /* Encrypt */
3 String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );
4 /* Decrypt */
5 String decrypted =newString( mcrypt.decrypt( encrypted ) );

[代码][PHP]代码

1 $mcrypt=newMCrypt();
2 #Encrypt
3 $encrypted=$mcrypt->encrypt("Text to encrypt");
4 #Decrypt
5 $decrypted=$mcrypt->decrypt($encrypted);

[代码]MCrypt.java

001 /***********/
002 /**JAVA**/
003
004 importjava.security.NoSuchAlgorithmException;
005
006 importjavax.crypto.Cipher;
007 importjavax.crypto.NoSuchPaddingException;
008 importjavax.crypto.spec.IvParameterSpec;
009 importjavax.crypto.spec.SecretKeySpec;
010
011 publicclassMCrypt {
012
013 privateString iv ="fedcba9876543210";//Dummy iv (CHANGE IT!)
014 privateIvParameterSpec ivspec;
015 privateSecretKeySpec keyspec;
016 privateCipher cipher;
017
018 privateString SecretKey ="0123456789abcdef";//Dummy secretKey (CHANGE IT!)
019
020 publicMCrypt()
021 {
022 ivspec =newIvParameterSpec(iv.getBytes());
023
024 keyspec =newSecretKeySpec(SecretKey.getBytes(),"AES");
025
026 try{
027 cipher = Cipher.getInstance("AES/CBC/NoPadding");
028 }catch(NoSuchAlgorithmException e) {
029 // TODO Auto-generated catch block
030 e.printStackTrace();
031 }catch(NoSuchPaddingException e) {
032 // TODO Auto-generated catch block
033 e.printStackTrace();
034 }
035 }
036
037 publicbyte[] encrypt(String text)throwsException
038 {
039 if(text ==null|| text.length() ==0)
040 thrownewException("Empty string");
041
042 byte[] encrypted =null;
043
044 try{
045 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
046
047 encrypted = cipher.doFinal(padString(text).getBytes());
048 }catch(Exception e)
049 {
050 thrownewException("[encrypt] "+ e.getMessage());
051 }
052
053 returnencrypted;
054 }
055
056 publicbyte[] decrypt(String code)throwsException
057 {
058 if(code ==null|| code.length() ==0)
059 thrownewException("Empty string");
060
061 byte[] decrypted =null;
062
063 try{
064 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
065
066 decrypted = cipher.doFinal(hexToBytes(code));
067 }catch(Exception e)
068 {
069 thrownewException("[decrypt] "+ e.getMessage());
070 }
071 returndecrypted;
072 }
073
074
075
076 publicstaticString bytesToHex(byte[] data)
077 {
078 if(data==null)
079 {
080 returnnull;
081 }
082
083 intlen = data.length;
084 String str ="";
085 for(inti=0; i<len; i++) {
086 if((data[i]&amp;0xFF)&lt;16)
087 str = str +"0"+ java.lang.Integer.toHexString(data[i]&amp;0xFF);
088 else
089 str = str + java.lang.Integer.toHexString(data[i]&amp;0xFF);
090 }
091 returnstr;
092 }
093
094
095 publicstaticbyte[] hexToBytes(String str) {
096 if(str==null) {
097 returnnull;
098 }elseif(str.length() &lt;2) {
099 returnnull;
100 }else{
101 intlen = str.length() /2;
102 byte[] buffer =newbyte[len];
103 for(inti=0; i&lt;len; i++) {
104 buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
105 }
106 returnbuffer;
107 }
108 }
109
110
111
112 privatestaticString padString(String source)
113 {
114 charpaddingChar =' ';
115 intsize =16;
116 intx = source.length() % size;
117 intpadLength = size - x;
118
119 for(inti =0; i &lt; padLength; i++)
120 {
121 source += paddingChar;
122 }
123
124 returnsource;
125 }
126 }

[代码]mcrypt.php

01 /**********/
02 /**PHP**/
03
04 &lt;?php
05
06 classMCrypt
07 {
08 private$iv='fedcba9876543210'; #Sameasin JAVA
09 private$key='0123456789abcdef'; #Sameasin JAVA
10
11
12 function__construct()
13 {
14 }
15
16 functionencrypt($str) {
17
18 //$key = $this->hex2bin($key);
19 $iv=$this-&gt;iv;
20
21 $td= mcrypt_module_open('rijndael-128','','cbc',$iv);
22
23 mcrypt_generic_init($td,$this-&gt;key,$iv);
24 $encrypted= mcrypt_generic($td,$str);
25
26 mcrypt_generic_deinit($td);
27 mcrypt_module_close($td);
28
29 returnbin2hex($encrypted);
30 }
31
32 functiondecrypt($code) {
33 //$key = $this-&gt;hex2bin($key);
34 $code=$this-&gt;hex2bin($code);
35 $iv=$this-&gt;iv;
36
37 $td= mcrypt_module_open('rijndael-128','','cbc',$iv);
38
39 mcrypt_generic_init($td,$this-&gt;key,$iv);
40 $decrypted= mdecrypt_generic($td,$code);
41
42 mcrypt_generic_deinit($td);
43 mcrypt_module_close($td);
44
45 returnutf8_encode(trim($decrypted));
46 }
47
48 protectedfunctionhex2bin($hexdata) {
49 $bindata='';
50
51 for($i= 0;$i&lt;strlen($hexdata);$i+= 2) {
52 $bindata.=chr(hexdec(substr($hexdata,$i, 2)));
53 }
54
55 return$bindata;
56 }
57
58 }
59 // see http://androidsnippets.com/encrypt-decrypt-between-android-and-php

更多相关文章

  1. Android下自定义IP控件
  2. Android在开机时自动启动一个应用程序
  3. android全屏,去掉title栏的办法
  4. Android计算地图上两点距离
  5. 工具:Android本地代码生成器
  6. Android热更新方案Robust——美团热更新(热修复)使用介绍
  7. Android很有用的代码片段
  8. SeekBar自定义
  9. android NDK环境搭建

随机推荐

  1. 用于 Android 智能手机的 Android Networ
  2. Android(安卓)拍照:如何使用已有相机应用
  3. Android应用程序线程消息循环模型分析(1)
  4. 开机自动运行程序【Android】
  5. 最新的数据显示,十分之四的 Android 用户
  6. Android中布局的巧妙设计【android进化二
  7. Android开发第1-1课:创建一个Android工程
  8. Android牛博
  9. 针对Android的Crash监控, 崩溃分析---推
  10. android基本理解