· 9 years ago · Dec 30, 2016, 06:14 PM
1public void encrypt(){
2 //encrypt
3 String decrypted = null;
4 String encryptedData = null; //getBytes("UTF-8")
5 EditText mEdit = (EditText)findViewById(R.id.editText);
6 str_key = (String) mEdit.getText().toString();
7
8 int iterationCount = 1000;
9 int keyLength = 128; //256
10 int saltLength = keyLength / 8; // same size as key output
11
12 SecureRandom random = new SecureRandom();
13 salt = new byte[saltLength];
14 random.nextBytes(salt);
15 KeySpec keySpec = new PBEKeySpec(str_key.toCharArray(), salt, iterationCount, keyLength);
16 SecretKeyFactory keyFactory = null;
17 try {
18 keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
19 } catch (NoSuchAlgorithmException e) {
20 e.printStackTrace();
21 }
22 byte[] keyBytes = new byte[0];
23 try {
24 keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
25 } catch (InvalidKeySpecException e) {
26 e.printStackTrace();
27 }
28 SecretKey key = new SecretKeySpec(keyBytes, "Blowfish");