Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Encrypting and Decrypting Passwords Using Java in Selenium

    Encrypting and Decrypting Passwords Using Java in Selenium

    July 29, 2024

    Security is a crucial aspect of any application, especially when dealing with sensitive information such as passwords. Storing passwords in plain text can expose them to potential security risks. In this blog, we’ll discuss how to securely encrypt and decrypt passwords in Java and how to integrate this functionality into your Selenium automation scripts.

    Why Encrypt Passwords?

    Encrypting passwords ensures that they are stored in an unreadable format, reducing the risk of unauthorized access. Even if someone gains access to the stored data, encrypted passwords remain secure unless the encryption key is compromised.

    Prerequisites

    Before we begin, ensure you have the following:

    Java Development Kit (JDK) installed.
    Selenium WebDriver library added to your project.
    Basic understanding of Java and Selenium.

     

    Setting Up Encryption and Decryption

    We’ll use the javax.crypto package in Java, which provides the necessary classes for encryption and decryption. We’ll create two classes: EncryptionHelper for handling encryption and decryption, and SeleniumTest to demonstrate the integration with Selenium.

     

    Step 1: Create the EncryptionHelper Class

    This class contains methods to generate a secret key, encrypt a password, and decrypt a password.

    Import Necessary Packages

    import javax.crypto.Cipher;

    import javax.crypto.KeyGenerator;

    import javax.crypto.SecretKey;

    import java.util.Base64;

     

    Define the EncryptionHelper Class

    public class EncryptionHelper {

        private static final String ALGORITHM = “AES”; // Algorithm for encryption

        // Generate a secret key

        public static SecretKey generateKey() throws Exception {

            KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);

            keyGen.init(128); // Key size can be 128, 192, or 256 bits

            return keyGen.generateKey();

        }

        // Encrypt the password

        public static String encrypt(String password, SecretKey key) throws Exception {

            Cipher cipher = Cipher.getInstance(ALGORITHM);

            cipher.init(Cipher.ENCRYPT_MODE, key);

            byte[] encryptedPassword = cipher.doFinal(password.getBytes());

            return Base64.getEncoder().encodeToString(encryptedPassword);

        }

        // Decrypt the password

        public static String decrypt(String encryptedPassword, SecretKey key) throws Exception {

            Cipher cipher = Cipher.getInstance(ALGORITHM);

            cipher.init(Cipher.DECRYPT_MODE, key);

            byte[] decodedPassword = Base64.getDecoder().decode(encryptedPassword);

            byte[] originalPassword = cipher.doFinal(decodedPassword);

            return new String(originalPassword);

        }

    }

     

    Explanation

    generateKey(): Generates a secret key using the AES algorithm.
    encrypt(): Encrypts the given password using the secret key.
    decrypt(): Decrypts the given encrypted password using the secret key.

     

    Step 2: Create the SeleniumTest Class

    This class demonstrates how to use the EncryptionHelper class to encrypt and decrypt passwords within a Selenium script.

     

    Import Necessary Packages

    import javax.crypto.SecretKey;

     

    Define the SeleniumTest Class

    public class SeleniumTest {

        public static void main(String[] args) {

            try {

                // Generate a secret key

                SecretKey secretKey = EncryptionHelper.generateKey();

                // Original password
                String originalPassword = “password@123”;

                // Encrypt the password
                String encryptedPassword = EncryptionHelper.encrypt(originalPassword, secretKey);

                System.out.println(“Encrypted Password: ” + encryptedPassword);

                // Decrypt the password
                String decryptedPassword = EncryptionHelper.decrypt(encryptedPassword, secretKey);

                System.out.println(“Decrypted Password: ” + decryptedPassword);

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

    }

     

    Explanation

    generateKey(): Generates a secret key for encryption and decryption.
    encrypt(): Encrypts the original password.
    decrypt(): Decrypts the encrypted password back to its original form.

     

    Output:

    Integrating Encryption with Selenium

    To demonstrate the integration of password encryption with a Selenium test, we will extend the SeleniumTest class to include a simple login automation script.

    Import Selenium Packages

    import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

    import org.openqa.selenium.chrome.ChromeDriver;

     

    Update the SeleniumTest Class

    public class SeleniumTest {

        public static void main(String[] args) {

            try {

                // Generate a secret key

                SecretKey secretKey = EncryptionHelper.generateKey();

                // Original password
                String originalPassword = “password@123”;

                // Encrypt the password
                String encryptedPassword = EncryptionHelper.encrypt(originalPassword, secretKey);

                System.out.println(“Encrypted Password: ” + encryptedPassword);

                // Decrypt the password
                String decryptedPassword = EncryptionHelper.decrypt(encryptedPassword, secretKey);

                System.out.println(“Decrypted Password: ” + decryptedPassword);

                // Set up WebDriver
                System.setProperty(“webdriver.chrome.driver”, “path/to/chromedriver”);

                WebDriver driver = new ChromeDriver();

                // Navigate to the login page
                driver.get(“https://example.com/login”);

                // Find username and password fields
                WebElement usernameField = driver.findElement(By.id(“username”));

                WebElement passwordField = driver.findElement(By.id(“password”));

                // Enter username and decrypted password
                usernameField.sendKeys(“myUsername”);

                passwordField.sendKeys(decryptedPassword);

                // Submit the login form
                WebElement loginButton = driver.findElement(By.id(“loginButton”));

                loginButton.click();

                // Close the browser
                driver.quit();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

     

    Advantages of Encrypting Passwords

    Security: Encrypting passwords ensures that they are not stored in plain text, reducing the risk of unauthorized access.
    Data Protection: Even if the encrypted passwords are exposed, they remain secure without the decryption key.
    Compliance: Helps in complying with security standards and regulations that mandate encryption of sensitive data.

    Conclusion

    Encrypting and decrypting passwords in Java is a straightforward process that significantly enhances the security of your application. By integrating this functionality into your Selenium scripts, you can ensure that sensitive data, such as passwords, is handled securely. Follow the steps outlined in this blog to implement password encryption and decryption in your projects, and enjoy the peace of mind that comes with enhanced security.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMastering Page Properties With Granite Render Conditions and Context-Aware Configuration
    Next Article Understanding Encryption Algorithms: AES, DES, and Blowfish

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 16, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-2305 – Apache Linux Path Traversal Vulnerability

    May 16, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    TRC Staffing Data Breach Fallout: Murphy Law Firm Offers Legal Support to Victims

    Development

    The Ultimate Conversion Rate Optimization (CRO) Checklist

    Web Development

    Aftermarket Software Firm eViridis, Clients Face Unverified Data Breach Claims

    Development

    Christmas Sale on Dukakeen.com – Flat 20% Off!

    Development
    GetResponse

    Highlights

    Development

    DeepSeek AI Releases JanusFlow: A Unified Framework for Image Understanding and Generation

    November 13, 2024

    The field of AI-driven image generation and understanding has seen rapid progress, but significant challenges…

    CVE-2025-4495 – JAdmin-JAVA Cross-Site Scripting Vulnerability

    May 10, 2025

    Case Management Software: 4 Crucial Questions to Consider Before You Invest

    November 18, 2024

    Quatre raisons d’utiliser MongoDB 8.0

    November 4, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.