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

发布时间:2013-8-16 16:01    发布者:reggae
关键词: PHP , Android , 数据加密
本文介绍在Android开发中,Android开发应用如何与PHP之间进行加密传输数据,详细的代码请参考本文。
(PS:新建的QQ群,有兴趣可以加入一起讨论:Android学习交流群278744577,验证:eec
java代码:
  1. 1 mcrypt = new MCrypt();  
  2. 2 /* Encrypt */  
  3. 3 String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );  
  4. 4 /* Decrypt */  
  5. 5 String decrypted = new String( mcrypt.decrypt( encrypted ) );
复制代码

PHP代码:
  1. 1 $mcrypt = new MCrypt();  
  2. 2 #Encrypt  
  3. 3 $encrypted = $mcrypt->encrypt("Text to encrypt");  
  4. 4 #Decrypt  
  5. 5 $decrypted = $mcrypt->decrypt($encrypted);
复制代码

MCrypt.java代码:
  1. 1 public class MCrypt {   
  2. 2         private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)  
  3. 3         private IvParameterSpec ivspec;  
  4. 4         private SecretKeySpec keyspec;  
  5. 5         private Cipher cipher;  
  6. 6         private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)  
  7. 7         public MCrypt()  
  8. 8          {  
  9. 9             ivspec = new IvParameterSpec(iv.getBytes());  
  10. 10             keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");  
  11. 11             cipher = Cipher.getInstance("AES/CBC/NoPadding");  
  12. 12          }  
  13. 13         public byte[] encrypt(String text) throws Exception  
  14. 14          {  
  15. 15             if(text == null || text.length() == 0)  
  16. 16                  throw new Exception("Empty string");  
  17. 17              byte[] encrypted = null;  
  18. 18                  cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);  
  19. 19                  encrypted = cipher.doFinal(padString(text).getBytes());
  20. 20              return encrypted;  
  21. 21          }  
  22. 22         public byte[] decrypt(String code) throws Exception  
  23. 23         {  
  24. 24              if(code == null || code.length() == 0)  
  25. 25                  throw new Exception("Empty string");  
  26. 26              byte[] decrypted = null;  
  27. 27                  cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);  
  28. 28                 decrypted = cipher.doFinal(hexToBytes(code));
  29. 29              return decrypted;  
  30. 30          }  
  31. 31          public static String bytesToHex(byte[] data)  
  32. 32          {  
  33. 33              if (data==null)  
  34. 34              {  
  35. 35                  return null;  
  36. 36             }               
  37. 37             int len = data.length;  
  38. 38              String str = "";  
  39. 39              for (int i=0; i
  40. 40                 if ((data[i]&0xFF)<16)  
  41. 41                     str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);  
  42. 42                  else  
  43. 43                      str = str + java.lang.Integer.toHexString(data[i]&0xFF);  
  44. 44              }  
  45. 45              return str;  
  46. 46         }  
  47. 47          public static byte[] hexToBytes(String str) {  
  48. 48             if (str==null) {  
  49. 49                  return null;  
  50. 50              } else if (str.length() < 2) {  
  51. 51                 return null;  
  52. 52              } else {  
  53. 53                 int len = str.length() / 2;  
  54. 54                  byte[] buffer = new byte[len];  
  55. 55                  for (int i=0; i
  56. 56                      buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);  
  57. 57                 }  
  58. 58                  return buffer;  
  59. 59              }  
  60. 60          }     
  61. 61
  62. 62          private static String padString(String source)  
  63. 63          {  
  64. 64            char paddingChar = ' ';  
  65. 65            int size = 16;  
  66. 66            int x = source.length() % size;  
  67. 67            int padLength = size - x;  
  68. 68           for (int i = 0; i < padLength; i++)  
  69. 69           {  
  70. 70                source += paddingChar;  
  71. 71            }  
  72. 72            return source;  
  73. 73          }  
  74. 74      }
复制代码

mcrypt.php代码:
  1. 1 class MCrypt  
  2. 2 {  
  3. 3     private $iv = 'fedcba9876543210'; #Same as in JAVA  
  4. 4     private $key = '0123456789abcdef'; #Same as in JAVA     
  5. 5
  6. 6     function __construct()  
  7. 7      {  
  8. 8      }  
  9. 9     function encrypt($str) {  
  10. 10       //$key = $this->hex2bin($key);     
  11. 11        $iv = $this->iv;  
  12. 12        $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);     
  13. 13        mcrypt_generic_init($td, $this->key, $iv);  
  14. 14        $encrypted = mcrypt_generic($td, $str);  
  15. 15        mcrypt_generic_deinit($td);  
  16. 16        mcrypt_module_close($td);  
  17. 17        return bin2hex($encrypted);  
  18. 18     }  
  19. 19      function decrypt($code) {  
  20. 20        //$key = $this->hex2bin($key);  
  21. 21        $code = $this->hex2bin($code);  
  22. 22        $iv = $this->iv;  
  23. 23        $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);  
  24. 24        mcrypt_generic_init($td, $this->key, $iv);  
  25. 25        $decrypted = mdecrypt_generic($td, $code);  
  26. 26        mcrypt_generic_deinit($td);  
  27. 27        mcrypt_module_close($td);  
  28. 28        return utf8_encode(trim($decrypted));  
  29. 29      }  
  30. 30     protected function hex2bin($hexdata) {  
  31. 31        $bindata = '';     
  32. 32        for ($i = 0; $i < strlen($hexdata); $i += 2) {  
  33. 33         $bindata .= chr(hexdec(substr($hexdata, $i, 2)));  
  34. 34        }     
  35. 35        return $bindata;  
  36. 36     }  
  37. 37 }
复制代码

本文地址:https://www.eechina.com/thread-119606-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

相关视频

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表