EncryptedProperties.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package com.cloudsoft.utils;
  7. import java.util.Properties;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.SecretKey;
  10. import javax.crypto.SecretKeyFactory;
  11. import javax.crypto.spec.PBEParameterSpec;
  12. /**
  13. *
  14. * @author Paul
  15. */
  16. public class EncryptedProperties extends Properties {
  17. private Cipher encrypter, decrypter;
  18. private static byte[] salt = {(byte) 0x00, 0x04, 0x00, 0x07, 0x01, 0x09, 0x06, 0x07}; // make up your own
  19. public EncryptedProperties() {
  20. this(salt);
  21. }
  22. public EncryptedProperties(byte[] bSalt) {
  23. try {
  24. String pw = "";
  25. int is = 80;
  26. while (pw.length() < 20) {
  27. is = is + pw.length() * ((pw.length()%2 == 1) ? -1:1);
  28. pw += (char) is;
  29. }
  30. PBEParameterSpec ps = new javax.crypto.spec.PBEParameterSpec(bSalt, 20);
  31. SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
  32. SecretKey k = kf.generateSecret(new javax.crypto.spec.PBEKeySpec(pw.toCharArray()));
  33. encrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
  34. decrypter = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
  35. encrypter.init(Cipher.ENCRYPT_MODE, k, ps);
  36. decrypter.init(Cipher.DECRYPT_MODE, k, ps);
  37. } catch (Exception ex) {
  38. System.out.println(ex.getMessage());
  39. }
  40. }
  41. public String getProperty(String key) {
  42. try {
  43. return decrypt(super.getProperty(key));
  44. } catch (Exception e) {
  45. throw new RuntimeException("Couldn't decrypt property");
  46. }
  47. }
  48. public synchronized Object setProperty(String key, String value) {
  49. return setProperty(key, value, true);
  50. }
  51. public synchronized Object setProperty(String key, String value, boolean bEncrypt) {
  52. try {
  53. return super.setProperty(key, bEncrypt ? encrypt(value) : value);
  54. } catch (Exception e) {
  55. throw new RuntimeException("Couldn't encrypt property");
  56. }
  57. }
  58. public synchronized Object setNormalProperty(String key, String value) {
  59. try {
  60. return super.setProperty(key, value);
  61. } catch (Exception e) {
  62. throw new RuntimeException("Couldn't encrypt property");
  63. }
  64. }
  65. private synchronized String decrypt(String str) throws Exception {
  66. byte[] dec = Base64.decode(str);
  67. byte[] utf8 = decrypter.doFinal(dec);
  68. return new String(utf8, "UTF-8");
  69. }
  70. private synchronized String encrypt(String str) throws Exception {
  71. byte[] utf8 = str.getBytes("UTF-8");
  72. byte[] enc = encrypter.doFinal(utf8);
  73. return Base64.encode(enc);
  74. }
  75. }