android&php 加密解密
分類: Php Android
2011-08-25 11:15
556人閱讀
收藏
舉報
MyCryptActivity.javaphp
- package com.test.crypt;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
-
- public class MyCryptActivity extends Activity {
-
- private EditText plainTextbefore, plaintextafter, ciphertext;
- private Button button01;
- private MCrypt mcrypt;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- plainTextbefore = (EditText)findViewById(R.id.EditText01);
- ciphertext = (EditText)findViewById(R.id.EditText02);
- plaintextafter = (EditText)findViewById(R.id.EditText03);
- button01=(Button)findViewById(R.id.Button01);
- plainTextbefore.setHint("請輸入要加密的字符串");
- button01.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
-
- String plainText = plainTextbefore.getText().toString();
- mcrypt = new MCrypt();
- try {
- String encrypted = MCrypt.bytesToHex( mcrypt.encrypt(plainText));
- String decrypted = new String( mcrypt.decrypt( encrypted ) );
- ciphertext.setText(encrypted);
- plaintextafter.setText(decrypted);
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- }
- });
- }
- }
MCrypt.java
main.xmljava
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
-
- <TableLayout
- android:id="@+id/TableLayout01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:collapseColumns="2"
- android:stretchColumns="1">
- <TableRow
- android:id="@+id/TableRow01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="明文:"></TextView>
- <EditText
- android:id="@+id/EditText01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></EditText>
- </TableRow>
-
- <TableRow
- android:id="@+id/TableRow02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密文:"></TextView>
- <EditText
- android:id="@+id/EditText02"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></EditText>
- </TableRow>
- <TableRow
- android:id="@+id/TableRow03"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="明文:"></TextView>
- <EditText
- android:id="@+id/EditText03"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"></EditText>
- </TableRow>
- </TableLayout>
-
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/Button01"
- android:text="顯示密文,並解密"></Button>
- </LinearLayout>
php:android
- <?php
-
- class MCrypt
- {
- private $iv = 'fedcba9876543210'; #Same as in JAVA
- private $key = '0123456789abcdef'; #Same as in JAVA
-
-
- function __construct()
- {
- }
-
- function encrypt($str) {
-
-
- $iv = $this->iv;
-
- $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
-
- mcrypt_generic_init($td, $this->key, $iv);
- $encrypted = mcrypt_generic($td, $str);
-
- mcrypt_generic_deinit($td);
- mcrypt_module_close($td);
-
- return bin2hex($encrypted);
- }
-
- function decrypt($code) {
-
- $code = $this->hex2bin($code);
- $iv = $this->iv;
-
- $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
-
- mcrypt_generic_init($td, $this->key, $iv);
- $decrypted = mdecrypt_generic($td, $code);
-
- mcrypt_generic_deinit($td);
- mcrypt_module_close($td);
-
- return utf8_encode(trim($decrypted));
- }
-
- protected function hex2bin($hexdata) {
- $bindata = '';
-
- for ($i = 0; $i < strlen($hexdata); $i += 2) {
- $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
- }
-
- return $bindata;
- }
-
- }
歡迎關注本站公眾號,獲取更多信息