Using Encrypted Passwords in Selenium Webdriver Automation

Lately I stumble across a situation in which I had to use encrypted passwords in Selenium test scripts, so I am writing this handy utility which is basic yet useful.

We can achieve this by using Java.util.base64 class.

Base64 encoding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred over media that are designed to deal with textual data. This is to ensure that the data remains intact without modification during transport. Base64 is commonly used in a number of applications including email via MIME and storing complex data in XML (Source – wiki)

Example:

public static String fbPassword() {

  String decryptedPassword, encryptedPassword;
  encryptedPassword=”VGVzdDAxKga=”;
  byte[] decryptedPasswordBytes = Base64.getDecoder().decode(encryptedPassword);
  decryptedPassword = new String(decryptedPasswordBytes);
 return decryptedPassword;
}

Here i’m returing decrypted password to login method to login to the application. you can get encrypted string of your password using gectEncoder() method of Base64 class. see below code snipet for the same.

String pass1=”Test”;

String encodedBytes = Base64.getEncoder().encodeToString(“Test01*”.getBytes());

getBytes() encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array. To make this platfrom independent provide specific charset name like getBytes(“UTF-8″) or getBytes( “ISO-8859-1“ ).

And getDecoder() decrypts encrypted password in to byte array and this byte array need to be converted into string using String(byte[]) method.

We can also do this using in another way using Base64 class methods – decodeBase64() and encodeBase64(). These methods are available in org-apche-commons-codec JAR

import org.apche.commons.codec.binary.Base64

String pass1 = “Test”;

byte[] encodedPwdBytes = Base64.encodeBase64(pass1.getBytes());

byte[] decodedPwdBytes = Base64.decodeBase64(encodedPwdBytes);

String decodedPwd= new String(decodedPwdBytes));

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s