· 8 years ago · Nov 22, 2017, 03:24 PM
1private void main(String[] args) throws LoginException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
2 String s = "ab";
3 Cipher cipher = Cipher.getInstance("AES");
4
5 KeyGenerator kgen = KeyGenerator.getInstance("AES");
6 kgen.init(128);
7 final SecretKey key = kgen.generateKey();
8 //SecretKeySpec key = new SecretKeySpec("bar12345Bar12345".getBytes(),"AES");
9time.setText(String.valueOf(key));
10 cipher.init(Cipher.ENCRYPT_MODE,key);
11 final byte[] bytes = cipher.doFinal(s.getBytes("UTF-8"));
12 String s=new String(bytes, "UTF-8");
13 for (byte b : bytes)kkk+=(char) b;
14 //time.setText(s);
15 byte[] byteArray = kkk.getBytes();
16
17 // String str = new String(bytes, StandardCharsets.UTF_8);
18 Cipher decript = Cipher.getInstance("AES");
19 decript.init(Cipher.DECRYPT_MODE,key);
20 byte[] decriptedBytes = decript.doFinal(bytes);
21 for (byte b : decriptedBytes)kk+=(char) b;
22
23time.setText(kk);
24
25
26
27 reg.setOnClickListener(new View.OnClickListener() {
28 @Override
29 public void onClick(View view) {
30 sPref = getSharedPreferences("save",MODE_PRIVATE);
31 SharedPreferences.Editor ed = sPref.edit();
32 ed.putString(SAVED_L, s);
33 ed.commit();
34 Toast.makeText(RegActivity.this, kk+String.valueOf(key), Toast.LENGTH_SHORT).show();
35 }
36 });
37 }