<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    【代碼】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 }
    復制代碼

    本文地址:http://www.portaltwn.com/thread-119606-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷