PageRenderTime 208ms CodeModel.GetById 30ms RepoModel.GetById 3ms app.codeStats 0ms

/noxious/util/StringOBF.java

https://gitlab.com/N00bVip/Noxious
Java | 88 lines | 57 code | 31 blank | 0 comment | 4 complexity | 265090719960bc92462b62ac508fe913 MD5 | raw file
  1. package noxious.util;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import javax.crypto.Cipher;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import javax.mail.internet.MimeUtility;
  9. public class StringOBF {
  10. public static String encrypt(String key, String text) throws Exception {
  11. SecretKeySpec sksSpec =
  12. new SecretKeySpec(key.getBytes(), "Blowfish");
  13. Cipher cipher = Cipher.getInstance("Blowfish");
  14. cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, sksSpec);
  15. return encodeBase64(cipher.doFinal(text.getBytes()));
  16. }
  17. public static String encodeBase64(byte[] data) throws Exception {
  18. ByteArrayOutputStream forEncode = new ByteArrayOutputStream();
  19. OutputStream toBase64 = MimeUtility.encode(forEncode, "base64");
  20. toBase64.write(data);
  21. toBase64.close();
  22. return forEncode.toString("iso-8859-1");
  23. }
  24. public static String encodeBase64(String string) throws Exception {
  25. ByteArrayOutputStream forEncode = new ByteArrayOutputStream();
  26. OutputStream toBase64 = MimeUtility.encode(forEncode, "base64");
  27. toBase64.write(string.getBytes());
  28. toBase64.close();
  29. return forEncode.toString("iso-8859-1");
  30. }
  31. public static String decrypt(String key, String codeBase64)
  32. throws Exception {
  33. SecretKeySpec sksSpec =
  34. new SecretKeySpec(key.getBytes(), "Blowfish");
  35. Cipher cipher = Cipher.getInstance("Blowfish");
  36. cipher.init(Cipher.DECRYPT_MODE, sksSpec);
  37. return new String(cipher.doFinal(decodeBase64(codeBase64)));
  38. }
  39. public static byte[] decodeBase64(String base64) throws Exception {
  40. InputStream fromBase64 = MimeUtility.decode(
  41. new ByteArrayInputStream(base64.getBytes()), "base64");
  42. byte[] buf = new byte[1024];
  43. ByteArrayOutputStream toByteArray = new ByteArrayOutputStream();
  44. for (int len = -1;(len = fromBase64.read(buf)) != -1;)
  45. toByteArray.write(buf, 0, len);
  46. return toByteArray.toByteArray();
  47. }
  48. public static String decodeBase641(String base64) throws Exception {
  49. InputStream fromBase64 = MimeUtility.decode(
  50. new ByteArrayInputStream(base64.getBytes()), "base64");
  51. byte[] buf = new byte[1024];
  52. ByteArrayOutputStream toByteArray = new ByteArrayOutputStream();
  53. for (int len = -1;(len = fromBase64.read(buf)) != -1;)
  54. toByteArray.write(buf, 0, len);
  55. return toByteArray.toString();
  56. }
  57. }