android&php 加密解密

时间:2023-03-09 03:40:46
android&php 加密解密
分类: Php Android2011-08-25 11:15 556人阅读 评论(2) 收藏 举报

MyCryptActivity.java

  1. package com.test.crypt;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. public class MyCryptActivity extends Activity {
  9. /** Called when the activity is first created. */
  10. private EditText plainTextbefore, plaintextafter, ciphertext;
  11. private Button button01;
  12. private MCrypt mcrypt;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. plainTextbefore = (EditText)findViewById(R.id.EditText01);
  18. ciphertext = (EditText)findViewById(R.id.EditText02);
  19. plaintextafter = (EditText)findViewById(R.id.EditText03);
  20. button01=(Button)findViewById(R.id.Button01);
  21. plainTextbefore.setHint("请输入要加密的字符串");
  22. button01.setOnClickListener(new OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. // TODO Auto-generated method stub
  26. String plainText = plainTextbefore.getText().toString();
  27. mcrypt = new MCrypt();
  28. try {
  29. String encrypted = MCrypt.bytesToHex( mcrypt.encrypt(plainText));
  30. String decrypted = new String( mcrypt.decrypt( encrypted ) );
  31. ciphertext.setText(encrypted);
  32. plaintextafter.setText(decrypted);
  33. } catch (Exception e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. }
  38. });
  39. }
  40. }

MCrypt.java

  1. package com.test.crypt;
  2. import java.security.NoSuchAlgorithmException;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.NoSuchPaddingException;
  5. import javax.crypto.spec.IvParameterSpec;
  6. import javax.crypto.spec.SecretKeySpec;
  7. public class MCrypt {
  8. private String iv = "0123456789123456";//
  9. private IvParameterSpec ivspec;
  10. private SecretKeySpec keyspec;
  11. private Cipher cipher;
  12. private String SecretKey = "0123456789abcdef";//secretKey
  13. public MCrypt()
  14. {
  15. ivspec = new IvParameterSpec(iv.getBytes());//偏移量
  16. keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");//生成密钥
  17. try {
  18. cipher = Cipher.getInstance("AES/CBC/NoPadding");
  19. } catch (NoSuchAlgorithmException e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. } catch (NoSuchPaddingException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }
  26. }
  27. public byte[] encrypt(String text) throws Exception
  28. {
  29. if(text == null || text.length() == 0)
  30. throw new Exception("Empty string");
  31. byte[] encrypted = null;
  32. try {
  33. cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
  34. encrypted = cipher.doFinal(padString(text).getBytes());
  35. } catch (Exception e)
  36. {
  37. throw new Exception("[encrypt] " + e.getMessage());
  38. }
  39. return encrypted;
  40. }
  41. public byte[] decrypt(String code) throws Exception
  42. {
  43. if(code == null || code.length() == 0)
  44. throw new Exception("Empty string");
  45. byte[] decrypted = null;
  46. try {
  47. cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);//<span style="font-family: Simsun;font-size:16px; ">用密钥和一组算法参数初始化此 Cipher。</span>
  48. decrypted = cipher.doFinal(hexToBytes(code));
  49. } catch (Exception e)
  50. {
  51. throw new Exception("[decrypt] " + e.getMessage());
  52. }
  53. return decrypted;
  54. }
  55. public static String bytesToHex(byte[] data)
  56. {
  57. if (data==null)
  58. {
  59. return null;
  60. }
  61. int len = data.length;
  62. String str = "";
  63. for (int i=0; i<len; i++) {
  64. if ((data[i]&0xFF)<16)
  65. str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
  66. else
  67. str = str + java.lang.Integer.toHexString(data[i]&0xFF);
  68. }
  69. return str;
  70. }
  71. public static byte[] hexToBytes(String str) {
  72. if (str==null) {
  73. return null;
  74. } else if (str.length() < 2) {
  75. return null;
  76. } else {
  77. int len = str.length() / 2;
  78. byte[] buffer = new byte[len];
  79. for (int i=0; i<len; i++) {
  80. buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
  81. }
  82. return buffer;
  83. }
  84. }
  85. private static String padString(String source)
  86. {
  87. char paddingChar = ' ';
  88. int size = 16;
  89. int x = source.length() % size;
  90. int padLength = size - x;
  91. for (int i = 0; i < padLength; i++)
  92. {
  93. source += paddingChar;
  94. }
  95. return source;
  96. }
  97. }

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. <TableLayout
  7. android:id="@+id/TableLayout01"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:collapseColumns="2"
  11. android:stretchColumns="1">
  12. <TableRow
  13. android:id="@+id/TableRow01"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content">
  16. <TextView
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="明文:"></TextView>
  20. <EditText
  21. android:id="@+id/EditText01"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"></EditText>
  24. </TableRow>
  25. <TableRow
  26. android:id="@+id/TableRow02"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content">
  29. <TextView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:text="密文:"></TextView>
  33. <EditText
  34. android:id="@+id/EditText02"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"></EditText>
  37. </TableRow>
  38. <TableRow
  39. android:id="@+id/TableRow03"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content">
  42. <TextView
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:text="明文:"></TextView>
  46. <EditText
  47. android:id="@+id/EditText03"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"></EditText>
  50. </TableRow>
  51. </TableLayout>
  52. <Button
  53. android:layout_width="fill_parent"
  54. android:layout_height="wrap_content"
  55. android:id="@+id/Button01"
  56. android:text="显示密文,并解密"></Button>
  57. </LinearLayout>

android&php 加密解密

php:

  1. <?php
  2. class MCrypt
  3. {
  4. private $iv = 'fedcba9876543210'; #Same as in JAVA
  5. private $key = '0123456789abcdef'; #Same as in JAVA
  6. function __construct()
  7. {
  8. }
  9. function encrypt($str) {
  10. //$key = $this->hex2bin($key);
  11. $iv = $this->iv;
  12. $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
  13. mcrypt_generic_init($td, $this->key, $iv);
  14. $encrypted = mcrypt_generic($td, $str);
  15. mcrypt_generic_deinit($td);
  16. mcrypt_module_close($td);
  17. return bin2hex($encrypted);
  18. }
  19. function decrypt($code) {
  20. //$key = $this->hex2bin($key);
  21. $code = $this->hex2bin($code);
  22. $iv = $this->iv;
  23. $td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
  24. mcrypt_generic_init($td, $this->key, $iv);
  25. $decrypted = mdecrypt_generic($td, $code);
  26. mcrypt_generic_deinit($td);
  27. mcrypt_module_close($td);
  28. return utf8_encode(trim($decrypted));
  29. }
  30. protected function hex2bin($hexdata) {
  31. $bindata = '';
  32. for ($i = 0; $i < strlen($hexdata); $i += 2) {
  33. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  34. }
  35. return $bindata;
  36. }
  37. }