thieumao

Security.java AES

Jul 9th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. package com.tvn.appgiaitrifree.utils;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import android.util.Base64;
  5.  
  6. public class Security {
  7. public static String encrypt(String input, String key){
  8. String base64 = "";
  9. byte[] crypted = null;
  10. try{
  11. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
  12. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  13. cipher.init(Cipher.ENCRYPT_MODE, skey);
  14. crypted = cipher.doFinal(input.getBytes());
  15. }catch(Exception e){
  16. System.out.println(e.toString());
  17. }
  18.  
  19. return new String(Base64.encodeToString(crypted, Base64.DEFAULT));
  20. }
  21.  
  22. public static String decrypt(String input, String key){
  23. byte[] output = null;
  24. try{
  25. SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
  26. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  27. cipher.init(Cipher.DECRYPT_MODE, skey);
  28. output = cipher.doFinal(Base64.decode(input,Base64.DEFAULT));
  29. }catch(Exception e){
  30. System.out.println(e.toString());
  31. }
  32. return new String(output);
  33. }
  34.  
  35. // public static void main(String[] args) {
  36. // String key = "1234567891234567";
  37. // String data = "example";
  38. // System.out.println(Security.decrypt(Security.encrypt(data, key), key));
  39. // System.out.println(Security.encrypt(data, key));
  40. // }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment