Encryption:
01 public static byte[] encrypt(String content, String password) {
02 try {
03 KeyGenerator kgen = KeyGenerator.getInstance("AES");
04 kgen.init(128, new SecureRandom(password.getBytes()));
05 SecretKey secretKey = kgen.generateKey();
06 byte[] enCodeFormat = secretKey.getEncoded();
07 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
08 Cipher cipher = Cipher.getInstance("AES");// Create the cipher
09 byte[] byteContent = content.getBytes("utf-8");
10 cipher.init(Cipher.ENCRYPT_MODE, key);// Initialization
11 byte[] result = cipher.doFinal(byteContent);
12 return result; // return the encrypted binary bytes.
13 //If we want, we can use Base64 to make it more
secure.
14 } catch (NoSuchAlgorithmException e) {
15 e.printStackTrace();
16 } catch (NoSuchPaddingException e) {
17 e.printStackTrace();
18 } catch (InvalidKeyException e) {
19 e.printStackTrace();
20 } catch (UnsupportedEncodingException e) {
21 e.printStackTrace();
22 } catch (IllegalBlockSizeException e) {
23 e.printStackTrace();
24 } catch (BadPaddingException e) {
25 e.printStackTrace();
26 }
27 return null;
28 }
Decryption:
01 public static byte[] decrypt(byte[] content, String password) {
02 try {
03 KeyGenerator kgen = KeyGenerator.getInstance("AES");
04 kgen.init(128, new SecureRandom(password.getBytes()));
05 SecretKey secretKey = kgen.generateKey();
06 byte[] enCodeFormat = secretKey.getEncoded();
07 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
08 Cipher cipher = Cipher.getInstance("AES");
09 cipher.init(Cipher.DECRYPT_MODE, key);
10 byte[] result = cipher.doFinal(content);
11 return result;
12 } catch (NoSuchAlgorithmException e) {
13 e.printStackTrace();
14 } catch (NoSuchPaddingException e) {
15 e.printStackTrace();
16 } catch (InvalidKeyException e) {
17 e.printStackTrace();
18 } catch (IllegalBlockSizeException e) {
19 e.printStackTrace();
20 } catch (BadPaddingException e) {
21 e.printStackTrace();
22 }
23 return null;
24 }
Tip: Using AES to encrypt, we must have a key. How to generate and this key is also one important security issue that need to be considered.
14 } catch (NoSuchAlgorithmException e) {
15 e.printStackTrace();
16 } catch (NoSuchPaddingException e) {
17 e.printStackTrace();
18 } catch (InvalidKeyException e) {
19 e.printStackTrace();
20 } catch (UnsupportedEncodingException e) {
21 e.printStackTrace();
22 } catch (IllegalBlockSizeException e) {
23 e.printStackTrace();
24 } catch (BadPaddingException e) {
25 e.printStackTrace();
26 }
27 return null;
28 }
Decryption:
01 public static byte[] decrypt(byte[] content, String password) {
02 try {
03 KeyGenerator kgen = KeyGenerator.getInstance("AES");
04 kgen.init(128, new SecureRandom(password.getBytes()));
05 SecretKey secretKey = kgen.generateKey();
06 byte[] enCodeFormat = secretKey.getEncoded();
07 SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
08 Cipher cipher = Cipher.getInstance("AES");
09 cipher.init(Cipher.DECRYPT_MODE, key);
10 byte[] result = cipher.doFinal(content);
11 return result;
12 } catch (NoSuchAlgorithmException e) {
13 e.printStackTrace();
14 } catch (NoSuchPaddingException e) {
15 e.printStackTrace();
16 } catch (InvalidKeyException e) {
17 e.printStackTrace();
18 } catch (IllegalBlockSizeException e) {
19 e.printStackTrace();
20 } catch (BadPaddingException e) {
21 e.printStackTrace();
22 }
23 return null;
24 }
Tip: Using AES to encrypt, we must have a key. How to generate and this key is also one important security issue that need to be considered.
No comments:
Post a Comment