Java 如何使用android解密php加密字符串?

Java 如何使用android解密php加密字符串?,java,php,android,encryption,Java,Php,Android,Encryption,嗨,我想解密我的php加密字符串。我的代码是 使用php.java import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.NameValuePair; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.os.Bun

嗨,我想解密我的php加密字符串。我的代码是

使用php.java

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class UsingPHP extends Activity {
    TextView encrypt_txt1, encrypt_txt2, decrypt_txt1, decrypt_txt2;
    Button decrypt_but;
    String original_value = "";
    String encrypted_value = "";
    byte[] byteArray1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.decrypt);

        encrypt_txt1 = (TextView) findViewById(R.id.entv1);
        encrypt_txt2 = (TextView) findViewById(R.id.entv2);
        decrypt_txt1 = (TextView) findViewById(R.id.decrytv1);
        decrypt_txt2 = (TextView) findViewById(R.id.decrytv2);
        decrypt_but = (Button) findViewById(R.id.decrybt);

        decrypt_but.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {
                try {

                    ArrayList < NameValuePair > postParameters = new ArrayList < NameValuePair > ();
                    String response = null;

                    response = CustomHttpClient.executeHttpPost(
                        "http://10.0.2.2/cyrpt/encrypt.php",
                    postParameters);
                    String res = response.toString();
                    System.out.println("HTTP Response comes here.......");
                    System.out.println(res);

                    JSONArray jArray = new JSONArray(res);
                    System.out.println("JSON Array created.....");

                    JSONObject json_data = null;
                    System.out.println("JSON data created......");
                    for (int i = 0; i < jArray.length(); i++) {
                        System.out.println("values fetched from the database.....");
                        json_data = jArray.getJSONObject(i);
                        original_value = json_data.getString("value");
                        encrypted_value = json_data.getString("encryptedvalue");
                        encrypt_txt2.setText(encrypted_value);
                        System.out.println(original_value);
                        System.out.println(encrypted_value);

                    }

                    System.out.println("Decrypt button has been clicked");
                    System.out.println("My encryption string is--->" + encrypted_value);
                    int encrypt_len = encrypted_value.length();
                    System.out.println(encrypt_len);

                    try {
                        System.out.println("Encrypted values going to decrrypted......");
                        byteArray1 = Base64.decode(encrypted_value, encrypt_len);
                        String decrypt = new String(byteArray1, "UTF-8");

                        System.out.println("Values decrypted-->" + decrypt);
                        decrypt_txt2.setText(decrypt);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
} 

我该怎么做?可能吗?有人能告诉我吗?提前感谢

有两件事:

  • encode5t
    函数返回无效的base64数据。如果调用
    encode5t(“malavika”)
    ,则返回无效的字符串
    ==AUVVVeZhlQhdlRspnUsRW
    =
    符号是填充符号,仅允许在base64字符串的末尾使用。我想你想要的是:

    function encode5t($value1)
    {
        for($i=0;$i<3;$i++)
        {
            $value1=base64_encode(strrev($value1));
        }
    
        return $value1;
    }
    
    函数编码($value1)
    {
    
    对于($i=0;$iHi),就这样改变

    使用php.java

    try 
    {
       System.out.println("Encrypted values going to decrypt......");
       byteArray1 = Base64.decode(encrypted_value, Base64.DEFAULT);
       System.out.println(byteArray1);
       String decrypt = new String(byteArray1, "UTF-8");                                   
       System.out.println("Values decrypted-->"+decrypt);
       decrypt_txt2.setText(decrypt);
    }
    
    encrypt.php

      <?php
      require_once("connection.php");
      $value='dcplsoft';
      function encode5t($value1)
      {
      $value1=base64_encode($value1);  // here you will get encrypted value
    return $value1;
      }
    $myvalue=encode5t($value);
        $mydata=mysql_query("SELECT * FROM crypt"); 
        while($row = mysql_fetch_assoc( $mydata )) 
        $sam=$row['value'];
        if($sam!='dcplsoft')
        $myinsert=mysql_query("insert into crypt values('".$value."','".$myvalue."')") or die (mysql_error());
        $data = mysql_query("SELECT * FROM crypt"); 
     while($row = mysql_fetch_assoc( $data )) 
    $output[]=$row;
     print(json_encode($output));
        ?>          
    

    看起来PHP和Java对
    base-64
    有不同的实现方式。有没有可能?有没有其他方法可以实现这一点?
    byteArray1=Base64.decode(加密的\u值,加密的\u len);
    我试过用这个太Base64.decode(加密的\u值,Base64.DEFAULT)。它不起作用。它执行起来就是为了尝试{System.out.println(“加密值将被解密……”);仅我在代码中看不到任何加密。
    try 
    {
       System.out.println("Encrypted values going to decrypt......");
       byteArray1 = Base64.decode(encrypted_value, Base64.DEFAULT);
       System.out.println(byteArray1);
       String decrypt = new String(byteArray1, "UTF-8");                                   
       System.out.println("Values decrypted-->"+decrypt);
       decrypt_txt2.setText(decrypt);
    }
    
      <?php
      require_once("connection.php");
      $value='dcplsoft';
      function encode5t($value1)
      {
      $value1=base64_encode($value1);  // here you will get encrypted value
    return $value1;
      }
    $myvalue=encode5t($value);
        $mydata=mysql_query("SELECT * FROM crypt"); 
        while($row = mysql_fetch_assoc( $mydata )) 
        $sam=$row['value'];
        if($sam!='dcplsoft')
        $myinsert=mysql_query("insert into crypt values('".$value."','".$myvalue."')") or die (mysql_error());
        $data = mysql_query("SELECT * FROM crypt"); 
     while($row = mysql_fetch_assoc( $data )) 
    $output[]=$row;
     print(json_encode($output));
        ?>