欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新

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

来源: 开发者 投稿于  被查看 25045 次 评论:126

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


1    mcrypt = 2    
3    String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt"4    
5    String decrypted = 1    $mcrypt = 23    $encrypted = $mcrypt->encrypt("Text to encrypt"45    $decrypted = $mcrypt->001    
002    
003     
004        005     
006        007        008        009        010     
011         012     
013             String iv = "fedcba9876543210";
014            015            016            017            
018             String SecretKey = "0123456789abcdef";
019            
020            021022                ivspec = 023     
024                keyspec =  SecretKeySpec(SecretKey.getBytes(), "AES"025                
026                027                    cipher = Cipher.getInstance("AES/CBC/NoPadding"028                } 029                    
030031                } 032                    
033034035036            
037             [] encrypt(String text) 038039                (text ==  || text.length() == 0040                      Exception("Empty string"041                
042                [] encrypted = 043     
044                045046     
047                    encrypted =048                } 049050                      Exception("[encrypt] " +051052                
053                054055            
056             [] decrypt(String code) 057058                (code ==  || code.length() == 0059                      Exception("Empty string"060                
061                [] decrypted = 062     
063                064065                    
066                    decrypted =067                } 068069                      Exception("[decrypt] " +070071                072073            
074     
075            
076              String bytesToHex(077078                 (data==079080                     081082                
083                 len =084                String str = ""085                 ( i=0; i<len; i++086                     ((data[i]&amp;0xFF)&lt;16087                        str = str + "0" + java.lang.Integer.toHexString(data[i]&amp;0xFF088                    
089                        str = str + java.lang.Integer.toHexString(data[i]&amp;0xFF090091                092093            
094                
095              096                 (str==097                     098                }   (str.length() &lt; 2099                     100                } 101                     len = str.length() / 2102                    [] buffer =  103                     ( i=0; i&lt;len; i++104                        buffer[i] = () Integer.parseInt(str.substring(i*2,i*2+2),16105106                    107108109            
110            
111     
112             113114               paddingChar = ' '115               size = 16116               x = source.length() %117               padLength = size -118     
119               ( i = 0; i &lt; padLength; i++120121                  source +=122123     
124              12512601    
02    
03     
04    &lt;?05     
06    0708         $iv = 'fedcba9876543210'09         $key = '0123456789abcdef'10     
11     
12131415     
1617     
18          
19          $iv = $-&20     
21          $td = mcrypt_module_open('rijndael-128', '', 'cbc'22     
23          mcrypt_generic_init($td, $-&24          $encrypted =25     
262728     
29          3031     
3233          
34          $code = $-&35          $iv = $-&36     
37          $td = mcrypt_module_open('rijndael-128', '', 'cbc'38     
39          mcrypt_generic_init($td, $-&40          $decrypted =41     
424344     
45          4647     
48        49          $bindata = ''50     
51           ($i = 0; $i &lt; strlen($hexdata); $i += 252            $bindata .= chr(hexdec(substr($hexdata, $i, 25354     
55          5657     
5859    

 

用户评论