/app/src/main/java/sun/bob/leela/runnable/CryptoRunnable.java

https://github.com/PuffOpenSource/Puff-Android · Java · 91 lines · 75 code · 13 blank · 3 comment · 5 complexity · 3afa80f57e8ada790359a797b18cd790 MD5 · raw file

  1. package sun.bob.leela.runnable;
  2. import android.util.Base64;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import de.greenrobot.event.EventBus;
  6. import sun.bob.leela.events.CryptoEvent;
  7. import sun.bob.leela.utils.AppConstants;
  8. /**
  9. * Created by bob.sun on 16/1/25.
  10. */
  11. public class CryptoRunnable implements Runnable {
  12. private String text, password, field;
  13. private byte[] rawText;
  14. private int runModel;
  15. public CryptoRunnable(String text, String password, int runModel, String field){
  16. this.text = text;
  17. this.password = password;
  18. this.runModel = runModel;
  19. this.field = field;
  20. }
  21. public CryptoRunnable setText(String text) {
  22. this.text = text;
  23. return this;
  24. }
  25. public CryptoRunnable setPassword(String password) {
  26. this.password = password;
  27. return this;
  28. }
  29. public CryptoRunnable setRunModel(int runModel) {
  30. this.runModel = runModel;
  31. return this;
  32. }
  33. public CryptoRunnable setField(String field) {
  34. this.field = field;
  35. return this;
  36. }
  37. @Override
  38. public void run() {
  39. CryptoEvent result = null;
  40. if (this.runModel == AppConstants.TYPE_ENCRYPT){
  41. try {
  42. byte[] rawRslt = encrypt();
  43. String rslt = Base64.encodeToString(rawRslt, Base64.DEFAULT);
  44. result = new CryptoEvent(rslt, AppConstants.TYPE_ENCRYPT, this.field);
  45. } catch (Exception e){
  46. e.printStackTrace();
  47. result = new CryptoEvent(e.getMessage(), AppConstants.TYPE_SHTHPPN, this.field);
  48. } finally {
  49. EventBus.getDefault().post(result);
  50. }
  51. } else if (this.runModel == AppConstants.TYPE_DECRYPT){
  52. try {
  53. this.rawText = Base64.decode(this.text, Base64.DEFAULT);
  54. String rslt = new String(decrypt());
  55. result = new CryptoEvent(rslt, AppConstants.TYPE_DECRYPT, this.field);
  56. } catch (Exception e){
  57. e.printStackTrace();
  58. result = new CryptoEvent(e.getMessage(), AppConstants.TYPE_SHTHPPN, this.field);
  59. } finally {
  60. EventBus.getDefault().post(result);
  61. }
  62. }
  63. }
  64. private byte[] encrypt() throws Exception{
  65. SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "Blowfish");
  66. Cipher cipher = Cipher.getInstance("Blowfish");
  67. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  68. byte[] encrypted = cipher.doFinal(text.getBytes());
  69. return encrypted;
  70. }
  71. public byte[] decrypt() throws Exception {
  72. SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "Blowfish");
  73. Cipher cipher = Cipher.getInstance("Blowfish");
  74. cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  75. byte[] decrypted = cipher.doFinal(this.rawText);
  76. return decrypted;
  77. }
  78. }