zoukankan      html  css  js  c++  java
  • android&php 加密解密

    from://http://blog.csdn.net/hecker385/article/details/6717647

     

    android&php 加密解密

    分类: Php Android 556人阅读 评论(2) 收藏 举报

    MyCryptActivity.java

    1. package com.test.crypt;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.view.View;  
    6. import android.view.View.OnClickListener;  
    7. import android.widget.Button;  
    8. import android.widget.EditText;  
    9.   
    10. public class MyCryptActivity extends Activity {  
    11.     /** Called when the activity is first created. */  
    12.     private EditText plainTextbefore, plaintextafter, ciphertext;  
    13.     private Button button01;  
    14.     private MCrypt mcrypt;  
    15.     @Override  
    16.     public void onCreate(Bundle savedInstanceState) {  
    17.         super.onCreate(savedInstanceState);  
    18.         setContentView(R.layout.main);  
    19.           
    20.         plainTextbefore = (EditText)findViewById(R.id.EditText01);  
    21.         ciphertext = (EditText)findViewById(R.id.EditText02);  
    22.         plaintextafter = (EditText)findViewById(R.id.EditText03);  
    23.         button01=(Button)findViewById(R.id.Button01);  
    24.         plainTextbefore.setHint("请输入要加密的字符串");  
    25.         button01.setOnClickListener(new OnClickListener() {  
    26.             @Override  
    27.             public void onClick(View v) {  
    28.                 // TODO Auto-generated method stub  
    29.                 String plainText = plainTextbefore.getText().toString();  
    30.                 mcrypt = new MCrypt();  
    31.                 try {  
    32.                     String encrypted = MCrypt.bytesToHex( mcrypt.encrypt(plainText));  
    33.                     String decrypted = new String( mcrypt.decrypt( encrypted ) );  
    34.                     ciphertext.setText(encrypted);  
    35.                     plaintextafter.setText(decrypted);  
    36.                 } catch (Exception e) {  
    37.                     // TODO Auto-generated catch block  
    38.                     e.printStackTrace();  
    39.                 }  
    40.             }  
    41.         });  
    42.     }  
    43. }  
    MCrypt.java
    1. package com.test.crypt;  
    2.   
    3. import java.security.NoSuchAlgorithmException;  
    4.   
    5. import javax.crypto.Cipher;  
    6. import javax.crypto.NoSuchPaddingException;  
    7. import javax.crypto.spec.IvParameterSpec;  
    8. import javax.crypto.spec.SecretKeySpec;  
    9.   
    10. public class MCrypt {  
    11.   
    12.     private String iv = "0123456789123456";//  
    13.     private IvParameterSpec ivspec;  
    14.     private SecretKeySpec keyspec;  
    15.     private Cipher cipher;  
    16.       
    17.     private String SecretKey = "0123456789abcdef";//secretKey  
    18.       
    19.     public MCrypt()  
    20.     {  
    21.             ivspec = new IvParameterSpec(iv.getBytes());//偏移量  
    22.   
    23.             keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");//生成密钥  
    24.               
    25.             try {  
    26.                     cipher = Cipher.getInstance("AES/CBC/NoPadding");  
    27.             } catch (NoSuchAlgorithmException e) {  
    28.                     // TODO Auto-generated catch block  
    29.                     e.printStackTrace();  
    30.             } catch (NoSuchPaddingException e) {  
    31.                     // TODO Auto-generated catch block  
    32.                     e.printStackTrace();  
    33.             }  
    34.     }  
    35.       
    36.     public byte[] encrypt(String text) throws Exception  
    37.     {  
    38.             if(text == null || text.length() == 0)  
    39.                     throw new Exception("Empty string");  
    40.               
    41.             byte[] encrypted = null;  
    42.   
    43.             try {  
    44.                     cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);  
    45.   
    46.                     encrypted = cipher.doFinal(padString(text).getBytes());  
    47.             } catch (Exception e)  
    48.             {                         
    49.                     throw new Exception("[encrypt] " + e.getMessage());  
    50.             }  
    51.               
    52.             return encrypted;  
    53.     }  
    54.       
    55.     public byte[] decrypt(String code) throws Exception  
    56.     {  
    57.             if(code == null || code.length() == 0)  
    58.                     throw new Exception("Empty string");  
    59.               
    60.             byte[] decrypted = null;  
    61.   
    62.             try {  
    63.                     cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);//<span style="font-family: Simsun;font-size:16px; ">用密钥和一组算法参数初始化此 Cipher。</span>  
    64.                       
    65.                     decrypted = cipher.doFinal(hexToBytes(code));  
    66.             } catch (Exception e)  
    67.             {  
    68.                     throw new Exception("[decrypt] " + e.getMessage());  
    69.             }  
    70.             return decrypted;  
    71.     }  
    72.       
    73.   
    74.       
    75.     public static String bytesToHex(byte[] data)  
    76.     {  
    77.             if (data==null)  
    78.             {  
    79.                     return null;  
    80.             }  
    81.               
    82.             int len = data.length;  
    83.             String str = "";  
    84.             for (int i=0; i<len; i++) {  
    85.                     if ((data[i]&0xFF)<16)  
    86.                             str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);  
    87.                     else  
    88.                             str = str + java.lang.Integer.toHexString(data[i]&0xFF);  
    89.             }  
    90.             return str;  
    91.     }  
    92.       
    93.               
    94.     public static byte[] hexToBytes(String str) {  
    95.             if (str==null) {  
    96.                     return null;  
    97.             } else if (str.length() < 2) {  
    98.                     return null;  
    99.             } else {  
    100.                     int len = str.length() / 2;  
    101.                     byte[] buffer = new byte[len];  
    102.                     for (int i=0; i<len; i++) {  
    103.                             buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);  
    104.                     }  
    105.                     return buffer;  
    106.             }  
    107.     }  
    108.       
    109.       
    110.   
    111.     private static String padString(String source)  
    112.     {  
    113.       char paddingChar = ' ';  
    114.       int size = 16;  
    115.       int x = source.length() % size;  
    116.       int padLength = size - x;  
    117.   
    118.       for (int i = 0; i < padLength; i++)  
    119.       {  
    120.               source += paddingChar;  
    121.       }  
    122.   
    123.       return source;  
    124.     }  
    125. }  

    main.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:orientation="vertical"  
    4.     android:layout_width="fill_parent"  
    5.     android:layout_height="fill_parent">  
    6.       
    7. <TableLayout  
    8. android:id="@+id/TableLayout01"  
    9. android:layout_width="fill_parent"  
    10. android:layout_height="wrap_content"  
    11. android:collapseColumns="2"  
    12. android:stretchColumns="1">  
    13.     <TableRow  
    14.     android:id="@+id/TableRow01"  
    15.     android:layout_width="wrap_content"  
    16.     android:layout_height="wrap_content">  
    17.         <TextView    
    18.             android:layout_width="fill_parent"   
    19.             android:layout_height="wrap_content"   
    20.             android:text="明文:"></TextView>  
    21.         <EditText  
    22.             android:id="@+id/EditText01"  
    23.             android:layout_width="wrap_content"  
    24.             android:layout_height="wrap_content"></EditText>  
    25.     </TableRow>     
    26.       
    27.     <TableRow  
    28.     android:id="@+id/TableRow02"  
    29.     android:layout_width="wrap_content"  
    30.     android:layout_height="wrap_content">  
    31.         <TextView    
    32.             android:layout_width="wrap_content"   
    33.             android:layout_height="wrap_content"   
    34.             android:text="密文:"></TextView>  
    35.         <EditText  
    36.             android:id="@+id/EditText02"  
    37.             android:layout_width="wrap_content"  
    38.             android:layout_height="wrap_content"></EditText>  
    39.     </TableRow>  
    40.     <TableRow  
    41.     android:id="@+id/TableRow03"  
    42.     android:layout_width="wrap_content"  
    43.     android:layout_height="wrap_content">  
    44.         <TextView    
    45.             android:layout_width="wrap_content"   
    46.             android:layout_height="wrap_content"   
    47.             android:text="明文:"></TextView>  
    48.         <EditText  
    49.             android:id="@+id/EditText03"  
    50.             android:layout_width="wrap_content"  
    51.             android:layout_height="wrap_content"></EditText>  
    52.     </TableRow>  
    53. </TableLayout>  
    54.   
    55.     <Button    
    56.         android:layout_width="fill_parent"   
    57.         android:layout_height="wrap_content"   
    58.         android:id="@+id/Button01"  
    59.         android:text="显示密文,并解密"></Button>  
    60. </LinearLayout>  

    php:

     

    1. <?php   
    2.   
    3. class MCrypt  
    4. {  
    5.         private $iv = 'fedcba9876543210'; #Same as in JAVA  
    6.         private $key = '0123456789abcdef'; #Same as in JAVA  
    7.   
    8.   
    9.         function __construct()  
    10.         {  
    11.         }  
    12.   
    13.         function encrypt($str) {  
    14.   
    15.           //$key = $this->hex2bin($key);      
    16.           $iv = $this->iv;  
    17.   
    18.           $td = mcrypt_module_open('rijndael-128''''cbc'$iv);  
    19.   
    20.           mcrypt_generic_init($td$this->key, $iv);  
    21.           $encrypted = mcrypt_generic($td$str);  
    22.   
    23.           mcrypt_generic_deinit($td);  
    24.           mcrypt_module_close($td);  
    25.   
    26.           return bin2hex($encrypted);  
    27.         }  
    28.   
    29.         function decrypt($code) {  
    30.           //$key = $this->hex2bin($key);  
    31.           $code = $this->hex2bin($code);  
    32.           $iv = $this->iv;  
    33.   
    34.           $td = mcrypt_module_open('rijndael-128''''cbc'$iv);  
    35.   
    36.           mcrypt_generic_init($td$this->key, $iv);  
    37.           $decrypted = mdecrypt_generic($td$code);  
    38.   
    39.           mcrypt_generic_deinit($td);  
    40.           mcrypt_module_close($td);  
    41.   
    42.           return utf8_encode(trim($decrypted));  
    43.         }  
    44.   
    45.         protected function hex2bin($hexdata) {  
    46.           $bindata = '';  
    47.   
    48.           for ($i = 0; $i < strlen($hexdata); $i += 2) {  
    49.                 $bindata .= chr(hexdec(substr($hexdata$i, 2)));  
    50.           }  
    51.   
    52.           return $bindata;  
    53.         }  
    54.   
    55. }  
  • 相关阅读:
    (转)大型网站架构演变和知识体系
    最小生成树-Prim算法和Kruskal算法
    最短路径——Floyd-Warshall算法
    SpringBootService,一个基于spring boot搭建的SOA服务框架
    Netty(六)UDP在netty中的使用
    Netty(五)序列化protobuf在netty中的使用
    Netty(四)分隔符与定长解码器的使用
    Netty(三)TCP粘包拆包处理
    Netty(二)入门
    Netty(一)引题
  • 原文地址:https://www.cnblogs.com/wanqieddy/p/3530010.html
Copyright © 2011-2022 走看看