Java 检索加密哈希响应上的id

Java 检索加密哈希响应上的id,java,android,json,android-fragments,httpresponse,Java,Android,Json,Android Fragments,Httpresponse,检索加密哈希响应上的id。它可以在Java Android函数上解密吗 当toast是一个结果字符串时出现,我无法获取id,当我获取用户名字符串时,出现的是字符串结果。帮帮忙,我很困惑 Json响应 { "status": 200, "reason": "OK", "success": true, "message": null, "result": "eyJ0eXAiOiJKV1QiLCJhbGciOiJITUFDLVNIQTI1NiJ9.eyJpZCI

检索加密哈希响应上的id。它可以在Java Android函数上解密吗

当toast是一个结果字符串时出现,我无法获取id,当我获取用户名字符串时,出现的是字符串结果。帮帮忙,我很困惑

Json响应

{
    "status": 200,
    "reason": "OK",
    "success": true,
    "message": null,
    "result": "eyJ0eXAiOiJKV1QiLCJhbGciOiJITUFDLVNIQTI1NiJ9.eyJpZCI6IjQ5OSIsImVtYWlsIjoiYmlzbWlsbGFoYmlzYUBleGFtcGxlLmNvbSIsIm1zaXNkbiI6IjA3OTc5Nzg0NjQ5NCIsInVzZXJuYW1lIjoiYmlzbWlsbGFoYmlzYSIsInZlcmlmaWVkTWVtYmVyIjpudWxsLCJwcm9maWxlIjp7ImlkIjoiMzE2IiwiaWRfZ2VvZGlyZWN0b3J5IjpudWxsLCJmdWxsbmFtZSI6ImJpc21pbGxhaGJpc2EiLCJudW1iZXIiOiIyNzQyNDciLCJpbWFnZSI6Imh0dHBzOlwvXC9kZW1vLmtyZWRpdGltcGlhbi5jb21cL3N0b3JhZ2VcL2ltYWdlc1wvZGVmYXVsdFwvYXZhdGFyLmpwZyIsInJlY29yZCI6eyJzdGF0dXMiOiJQVUJMSVNIIiwiY3JlYXRlIjp7InVzZXIiOm51bGwsInRpbWVzdGFtcCI6eyJkYXRlIjoiMjAxOS0xMi0wMyAxNTowODozMi4wMDAwMDAiLCJ0aW1lem9uZV90eXBlIjozLCJ0aW1lem9uZSI6IkFzaWFcL0pha2FydGEifX0sInVwZGF0ZSI6eyJ1c2VyIjpudWxsLCJ0aW1lc3RhbXAiOm51bGx9fSwibWV0YWRhdGEiOnsidXNlcm5hbWUiOiJiaXNtaWxsYWhiaXNhIiwiZW1haWwiOiJiaXNtaWxsYWhiaXNhQGV4YW1wbGUuY29tIiwicGhvbmUiOiIwNzk3OTc4NDY0OTQifSwic3RhdGlzdGljIjpudWxsfX0.zIEhdU5MyNjReG_9_661FWf0_R5eZuJweyl0JNFd7X0"
}
LoginUser.java

    public void LoginUser() {
        //membuat progress dialog
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);
        pDialog.setMessage("Tunggu proses login ...");
        pDialog.show();

        //mengambil data dari edittext

        final String username = txtusername.getText().toString().trim();
        final String password = txtpassword.getText().toString().trim();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(50, TimeUnit.SECONDS)
                .readTimeout(50, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL).client(client)
                .addConverterFactory(GsonConverterFactory.create(new Gson())).build();

        RequestInterface api = retrofit.create(RequestInterface.class);
        Call<ResponseLoginSucces> call = api.login_member(username,  password);

        call.enqueue(new Callback<ResponseLoginSucces>() {
            @Override
            public void onResponse(Call<ResponseLoginSucces> call, Response<ResponseLoginSucces> response) {

                if(response.isSuccessful()){
                    pDialog.dismiss();
                    if(response.body().getResult() != null){

                        // Jika login berhasil
                        String id = response.body().getResult();
                        String email = response.body().getResult();
                        String username = response.body().getResult();
                        String msisdn = response.body().getResult();
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_ID, id);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_EMAIL, email);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_USERNAME, username);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_MSISDN, msisdn);

                        Toast.makeText(getApplicationContext(), "Berhasil Login" +id, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginUser.this, MenuUtama.class);
                        sharedPrefManager.saveSPBoolean(SharedPrefManager.SP_SUDAH_LOGIN, true);
                        startActivity(intent);
                        finish();


                    } else {
                        Toast.makeText(LoginUser.this, "The username or password is incorrect", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginUser.this, "Error! Please try again!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseLoginSucces> call, Throwable t) {
                t.printStackTrace();
                pDialog.dismiss();
                Toast.makeText(LoginUser.this, "Koneksi internet terputus.", Toast.LENGTH_SHORT).show();
            }
        });



    }
FragmentMenu.java

sharedPrefManager = new SharedPrefManager(getActivity());

Toast.makeText(getActivity(), "Identitas mu "+ sharedPrefManager.getSPID(), Toast.LENGTH_SHORT).show();

如上@Mike M.所述,响应JSON字符串中的
结果
似乎是用Base64编码的,它可以被看作是由点(.)分隔的3个部分。因此,如果您解码第二部分,您将得到以下有效的JSON字符串,那么字段
id
的值
499
就是您想要检索的值

{ 
  "id":"499",
  "email":"bismillahbisa@example.com",
  "msisdn":"079797846494",
  "username":"bismillahbisa",
  "verifiedMember":null,
  "profile":{ 
    "id":"316",
    "id_geodirectory":null,
    "fullname":"bismillahbisa",
    "number":"274247",
    "image":"https:\/\/demo.kreditimpian.com\/storage\/images\/default\/avatar.jpg",
    "record":{ 
      "status":"PUBLISH",
      "create":{ 
        "user":null,
        "timestamp":{ 
          "date":"2019-12-03 15:08:32.000000",
          "timezone_type":3,
          "timezone":"Asia\/Jakarta"
        }
      },
      "update":{ 
        "user":null,
        "timestamp":null
      }
    },
    "metadata":{ 
      "username":"bismillahbisa",
      "email":"bismillahbisa@example.com",
      "phone":"079797846494"
    },
    "statistic":null
  }
}
如果您可以首先在响应中获得
result
,那么您可以使用下面的代码片段,使用
java.util.Base64
(java 8)对
result
的第二部分进行解码。对于Java7,您可以使用
ApacheCommonsCodec
进行解码(Maven依赖项如下所示)。最后,使用3个最流行的JSON库检索
id
的值-
Jackson
Gson
org.JSON

Apache Commons编解码器(适用于Java 7)

public void登录用户(){
//membuat进度对话框
pDialog=新建进度对话框(此对话框);
pDialog.setCancelable(假);
setMessage(“Tunggu进程登录…”);
pDialog.show();
//MENGABILDATA dari edittext
最终字符串username=txtusername.getText().toString().trim();
最终字符串密码=txtpassword.getText().toString().trim();
OkHttpClient客户端=新建OkHttpClient.Builder()
.connectTimeout(50,时间单位为秒)
.readTimeout(50,TimeUnit.SECONDS).build();
改装改装=新改装.Builder()
.baseUrl(BuildConfig.BASE\u URL).client(客户端)
.addConverterFactory(GsonConverterFactory.create(new Gson()).build();
RequestInterface api=reformation.create(RequestInterface.class);
Call Call=api.login\u成员(用户名、密码);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.issusccessful()){
pDialog.disclose();
if(response.body().getResult()!=null){
String jsonStr=新字符串(Base64.getDecoder().decode(resultStr.split(“\\”)[1].getBytes(),“UTF-8”);
//格森
id=new Gson().fromJson(jsonStr,JsonObject.class).get(“id”).getAsString();
//吉卡·贝哈西尔
String id=response.body().getResult();
字符串email=response.body().getResult();
字符串username=response.body().getResult();
字符串msisdn=response.body().getResult();
sharedPrefManager.saveSPString(sharedPrefManager.SP_ID,ID);
sharedPrefManager.saveSPString(sharedPrefManager.SP_电子邮件,电子邮件);
sharedPrefManager.saveSPString(sharedPrefManager.SP_用户名,用户名);
saveSPString(sharedPrefManager.SP_MSISDN,MSISDN);
Toast.makeText(getApplicationContext(),“Berhasil Login”+id,Toast.LENGTH_SHORT).show();
意向意向=新意向(logiuser.this,MenuUtama.class);
sharedPrefManager.saveSPBoolean(sharedPrefManager.SP_SUDAH_登录,true);
星触觉(意向);
完成();
}否则{
Toast.makeText(logiuser.this,“用户名或密码不正确”,Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(logiuser.this,“错误!请重试!”,Toast.LENGTH\u SHORT.show();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
pDialog.disclose();
Toast.makeText(logiuser.this,“Koneksi internet terputus.”,Toast.LENGTH_SHORT.show();
}
});
}

我不熟悉Android开发,但是如果你知道加密的算法和密钥,那么你也可以解密。你最初是如何生成哈希代码的?我不知道哈希是如何开始的。我从供应商那里得到了一个API,登录验证的结果得到了加密的结果。当我询问时,代码由字符串ID、用户名、电子邮件和电话生成。他们没有解释使用散列的方法。你能解释一下上面的问题吗?我不太确定我是否理解你的要求,但是JSON中的
结果
是Base64编码的(不管怎样,大部分都是)。好的,显然它是一个JSON Web令牌,由三个Base64编码的部分组成,这些部分用句点(
)分隔。你想要的数据在中间部分。我以前从未处理过这些问题,但它看起来非常简单,可以进行字符串拆分和Base64解码。上面的答案可能会对你有所帮助。如果你仔细研究的话,这里肯定还有其他帖子。“resultStr.split”你从哪里得到的?@indriyanto Nugroho--resultStr等于
response.body().getResult()
,如果它的输出是响应JSON字符串中
result
的值。我能把它放在响应函数后面吗???如下@LHCHIN@indriyanto努格罗霍当然可以!你能在上面的URL中发布你的IDE关于错误的说明吗?这个错误,我不知道有什么错误,我需要为Base64添加依赖项吗?如果是,哪种依赖关系@勒钦
{ 
  "id":"499",
  "email":"bismillahbisa@example.com",
  "msisdn":"079797846494",
  "username":"bismillahbisa",
  "verifiedMember":null,
  "profile":{ 
    "id":"316",
    "id_geodirectory":null,
    "fullname":"bismillahbisa",
    "number":"274247",
    "image":"https:\/\/demo.kreditimpian.com\/storage\/images\/default\/avatar.jpg",
    "record":{ 
      "status":"PUBLISH",
      "create":{ 
        "user":null,
        "timestamp":{ 
          "date":"2019-12-03 15:08:32.000000",
          "timezone_type":3,
          "timezone":"Asia\/Jakarta"
        }
      },
      "update":{ 
        "user":null,
        "timestamp":null
      }
    },
    "metadata":{ 
      "username":"bismillahbisa",
      "email":"bismillahbisa@example.com",
      "phone":"079797846494"
    },
    "statistic":null
  }
}
<dependency>
  <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.9</version>
</dependency>
// Java 8
//String jsonStr = new String(Base64.getDecoder().decode(resultStr.split("\\.")[1].getBytes()), "UTF-8");

// Java 7
String jsonStr = new String(Base64.decodeBase64(result.split("\\.")[1]), "UTF-8");

// Jackson
String id = new ObjectMapper().readTree(jsonStr).get("id").asText();

// Gson
id = new Gson().fromJson(jsonStr, JsonObject.class).get("id").getAsString();

// org.json
id = new JSONObject(jsonStr).getString("id");
   public void LoginUser() {
        //membuat progress dialog
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);
        pDialog.setMessage("Tunggu proses login ...");
        pDialog.show();

        //mengambil data dari edittext

        final String username = txtusername.getText().toString().trim();
        final String password = txtpassword.getText().toString().trim();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(50, TimeUnit.SECONDS)
                .readTimeout(50, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL).client(client)
                .addConverterFactory(GsonConverterFactory.create(new Gson())).build();

        RequestInterface api = retrofit.create(RequestInterface.class);
        Call<ResponseLoginSucces> call = api.login_member(username,  password);

        call.enqueue(new Callback<ResponseLoginSucces>() {
            @Override
            public void onResponse(Call<ResponseLoginSucces> call, Response<ResponseLoginSucces> response) {

                if(response.isSuccessful()){
                    pDialog.dismiss();
                    if(response.body().getResult() != null){

                        String jsonStr = new String(Base64.getDecoder().decode(resultStr.split("\\.")[1].getBytes()), "UTF-8");
                        // Gson
                        id = new Gson().fromJson(jsonStr, JsonObject.class).get("id").getAsString();





                        // Jika login berhasil
                        String id = response.body().getResult();
                        String email = response.body().getResult();
                        String username = response.body().getResult();
                        String msisdn = response.body().getResult();
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_ID, id);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_EMAIL, email);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_USERNAME, username);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_MSISDN, msisdn);

                        Toast.makeText(getApplicationContext(), "Berhasil Login" +id, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginUser.this, MenuUtama.class);
                        sharedPrefManager.saveSPBoolean(SharedPrefManager.SP_SUDAH_LOGIN, true);
                        startActivity(intent);
                        finish();



                    } else {
                        Toast.makeText(LoginUser.this, "The username or password is incorrect", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginUser.this, "Error! Please try again!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseLoginSucces> call, Throwable t) {
                t.printStackTrace();
                pDialog.dismiss();
                Toast.makeText(LoginUser.this, "Koneksi internet terputus.", Toast.LENGTH_SHORT).show();
            }
        });



    }