· 5 years ago · Jul 19, 2020, 09:34 AM
1public class MainActivity extends AppCompatActivity {
2 public static final String ALGORITHM = "AES";
3 public static final String CHAR_SET = "UTF-8";
4 public static final String SECURE_RANDOM_ALGORITHM = "SHA1PRNG";
5 private byte[] bytes;
6 private Cipher cipher = null;
7 private byte[] encodeBytes;
8 private EditText etEnterString;
9 private byte[] privateKey;
10 private SecretKey secretKey;
11 private TextView txDecryptString;
12 private TextView txEncryptString;
13
14 /* JADX INFO: used method not loaded: androidx.appcompat.app.AppCompatActivity.setContentView(int):null, types can be incorrect */
15 /* access modifiers changed from: protected */
16 public void onCreate(Bundle savedInstanceState) {
17 super.onCreate(savedInstanceState);
18 setContentView((int) R.layout.activity_main);
19 extraAdded();
20 init();
21 }
22
23 private void extraAdded() {
24 try {
25 this.privateKey = generateKey();
26 } catch (NoSuchAlgorithmException e) {
27 e.printStackTrace();
28 }
29 }
30
31 private void init() {
32 this.etEnterString = (EditText) findViewById(R.id.etEnterString);
33 this.txEncryptString = (TextView) findViewById(R.id.txEncryptString);
34 this.txDecryptString = (TextView) findViewById(R.id.txDecryptString);
35 }
36
37 public void onClick(View view) {
38 switch (view.getId()) {
39 case R.id.btnDecryptString /*2131165250*/:
40 decryptData();
41 return;
42 case R.id.btnEncryptString /*2131165251*/:
43 encryptData();
44 return;
45 default:
46 return;
47 }
48 }
49
50 private void encryptData() {
51 if (this.etEnterString.getText().toString().trim().length() != 0) {
52 try {
53 this.encodeBytes = encrypt(this.privateKey, this.etEnterString.getText().toString().getBytes());
54 this.txEncryptString.setText(new String(this.encodeBytes));
55 } catch (Exception e) {
56 e.printStackTrace();
57 }
58 } else {
59 Toast.makeText(this, "Enter String value", 0).show();
60 }
61 }
62
63 private void decryptData() {
64 if (this.txEncryptString.getText().toString().trim().length() != 0) {
65 try {
66 this.txDecryptString.setText(new String(decrypt(this.privateKey, this.encodeBytes)));
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70 } else {
71 Toast.makeText(this, "first encrypt your string", 0).show();
72 }
73 }
74
75 private byte[] generateKey() throws NoSuchAlgorithmException {
76 byte[] keyStart = "Key Testing".getBytes();
77 KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
78 SecureRandom sr = SecureRandom.getInstance(SECURE_RANDOM_ALGORITHM);
79 sr.setSeed(keyStart);
80 kgen.init(128, sr);
81 return kgen.generateKey().getEncoded();
82 }
83
84 private byte[] encrypt(byte[] raw, byte[] inputByte) throws Exception {
85 String str = ALGORITHM;
86 SecretKeySpec skeySpec = new SecretKeySpec(raw, str);
87 Cipher cipher2 = Cipher.getInstance(str);
88 cipher2.init(1, skeySpec);
89 return cipher2.doFinal(inputByte);
90 }
91
92 private byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
93 String str = ALGORITHM;
94 SecretKeySpec skeySpec = new SecretKeySpec(raw, str);
95 Cipher cipher2 = Cipher.getInstance(str);
96 cipher2.init(2, skeySpec);
97 return cipher2.doFinal(encrypted);
98 }
99}