国产毛片a精品毛-国产毛片黄片-国产毛片久久国产-国产毛片久久精品-青娱乐极品在线-青娱乐精品

【代碼】Android開(kāi)發(fā)與PHP進(jìn)行數(shù)據(jù)加密傳輸

發(fā)布時(shí)間:2013-8-16 16:01    發(fā)布者:reggae
關(guān)鍵詞: PHP , Android , 數(shù)據(jù)加密
本文介紹在Android開(kāi)發(fā)中,Android開(kāi)發(fā)應(yīng)用如何與PHP之間進(jìn)行加密傳輸數(shù)據(jù),詳細(xì)的代碼請(qǐng)參考本文。
(PS:新建的QQ群,有興趣可以加入一起討論:Android學(xué)習(xí)交流群278744577,驗(yàn)證: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 ) );
復(fù)制代碼

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);
復(fù)制代碼

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      }
復(fù)制代碼

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 }
復(fù)制代碼

本文地址:http://www.qingdxww.cn/thread-119606-1-1.html     【打印本頁(yè)】

本站部分文章為轉(zhuǎn)載或網(wǎng)友發(fā)布,目的在于傳遞和分享信息,并不代表本網(wǎng)贊同其觀點(diǎn)和對(duì)其真實(shí)性負(fù)責(zé);文章版權(quán)歸原作者及原出處所有,如涉及作品內(nèi)容、版權(quán)和其它問(wèn)題,我們將根據(jù)著作權(quán)人的要求,第一時(shí)間更正或刪除。
您需要登錄后才可以發(fā)表評(píng)論 登錄 | 立即注冊(cè)

廠商推薦

  • Microchip視頻專區(qū)
  • 安靜高效的電機(jī)控制——這才是正確的方向!
  • 無(wú)線充電基礎(chǔ)知識(shí)及應(yīng)用培訓(xùn)教程2
  • 了解一下Microchip強(qiáng)大的PIC18-Q24 MCU系列
  • 5分鐘詳解定時(shí)器/計(jì)數(shù)器E和波形擴(kuò)展!
  • 貿(mào)澤電子(Mouser)專區(qū)

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權(quán)所有   京ICP備16069177號(hào) | 京公網(wǎng)安備11010502021702
快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: porn 国产 | 国产wwwww| 亚洲欧美视频二区 | 日韩一区二区三区免费体验 | 正在播放亚洲一区 | 亚洲色视频在线播放网站 | 国产精品免费视频能看 | 狠狠色婷婷丁香综合久久韩国 | 亚洲欧美另类在线观看 | 99久久精品免费看国产麻豆 | 中文字幕在线看视频一区二区三区 | 国产香蕉一区二区在线网站 | 成人欧美视频免费看黄黄 | 久久精品国产99国产精品小说 | 成年人免费黄色片 | 免费毛片网站在线观看 | 久久国产影视 | 国产中文字幕免费 | 亚洲一区第一页 | 国产三级全黄在线观看 | 亚洲成人aa| 男男视频免费在线观看 | 国产三级一区 | 五月天最新网站 | 国产精品免费精品自在线观看 | 精品国产福利在线观看一区 | 亚洲日日干 | 92精品国产自产在线 | www.riben| 亚洲激情自拍偷拍 | 色香婷婷| 老子影院午夜精品欧美视频 | 亚洲色图综合在线 | 国内自拍视频网站 | 国产一区二区在线播放 | 国产精品久久久久久影院 | 国产视频www| 致命弯道8在线观看高清免费观看 | 欧美地区一二三区 | 久久艹视频| 成人午夜视频免费看欧美 |