Password Encryption and Decryption in selenium

Tahera Firdose
4 min readJul 23, 2021

In this article, we will learn how to use AES for encryption and decryption of password for selenium webdriver. we can use the same code for encrypting /decrypting of any data.

What is Java AES Encryption and Decryption?

Encryption is the process of converting plaintext data into an unreadable format using an encryption algorithm and an encryption key parameter. The unreadable format is often known as the ciphertext format. The data can only be decrypted and the original plaintext recovered by those who have the decryption key.

The process of encrypting passwords in configuration files can be divided into two sub-tasks.

1. Encrypt the password that is stored in plaintext in the file.

2. Decrypt the password that was read from the encrypted text.

AES includes three block ciphers: AES-128, AES-192 and AES-256.

AES-128 uses a 128-bit key length to encrypt and decrypt a block of messages, while AES-192 uses a 192-bit key length and AES-256 a 256-bit key length to encrypt and decrypt messages. Each cipher encrypts and decrypts data in blocks of 128 bits using cryptographic keys of 128, 192 and 256 bits, respectively.

Symmetric, also known as secret key, ciphers use the same key for encrypting and decrypting, so the sender and the receiver must both know — and use — the same secret key

So, let’s understand the Advance Encryption algorithm( AES) with the help of a coding example.

Below is the utility class which will explain how AES will be used for encryption and decryption of Password in Selenium WebDriver.

I have created a class called AESUtils.java where I have defined all the methods for creating the secret key, encryption and decryption of data.

· Secret Key

In this approach, the AES secret key is derived from a given password using a password-based key derivation function like PBKDF2. We also need a salt value for turning a password into a secret key. The salt is also a random value.

We can use the SecretKeyFactory class with the PBKDF2WithHmacSHA256 algorithm for generating a key from a given password.

Let’s define a method for generating the AES key from a given password with a key length of 256 bits:

Encryption

To implement encryption, we need to generate secret key as shown above. The next step is, we need to create an instance from the Cipher class by using the getInstance() method.

We also use the init() method to set up a cypher instance with a secret key, IV, and encryption mode. Finally, we encrypt the input string by invoking the doFinal() method.

For decrypting an input string, we can initialize our cipher using the Decrypt mode to decrypt the content:

Let’s write a test method for encrypting and decrypting a password:

config.properties file

Output:

Refer to below repository for complete code.

Test project repository: https://github.com/taherafirdose/Selenium_Encrypt_Decrypt.git

--

--

Tahera Firdose

Datascience - Knowledge grows exponentially when it is shared