如何用安卓系统解密php加密的字符串?[英] How to decrypting the php encrypted string using android?

本文是小编为大家收集整理的关于如何用安卓系统解密php加密的字符串?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

嗨,我想解密我的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();
                }
            }
        });
    }
} 

Encrypt.php

<?php
require_once("connection.php");
$value = 'malavika';
function encode5t($value1)
{
    for ($i = 0; $i < 3; $i++) {
        $value1 = strrev(base64_encode($value1));
    }

    return $value1;
}

$myvalue = encode5t($value);

$mydata = mysql_query("SELECT * FROM crypt");
while ($row = mysql_fetch_assoc($mydata))
    $sam = $row['value'];
if ($sam != 'malavika')
    $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));
?>

现在,我想在Android中解密我的加密价值.但是我得到了以下警告:

  12-23 10:27:19.343: WARN/System.err(609): java.lang.IllegalArgumentException: bad base-64
  12-23 10:27:19.353: WARN/System.err(609):     at android.util.Base64.decode(Base64.java:161)
  12-23 10:27:19.353: WARN/System.err(609):     at android.util.Base64.decode(Base64.java:136)
  12-23 10:27:19.363: WARN/System.err(609):     at android.util.Base64.decode(Base64.java:118)
  12-23 10:27:19.363: WARN/System.err(609):     at com.my.databaseconnection.UsingPHP$1.onClick(UsingPHP.java:76)
  12-23 10:27:19.363: WARN/System.err(609):     at android.view.View.performClick(View.java:2485)
  12-23 10:27:19.363: WARN/System.err(609):     at  android.view.View$PerformClick.run(View.java:9080)
 12-23 10:27:19.363: WARN/System.err(609):     at android.os.Handler.handleCallback(Handler.java:587)
 12-23 10:27:19.373: WARN/System.err(609):     at android.os.Handler.dispatchMessage(Handler.java:92)
 12-23 10:27:19.373: WARN/System.err(609):     at android.os.Looper.loop(Looper.java:123)
 12-23 10:27:19.373: WARN/System.err(609):     at android.app.ActivityThread.main(ActivityThread.java:3647)
 12-23 10:27:19.383: WARN/System.err(609):     at java.lang.reflect.Method.invokeNative(Native Method)
 12-23 10:27:19.383: WARN/System.err(609):     at java.lang.reflect.Method.invoke(Method.java:507)
 12-23 10:27:19.383: WARN/System.err(609):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 12-23 10:27:19.383: WARN/System.err(609):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 12-23 10:27:19.393: WARN/System.err(609):     at dalvik.system.NativeStart.main(Native Method)

我该怎么做?可能吗?有人可以告诉我吗?预先感谢

推荐答案

好吧,有两件事:

  1. 您的encode5t函数返回无效的基本64数据.如果我调用encode5t("malavika"),则返回字符串==AUVVVeZhlQhdlRspnUsRW,这不是有效的base64. =符号是填充物,仅在基本64弦的末端才允许.我想您想要的是:

    function encode5t($value1)
    {
        for($i=0;$i<3;$i++)
        {
            $value1=base64_encode(strrev($value1));
        }
    
        return $value1;
    }
    

    这意味着您只需要首先调用strrev,然后才需要base64_encode.在解码功能中,您首先调用base64_decode,然后strrev(或Java ounterparts).

  2. 正如我在评论中所说的那样,第二个参数为 Base64.decode 在您的Java-code中期望base64处理的标志,而不是字符串的长度.例如,这可能打开URL_SAFE标志,这使其与PHP的base64_encode.

  3. 的输出不相容

其他推荐答案

嗨,就像这样更改.

使用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));
    ?>          

本文地址:https://www.itbaoku.cn/post/102360.html

问题描述

Hi I want to decrypting my php encrypted string. My code is

UsingPHP.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();
                }
            }
        });
    }
} 

encrypt.php

<?php
require_once("connection.php");
$value = 'malavika';
function encode5t($value1)
{
    for ($i = 0; $i < 3; $i++) {
        $value1 = strrev(base64_encode($value1));
    }

    return $value1;
}

$myvalue = encode5t($value);

$mydata = mysql_query("SELECT * FROM crypt");
while ($row = mysql_fetch_assoc($mydata))
    $sam = $row['value'];
if ($sam != 'malavika')
    $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));
?>

Now I want to decrypt my encrypted value in android. But I got this following warning:

  12-23 10:27:19.343: WARN/System.err(609): java.lang.IllegalArgumentException: bad base-64
  12-23 10:27:19.353: WARN/System.err(609):     at android.util.Base64.decode(Base64.java:161)
  12-23 10:27:19.353: WARN/System.err(609):     at android.util.Base64.decode(Base64.java:136)
  12-23 10:27:19.363: WARN/System.err(609):     at android.util.Base64.decode(Base64.java:118)
  12-23 10:27:19.363: WARN/System.err(609):     at com.my.databaseconnection.UsingPHP$1.onClick(UsingPHP.java:76)
  12-23 10:27:19.363: WARN/System.err(609):     at android.view.View.performClick(View.java:2485)
  12-23 10:27:19.363: WARN/System.err(609):     at  android.view.View$PerformClick.run(View.java:9080)
 12-23 10:27:19.363: WARN/System.err(609):     at android.os.Handler.handleCallback(Handler.java:587)
 12-23 10:27:19.373: WARN/System.err(609):     at android.os.Handler.dispatchMessage(Handler.java:92)
 12-23 10:27:19.373: WARN/System.err(609):     at android.os.Looper.loop(Looper.java:123)
 12-23 10:27:19.373: WARN/System.err(609):     at android.app.ActivityThread.main(ActivityThread.java:3647)
 12-23 10:27:19.383: WARN/System.err(609):     at java.lang.reflect.Method.invokeNative(Native Method)
 12-23 10:27:19.383: WARN/System.err(609):     at java.lang.reflect.Method.invoke(Method.java:507)
 12-23 10:27:19.383: WARN/System.err(609):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
 12-23 10:27:19.383: WARN/System.err(609):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
 12-23 10:27:19.393: WARN/System.err(609):     at dalvik.system.NativeStart.main(Native Method)

How can I do this? Is it possible? Can anybody tell me? Thanks in advance

推荐答案

Well there are two things:

  1. Your encode5t function returns invalid base64 data. If I call encode5t("malavika") then the string ==AUVVVeZhlQhdlRspnUsRW is returned which is not valid base64. The = sign is the padding and is only allowed at the end of the base64-string. I guess what you wanted is:

    function encode5t($value1)
    {
        for($i=0;$i<3;$i++)
        {
            $value1=base64_encode(strrev($value1));
        }
    
        return $value1;
    }
    

    That means you just have to first call strrev and then base64_encode. And in the decode-function you first call base64_decode and then strrev (or the Java counter-parts).

  2. As I've said in the comment, the second Parameter to Base64.decode in your Java-code is expects flags for the base64-processing and not the length of the string. This might for example turn on the URL_SAFE flag which makes it incompatible with the output of PHP's base64_encode.

其他推荐答案

Hi just change like this.

UsingPHP.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));
    ?>