app/src/main/java/com/sqh/market/activity/TologinActivity.java
<gh_stars>0package com.sqh.market.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.chaquo.python.Kwarg;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
import com.sqh.market.MyApplication;
import com.sqh.market.R;
import com.sqh.market.constant.Constants;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TologinActivity extends AppCompatActivity {

    public Button login;
    public Button sign;
    private static String publickey = "se2xy1bknelxn4y8xzxu3trosptip3q5" +
            "se2xy1bknelxn4y8xzxu3trosptip3q5" +
            "se2xy1bknelxn4y8xzxu3trosptip3q5";
    private static String privatekey = "9q3vfhm7l33rus21toc8fndupq76itje" +
            "9q3vfhm7l33rus21toc8fndupq76itje" +
            "9q3vfhm7l33rus21toc8fndupq76itje" +
            "9q3vfhm7l33rus21toc8fndupq76itje" +
            "949d1u22cbffbrarjh182eig55721odj" +
            "ax5kh6jaqkcd2tiexxs8v6xjo8yv8a6b" +
            "se2xy1bknelxn4y8xzxu3trosptip3q5" +
            "ngw6fo1pu3tjgnp9jnlp7vnwvfqb9yn7" +
            "74t3tndxag9o7h0890bnpfzh4olk2h9x" +
            "ngw6fo1pu3tjgnp9jnlp7vnwvfqb9yn7" +
            "ax5kh6jaqkcd2tiexxs8v6xjo8yv8a6b" +
            "L+KbNA9UbQ==";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_1);
        initview();
    }

    public void initview(){
        login = (Button) this.findViewById(R.id.btn_confirm);
        sign = (Button) this.findViewById(R.id.btn_register);

        initlistener();
    }

    public void initlistener() {
        login.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View view) {
                handle();
            }
        });

        sign.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TologinActivity.this,TosignupActivity.class);
                startActivity(intent);
            }
        });

    }





    void initPython(){
        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void handle () {

        login.setOnClickListener(
                v ->
                {
                    new Thread(()-> {
                        OkHttpClient okHttpClient = new OkHttpClient();
                        String name = ((EditText) findViewById(R.id.et_username)).getText().toString();
                        String password = ((EditText) findViewById(R.id.et_password)).getText().toString();
                        try {
                            initPython();
                            Python py = Python.getInstance();
                            PyObject obj1 = py.getModule("to_aes").callAttr("akey");
                            String akey = obj1.toJava(String.class);
                            PyObject obj2 = py.getModule("to_aes").callAttr("getresult",new Kwarg("ori", name),new Kwarg("key", akey));
                            String enname = obj2.toJava(String.class);
                            PyObject obj3 = py.getModule("to_aes").callAttr("getresult",new Kwarg("ori",password),new Kwarg("key", akey));
                            String enpassword = obj3.toJava(String.class);
                            PyObject obj4 = py.getModule("to_aes").callAttr("getresult",new Kwarg("ori", "name"),new Kwarg("key", akey));
                            String s2 = obj4.toJava(String.class);
                            PyObject obj5 = py.getModule("to_rsa").callAttr("t_result",new Kwarg("ori", akey));
                            String s1 = obj5.toJava(String.class);


                            String json = "{\n" +
                                    "  \"s1\": \"" + s1 + "\",\n" +
                                    "  \"name\": \"" + enname + "\",\n" +
                                    "  \"password\": \"" + enpassword+ "\",\n" +
                                    "  \"s2\": \"" + s2 + "\",\n"+
                                    "  \"s3\": \"" + s2 + "\",\n"+
                                    "  \"s4\": \"" + s2 + "\"\n"+
                                    "}";
                            Request request = new Request.Builder()
                                    .url(Constants.login_url)
                                    .post(RequestBody.create(MediaType.parse("application/json"),json))
                                    .build();
                            try (Response response = okHttpClient.newCall(request).execute()) {
                                // List<User> users = JSONArray.parseArray(response.body().string(),User.class);
                                Looper.prepare();
                                if(Boolean.parseBoolean(response.body().string()))
                                {
                                    Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
                                    MyApplication myApplication = (MyApplication) this.getApplicationContext();
                                    myApplication.setNumber(1);
                                }
                                else
                                {
                                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                                }
                                Looper.loop();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }




                    }).start();
                }
        );

    }
}
