免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1251 | 回复: 1
打印 上一主题 下一主题

[Android] Android 和 PHP 之间进行数据加密传输 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-13 11:17 |只看该作者 |倒序浏览
代码
  1. mcrypt = new MCrypt();
  2. /* Encrypt */
  3. String encrypted = MCrypt.bytesToHex( mcrypt.encrypt("Text to Encrypt") );
  4. /* Decrypt */
  5. String decrypted = new String( mcrypt.decrypt( encrypted ) );
复制代码
代码
  1. $mcrypt = new MCrypt();
  2. #Encrypt
  3. $encrypted = $mcrypt->encrypt("Text to encrypt");
  4. #Decrypt
  5. $decrypted = $mcrypt->decrypt($encrypted);
复制代码
MCrypt.java
  1. /***********/
  2. /**JAVA**/

  3.     import java.security.NoSuchAlgorithmException;

  4.     import javax.crypto.Cipher;
  5.     import javax.crypto.NoSuchPaddingException;
  6.     import javax.crypto.spec.IvParameterSpec;
  7.     import javax.crypto.spec.SecretKeySpec;

  8.     public class MCrypt {

  9.         private String iv = "fedcba9876543210";//Dummy iv (CHANGE IT!)
  10.         private IvParameterSpec ivspec;
  11.         private SecretKeySpec keyspec;
  12.         private Cipher cipher;
  13.          
  14.         private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
  15.          
  16.         public MCrypt()
  17.         {
  18.             ivspec = new IvParameterSpec(iv.getBytes());

  19.             keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");
  20.             
  21.             try {
  22.                 cipher = Cipher.getInstance("AES/CBC/NoPadding");
  23.             } catch (NoSuchAlgorithmException e) {
  24.                 // TODO Auto-generated catch block
  25.                 e.printStackTrace();
  26.             } catch (NoSuchPaddingException e) {
  27.                 // TODO Auto-generated catch block
  28.                 e.printStackTrace();
  29.             }
  30.         }
  31.          
  32.         public byte[] encrypt(String text) throws Exception
  33.         {
  34.             if(text == null || text.length() == 0)
  35.                 throw new Exception("Empty string");
  36.             
  37.             byte[] encrypted = null;

  38.             try {
  39.                 cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

  40.                 encrypted = cipher.doFinal(padString(text).getBytes());
  41.             } catch (Exception e)
  42.             {           
  43.                 throw new Exception("[encrypt] " + e.getMessage());
  44.             }
  45.             
  46.             return encrypted;
  47.         }
  48.          
  49.         public byte[] decrypt(String code) throws Exception
  50.         {
  51.             if(code == null || code.length() == 0)
  52.                 throw new Exception("Empty string");
  53.             
  54.             byte[] decrypted = null;

  55.             try {
  56.                 cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
  57.                  
  58.                 decrypted = cipher.doFinal(hexToBytes(code));
  59.             } catch (Exception e)
  60.             {
  61.                 throw new Exception("[decrypt] " + e.getMessage());
  62.             }
  63.             return decrypted;
  64.         }
  65.          

  66.          
  67.         public static String bytesToHex(byte[] data)
  68.         {
  69.             if (data==null)
  70.             {
  71.                 return null;
  72.             }
  73.             
  74.             int len = data.length;
  75.             String str = "";
  76.             for (int i=0; i<len; i++) {
  77.                 if ((data[i]&amp;0xFF)&lt;16)
  78.                     str = str + "0" + java.lang.Integer.toHexString(data[i]&amp;0xFF);
  79.                 else
  80.                     str = str + java.lang.Integer.toHexString(data[i]&amp;0xFF);
  81.             }
  82.             return str;
  83.         }
  84.          
  85.             
  86.         public static byte[] hexToBytes(String str) {
  87.             if (str==null) {
  88.                 return null;
  89.             } else if (str.length() &lt; 2) {
  90.                 return null;
  91.             } else {
  92.                 int len = str.length() / 2;
  93.                 byte[] buffer = new byte[len];
  94.                 for (int i=0; i&lt;len; i++) {
  95.                     buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
  96.                 }
  97.                 return buffer;
  98.             }
  99.         }
  100.          
  101.          

  102.         private static String padString(String source)
  103.         {
  104.           char paddingChar = ' ';
  105.           int size = 16;
  106.           int x = source.length() % size;
  107.           int padLength = size - x;

  108.           for (int i = 0; i &lt; padLength; i++)
  109.           {
  110.               source += paddingChar;
  111.           }

  112.           return source;
  113.         }
  114.     }
复制代码
mcrypt.php
  1. /**********/
  2. /**PHP**/

  3. &lt;?php

  4. class MCrypt
  5. {
  6.     private $iv = 'fedcba9876543210'; #Same as in JAVA
  7.     private $key = '0123456789abcdef'; #Same as in JAVA


  8.     function __construct()
  9.     {
  10.     }

  11.     function encrypt($str) {

  12.       //$key = $this->hex2bin($key);   
  13.       $iv = $this-&gt;iv;

  14.       $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

  15.       mcrypt_generic_init($td, $this-&gt;key, $iv);
  16.       $encrypted = mcrypt_generic($td, $str);

  17.       mcrypt_generic_deinit($td);
  18.       mcrypt_module_close($td);

  19.       return bin2hex($encrypted);
  20.     }

  21.     function decrypt($code) {
  22.       //$key = $this-&gt;hex2bin($key);
  23.       $code = $this-&gt;hex2bin($code);
  24.       $iv = $this-&gt;iv;

  25.       $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);

  26.       mcrypt_generic_init($td, $this-&gt;key, $iv);
  27.       $decrypted = mdecrypt_generic($td, $code);

  28.       mcrypt_generic_deinit($td);
  29.       mcrypt_module_close($td);

  30.       return utf8_encode(trim($decrypted));
  31.     }

  32.     protected function hex2bin($hexdata) {
  33.       $bindata = '';

  34.       for ($i = 0; $i &lt; strlen($hexdata); $i += 2) {
  35.         $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  36.       }

  37.       return $bindata;
  38.     }

  39. }
  40. // see http://androidsnippets.com/encrypt-decrypt-between-android-and-php
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
2 [报告]
发表于 2015-07-14 10:13 |只看该作者
真是好东西 谢谢分享。楼主有没有什么办法能把 String iv = "fedcba9876543210"  和 SecretKey = "0123456789abcdef"  加密一下呢?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP