· 7 years ago · Aug 26, 2018, 09:54 PM
1Encrypting a shared key on the Android/Java platform
2String input = "TheParameterIWantToEncrypt";
3 String secretID = "PublicKey12345678910";
4 char[] inputChars = input.toCharArray();
5 char[] pswChars = secretID.toCharArray();
6
7 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES", new BouncyCastleProvider());
8 KeySpec spec = new PBEKeySpec(pswChars);
9 SecretKey tmp = factory.generateSecret(spec);
10 SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
11
12 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
13 cipher.init(Cipher.ENCRYPT_MODE, secret);
14 AlgorithmParameters params = cipher.getParameters();
15 byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
16 byte[] ciphertext = cipher.doFinal(input.getBytes());
17
18 System.out.println(new String(ciphertext));