package net.droidtech.rsautils; import android.app.Activity; import android.app.ProgressDialog; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import net.droidtech.utils.GenerateKeyPairThread; import net.droidtech.utils.HexStringUtils; import net.droidtech.utils.KeyPairUtils; import net.droidtech.utils.KeyStoreManager; import net.droidtech.utils.Settings; /** * Created by root on 2019/7/17. */ public class GenerateKeyPairActivity extends Activity { private EditText publicKey; private EditText privateKey; private static ProgressDialog inProgress=null; private GenerateKeyPairThread thread=null; private KeyGenerationResultHandler handler=null; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); if(!Settings.isAllowScreenShot()){ this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); } setContentView(R.layout.activity_generatekeypair); this.publicKey=this.findViewById(R.id.public_key_generate); this.privateKey=this.findViewById(R.id.private_key_generate); this.publicKey.setEnabled(false); this.privateKey.setEnabled(false); this.publicKey.setFocusable(false); this.privateKey.setFocusable(false); Button generateKeypair=this.findViewById(R.id.generate_keypair); Button copyPubKey=this.findViewById(R.id.copy_pubkey); Button saveKeyPair=this.findViewById(R.id.savekeypair); inProgress=new ProgressDialog(this); inProgress.setTitle(R.string.please_wait); inProgress.setMessage(this.getApplicationContext().getResources().getString(R.string.in_progress)); inProgress.setCancelable(false); this.handler=new KeyGenerationResultHandler(this); this.handler.setPublicKeyOutputEditText(this.publicKey); this.handler.setPrivateKeyOutputEditText(this.privateKey); generateKeypair.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { inProgress.show(); thread=new GenerateKeyPairThread(); thread.setHandler(handler); thread.start(); } }); copyPubKey.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(handler.getPublicKeyData()==null){ Toast.makeText(GenerateKeyPairActivity.this,R.string.copy_failed,Toast.LENGTH_SHORT).show(); return; } ClipboardManager cm =(ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); ClipData cd = ClipData.newPlainText("output",HexStringUtils.byteToHexString(handler.getPublicKeyData())); cm.setPrimaryClip(cd); Toast.makeText(GenerateKeyPairActivity.this,R.string.copied,Toast.LENGTH_SHORT).show(); } }); saveKeyPair.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(handler.getPublicKeyData()!=null&&handler.getPrivateKeyData()!=null){ Toast.makeText(GenerateKeyPairActivity.this,R.string.keypair_notice,Toast.LENGTH_LONG).show(); KeyStoreManager.KeyItem pub_key=new KeyStoreManager.KeyItem(); pub_key.setNote(GenerateKeyPairActivity.this.getResources().getText(R.string.pubkey_generated_by_user).toString()); pub_key.setKey(handler.getPublicKeyData()); pub_key.setType(KeyStoreManager.KEY_TYPE_PUBLIC); KeyStoreManager.addItem(pub_key); KeyStoreManager.KeyItem priv_key=new KeyStoreManager.KeyItem(); priv_key.setNote(GenerateKeyPairActivity.this.getResources().getText(R.string.privkey_generated_by_user).toString()); priv_key.setKey(handler.getPrivateKeyData()); priv_key.setType(KeyStoreManager.KEY_TYPE_PRIVATE); KeyStoreManager.addItem(priv_key); } } }); } private static class KeyGenerationResultHandler extends Handler{ private byte[] pub_key_data; private byte[] priv_key_data; private EditText publicKeyOutput; private EditText privateKeyOutput; private Context appContext; public byte[] getPublicKeyData(){ return this.pub_key_data; } public byte[] getPrivateKeyData(){ return this.priv_key_data; } public void setPublicKeyOutputEditText(EditText output){ this.publicKeyOutput=output; } public void setPrivateKeyOutputEditText(EditText output){ this.privateKeyOutput=output; } public KeyGenerationResultHandler(Context appContext){ this.appContext=appContext; } @Override public void handleMessage(Message msg){ inProgress.dismiss(); if(msg.what==GenerateKeyPairThread.KEY_GENERATION_SUCCESS){ this.pub_key_data=((KeyPairUtils)(msg.obj)).getPublicKey(); if(this.pub_key_data.length<=1024){ this.publicKeyOutput.setText(HexStringUtils.byteToHexString(this.pub_key_data)); }else{ String tmpKey=HexStringUtils.byteToHexString(this.pub_key_data); this.publicKeyOutput.setText(tmpKey.substring(0,2048)); } this.priv_key_data=((KeyPairUtils)(msg.obj)).getPrivateKey(); this.privateKeyOutput.setHint(this.appContext.getResources().getText(R.string.priv_key_hidden)); } if(msg.what==GenerateKeyPairThread.KEY_GENERATION_ERROR){ Toast.makeText(this.appContext,((Exception)msg.obj).toString(),Toast.LENGTH_LONG).show(); } } } }