Selenium Webdriver Encrypting and Decrypting a password/string in Core Java!!!
For Security reasons, most of the companies has restriction that security information like passwords should not written any where in the programs or xls file in plain text format.
So, in Selenium Webdriver instead of passing plain text for password as an input for the program. We can encrypt the password and pass the encrypted string in the program or xls, and while entering the data in the webpage we can decrypt and enter the password.
This way we can manage the security information, not to steal from others. In order to implement this we should under a concept called cryptography.
Cryptography is a method of storing and transmitting data in a particular form so that only those for whom it is intended can read and process it. The term is most often associated with scrambling plaintext (ordinary text, sometimes referred to as cleartext) into ciphertext (a process called encryption), then back again (known as decryption).
DES(Data Encryption Standard) works by using the same key to encrypt and decrypt a message, so both the sender and the receiver must know and use the same private key. Once the go-to, symmetric-key algorithm for the encryption of electronic data, DES has been superseded by the more secure Advanced Encryption Standard (AES) algorithm.
Encodes the bytes to Base64 using the class Base64.Encoder.
UTF-8 (U from Universal Character Set + Transformation Format—8-bit[1]) is a character encoding capable of encoding all possible characters (called code points) in Unicode.
Program for Implementing the Encryption and Decryption of the password:
package javaprograms;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class EncryptDecryptPassword {
Cipher ecipher;
Cipher dcipher;
EncryptDecryptPassword(SecretKey key) throws Exception {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
public String encrypt(String str) throws Exception {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
public String decrypt(String str) throws Exception {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
public static void main(String[] args) throws Exception {
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
EncryptDecryptPassword encrypter = new EncryptDecryptPassword(key);
String password="selenium";
String encryptedPassword = encrypter.encrypt(password);
String decryptedPassword = encrypter.decrypt(encryptedPassword);
System.out.println(encryptedPassword);
System.out.println(decryptedPassword);
}
}
Output:
Encrypted Password: as/OOfwqcj6ODk6DZshWzQ==
Decrypted Password: selenium
So, in Selenium Webdriver instead of passing plain text for password as an input for the program. We can encrypt the password and pass the encrypted string in the program or xls, and while entering the data in the webpage we can decrypt and enter the password.
This way we can manage the security information, not to steal from others. In order to implement this we should under a concept called cryptography.
Cryptography is a method of storing and transmitting data in a particular form so that only those for whom it is intended can read and process it. The term is most often associated with scrambling plaintext (ordinary text, sometimes referred to as cleartext) into ciphertext (a process called encryption), then back again (known as decryption).
DES(Data Encryption Standard) works by using the same key to encrypt and decrypt a message, so both the sender and the receiver must know and use the same private key. Once the go-to, symmetric-key algorithm for the encryption of electronic data, DES has been superseded by the more secure Advanced Encryption Standard (AES) algorithm.
Encodes the bytes to Base64 using the class Base64.Encoder.
UTF-8 (U from Universal Character Set + Transformation Format—8-bit[1]) is a character encoding capable of encoding all possible characters (called code points) in Unicode.
Program for Implementing the Encryption and Decryption of the password:
package javaprograms;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class EncryptDecryptPassword {
Cipher ecipher;
Cipher dcipher;
EncryptDecryptPassword(SecretKey key) throws Exception {
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
}
public String encrypt(String str) throws Exception {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
public String decrypt(String str) throws Exception {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
public static void main(String[] args) throws Exception {
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
EncryptDecryptPassword encrypter = new EncryptDecryptPassword(key);
String password="selenium";
String encryptedPassword = encrypter.encrypt(password);
String decryptedPassword = encrypter.decrypt(encryptedPassword);
System.out.println(encryptedPassword);
System.out.println(decryptedPassword);
}
}
Output:
Encrypted Password: as/OOfwqcj6ODk6DZshWzQ==
Decrypted Password: selenium
1 comments:
This is awesome!!!
Post a Comment