· 8 years ago · Dec 02, 2017, 09:28 AM
1void main1() throws LoginException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
2 String test = "hello";
3 Cipher cipher2 = Cipher.getInstance("RSA");
4 //key
5 byte [] decodedPublicKey = Base64.decode(myPublickKey, 4);
6 byte[] decodedPrivateKey = Base64.decode(myPrivateKey,4);
7
8 SecretKey originalPublicKey = new SecretKeySpec(decodedPublicKey, 0, decodedPublicKey.length, "RSA");
9 SecretKeySpec originalPrivateKey = new SecretKeySpec(decodedPrivateKey, 0, decodedPrivateKey.length, "RSA");
10
11
12 cipher2.init(Cipher.ENCRYPT_MODE, originalPublicKey);
13
14 byte[] bytes2 = cipher2.doFinal(test.getBytes());
15
16 Cipher decript2 = Cipher.getInstance("RSA");
17
18
19 decript2.init(Cipher.DECRYPT_MODE,originalPrivateKey);
20
21 byte[] decriptedBytes2 = decript2.doFinal(bytes2);
22
23 for (byte b : decriptedBytes2)kodMess+=(char) b;
24
25 Toast.makeText(ChatActivity.this, kodMess, Toast.LENGTH_SHORT).show();
26
27 }