Monday, 23 October 2017

DataSource passwords Decryption in JBOSS EAP 6/7

JBoss Uses Pretty Simple process to encrypt and decrypt plain text Strings to encrypt. The encryption is provided by JBoss. The code for encryption and decryption are as below. 


import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;


public class DecodeIdentity {
   
   private static String encode(String secret) throws NoSuchPaddingException, NoSuchAlgorithmException,InvalidKeyException, BadPaddingException, IllegalBlockSizeException{
      byte[] kbytes = "jaas is the way".getBytes();
      SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
      Cipher cipher = Cipher.getInstance("Blowfish");
      cipher.init(Cipher.ENCRYPT_MODE, key);
      byte[] encoding = cipher.doFinal(secret.getBytes());
      BigInteger n = new BigInteger(encoding);
      return n.toString(16);
   }

   private static char[] decode(String secret)throws NoSuchPaddingException, NoSuchAlgorithmException,InvalidKeyException, BadPaddingException, IllegalBlockSizeException{
      byte[] kbytes = "jaas is the way".getBytes();
      SecretKeySpec key = new SecretKeySpec(kbytes, "Blowfish");
      BigInteger n = new BigInteger(secret, 16);
      byte[] encoding = n.toByteArray();
      //SECURITY-344: fix leading zeros
      if (encoding.length % 8 != 0){
         int length = encoding.length;
         int newLength = ((length / 8) + 1) * 8;
         int pad = newLength - length; //number of leading zeros
         byte[] old = encoding;
         encoding = new byte[newLength];
         for (int i = old.length - 1; i >= 0; i--){
            encoding[i + pad] = old[i];
         }
         if (n.signum() == -1){
            for (int i = 0; i < newLength - length; i++){
               encoding[i] = (byte) -1;
            }
         }
      }
      Cipher cipher = Cipher.getInstance("Blowfish");
      cipher.init(Cipher.DECRYPT_MODE, key);
      byte[] decode = cipher.doFinal(encoding);
      return new String(decode).toCharArray();
   }
   public static void main(String[] args) throws Exception{
      if(args.length == 2){
          if (args[0].equals("encode")){
            String encode = encode(args[1]);
            System.out.println("Encoded password: " + encode);
          }else if (args[0].equals("decode")){
            System.out.println(decode(args[1])); 
          }else{
              System.out.println("Function not defined");
          }
      }
   }
}



Copy the above Code and create a file DecodeIdentity.java and place the file in home Directory:

Let see how this java code will work:




please subscribe my blog  and provide the feedback on this article/blog to improve the articles.

1 comment:

Anonymous said...

Hello,

thank you for your article.
I'm trying to find how to decrypt datasource password in windows. I Have tried your code but maybe i do something wrong, and it doesn't work.
I created a file named DecodeIdentity.java with the content of your article and save it in the Java folder in Program files.
After that i called cmd and start the file, but it only open the file.

Could you please help me with this?

Thank you!