How to encode or encrypt password and other personal information in Selenium Webdriver using Java

Daily we sign in many popular websites like Facebook, Gmail, LinkedIn etc. with login credential. We need to send password as plain text to sign in those popular websites if want to write automation login script in selenium webdriver. One of the most common practice to send encrypted passwords and other personal information instead of plain text for the automation login script.

Base64: Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation. The term Base64 originates from a specific MIME content transfer encoding.
 byte[] encodedBytes = Base64.encodeBase64("TextforEncoding".getBytes());  
 System.out.println("encodedBytes " + new String(encodedBytes));  
 byte[] decodedBytes = Base64.decodeBase64(encodedBytes);  
 System.out.println("decodedBytes " + new String(decodedBytes));  
Prerequisite
1. Create a 'data.properties' file your project home directory
2. Download Base64 jar file and add jar file in project as external jar.

Demo Source code:
 import org.apache.commons.codec.binary.Base64;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import java.io.File;  
 import java.io.FileInputStream;  
 import java.io.IOException;  
 import java.util.Properties;  
 import java.util.concurrent.TimeUnit;    
   
 public class Encryptionpassword {  
   private static By email = By.id("email");  
   private static By password = By.id("pass");  
   private static By loginbutton = By.id("u_0_m");  
   
   public static void main(String[] arg) throws IOException {  
   
     // Create FileInputStream Object  
     FileInputStream fileInput = new FileInputStream(new File("data.properties"));  
     // Create Properties object  
     Properties prop = new Properties();  
     //load properties file  
     prop.load(fileInput);  
   
     // create Firebox object  
     WebDriver driver = new FirefoxDriver();  
     //Go to URL  
     driver.get("http://www.facebook.com");  
     //Maximize browser window  
     driver.manage().window().maximize();  
     //Set timeout  
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
   
     driver.findElement(email).clear();  
     driver.findElement(email).sendKeys(prop.getProperty("username"));  
   
     driver.findElement(password).clear();  
     driver.findElement(password).sendKeys(new String(Base64.encodeBase64(prop.getProperty("password").getBytes())));  
   
     driver.findElement(loginbutton).click();  
   
     //Print Encrypted Password  
     System.out.println("Encrypted Password =: " + new String(Base64.encodeBase64(prop.getProperty("password").getBytes())));  
     //Print Decrypted Password  
     System.out.println("Decrypted Password =: " + new String(Base64.decodeBase64(Base64.encodeBase64(prop.getProperty("password").getBytes()))));  
   
     //close firefox browser  
     driver.close();  
   }  
 }  
   

Output:

No comments:

Post a Comment