PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/GAE/src/aharisu/tools/SyncBookmarks/Data/Cryption.java

https://github.com/aharisu/SyncBookmark
Java | 68 lines | 52 code | 11 blank | 5 comment | 0 complexity | 9e9372f4ad6f4443405199b783fbb016 MD5 | raw file
  1. package aharisu.tools.SyncBookmarks.Data;
  2. import java.security.InvalidKeyException;
  3. import java.security.NoSuchAlgorithmException;
  4. import javax.crypto.BadPaddingException;
  5. import javax.crypto.Cipher;
  6. import javax.crypto.IllegalBlockSizeException;
  7. import javax.crypto.NoSuchPaddingException;
  8. import javax.crypto.spec.SecretKeySpec;
  9. /**
  10. * 文字列の暗号化と復号化を行うクラス
  11. * @author aharisu
  12. *
  13. */
  14. public class Cryption {
  15. private static final String TRANSFORMATION = "Blowfish";
  16. public Cryption() {}
  17. public static byte[] encrypt(String key, String text) {
  18. SecretKeySpec spec = new SecretKeySpec(key.getBytes(), TRANSFORMATION);
  19. Exception exception;
  20. try {
  21. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  22. cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, spec);
  23. return cipher.doFinal(text.getBytes());
  24. } catch (NoSuchAlgorithmException e) {
  25. exception = e;
  26. } catch (NoSuchPaddingException e) {
  27. exception = e;
  28. } catch (InvalidKeyException e) {
  29. exception = e;
  30. } catch (IllegalBlockSizeException e) {
  31. exception = e;
  32. } catch (BadPaddingException e) {
  33. exception = e;
  34. }
  35. throw new RuntimeException(exception);
  36. }
  37. public static String decrypt(String key, byte[] encrypted) {
  38. SecretKeySpec spec = new SecretKeySpec(key.getBytes(), TRANSFORMATION);
  39. Exception exception;
  40. try {
  41. Cipher cipher = Cipher.getInstance(TRANSFORMATION);
  42. cipher.init(javax.crypto.Cipher.DECRYPT_MODE, spec);
  43. return new String(cipher.doFinal(encrypted));
  44. } catch (NoSuchAlgorithmException e) {
  45. exception = e;
  46. } catch (NoSuchPaddingException e) {
  47. exception = e;
  48. } catch (InvalidKeyException e) {
  49. exception = e;
  50. } catch (IllegalBlockSizeException e) {
  51. exception = e;
  52. } catch (BadPaddingException e) {
  53. exception = e;
  54. }
  55. throw new RuntimeException(exception);
  56. }
  57. }