Encode , decode passwords using Base64 and Reading properties file in java

As a developer, I often use username and passwords in the test classes especially in integration test cases.

Sometime we forgot to delete this, which leads to code base having passwords and that is not safe. I normally use properties file to store username and password( encrypted ) using Base64 algorithm.

So I will run a java program and pass the real password and encode it and then store it in the properties files.

Whenever i need it , i will call the util program to decode and then use it in test cases.So that the password is not human readable.

I am going to write steps to show you how to encode and store it in properties file, then how to read properties file in java.

Step 1:
Create a maven project. Please check this link Creating maven java project
You can see a project structure like this:

in Eclipse as follows:

Step 2:
Now Please create a java class called PropertiesUtil.java with the following contents:

This class will contain methods to do the following:

  1. 1.Encode the given string
  2. 2.Decode the given string
  3. 3.Get the string from a properties file and/or decode it


package com.mksiva.app;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.Properties;
/**
*
* @author mksiva
*
*/
public class PropertiesUtil {

/**
* Encoding using base64
* @param aInput
* @return
*/
public static String encode(String aInput) {
byte[] bytesEncoded = Base64.getEncoder().encode((aInput).getBytes());
return new String(bytesEncoded);
}

/**
* Decoding using base64
* @param aEncrypterInput
* @return
*/
public static String decode(String aEncrypterInput) {
byte[] bytesDecoded = Base64.getDecoder().decode(aEncrypterInput);
return new String(bytesDecoded);
}

/**
* Getting the config.properties file as stream.
* @return an InputStream
*/
private InputStream getStream() {
InputStream in = getClass().getClassLoader().getResourceAsStream("config.properties");
return in;
}

/**
* Getting the password (encoded/not) and decode it and return it
* @param aPropertyKey
* @param aDecode
* @return decoded String
*/
public static String getValueFromPropertiesFile(String aPropertyKey, boolean aDecode) {

Properties prop = new Properties();
InputStream in = new PropertiesUtil().getStream();
try {
prop.load(in);
String propValue = prop.getProperty(aPropertyKey);
if (propValue != null && aDecode) {
return decode(propValue);
}
return propValue;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}

Step 3:
Create a property file called “config.properties” under “src/main/resources”.Encoding the password and store it in the properties file.(config.properties)

package com.mksiva.app;
/**
* App
*
*/
public class App
{
public static void main( String[] args )
{
String thePasswordToEncode = "password";
System.out.println(PropertiesUtil.encode(thePasswordToEncode));
}
}

Run the above program and get the password and store it in config.properties.

config.properties:
userName=test
passWord=cGFzc3dvcmQ=

Step 4:
Whenever I needed the password, I will first decode is using decodeFromPropertiesFile method from the PropertiesUtil.java, and then use it.

Testing the decode method:
package com.mksiva.app;

/**
* App
*
*/
public class App
{
public static void main( String[] args )
{
String decodedPassword = PropertiesUtil.getValueFromPropertiesFile("passWord", true);
System.out.println(decodedPassword);
}
}

The above program will print the decoded password string ‘password’. Which can be used in other parts of the application.

Leave a Reply

Your email address will not be published. Required fields are marked *