Created
September 30, 2021 08:30
-
-
Save JunilJacob/2803f7b61aeb77377a93e423a6b641dc to your computer and use it in GitHub Desktop.
Encrypted Credentials Java Selenium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.NoSuchAlgorithmException; | |
import java.util.Base64; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
public class CryptoKeyGenerator { | |
public static void main(String[] args) throws NoSuchAlgorithmException { | |
SecretKey key = KeyGenerator.getInstance("AES").generateKey(); | |
String encodedKey = Base64 | |
.getEncoder() | |
.withoutPadding() | |
.encodeToString(key.getEncoded()); | |
System.out.println("New Secret Key: " + encodedKey); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package common.core.secrets; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Base64; | |
import javax.crypto.Cipher; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
public class CryptoProcessor { | |
public static final String SECRETS_PATH = | |
Paths | |
.get(System.getProperty("user.dir"), "src", "test", "resources", "secrets") | |
.toString() + | |
File.separator; | |
public static String environment = System.getProperty("env"); | |
public static void main(String[] args) { | |
environment = args[1]; | |
if (args[0].equalsIgnoreCase("encrypt")) { | |
CryptoProcessor.encrypt(); | |
} else if (args[0].equalsIgnoreCase("decrypt")) { | |
CryptoProcessor.decrypt(); | |
} | |
} | |
public static void decrypt() { | |
if (environment == null) { | |
environment = "uat"; | |
} | |
System.out.println("Started decrypting " + SECRETS_PATH + environment + ".enc"); | |
File inputFile = new File(SECRETS_PATH + environment + ".enc"); | |
File outputFile = new File(SECRETS_PATH + environment + ".secret"); | |
byte[] data = new byte[(int) inputFile.length()]; | |
try { | |
FileOutputStream decryptedFile = new FileOutputStream(outputFile); | |
FileInputStream encryptedFile = new FileInputStream(inputFile); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.DECRYPT_MODE, getSecretKey()); | |
encryptedFile.read(data); | |
byte[] decryptedText = cipher.doFinal(data); | |
decryptedFile.write(decryptedText); | |
decryptedFile.close(); | |
encryptedFile.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("Failed to decrypt secrets"); | |
} | |
} | |
public static void encrypt() { | |
System.out.println("Started encrypting " + SECRETS_PATH + environment + ".secret"); | |
File inputFile = new File(SECRETS_PATH + environment + ".secret"); | |
File outputFile = new File(SECRETS_PATH + environment + ".enc"); | |
byte[] data = new byte[(int) inputFile.length()]; | |
try { | |
FileOutputStream encryptedFile = new FileOutputStream(outputFile); | |
FileInputStream decryptedFile = new FileInputStream(inputFile); | |
Cipher cipher = Cipher.getInstance("AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey()); | |
decryptedFile.read(data); | |
byte[] textEncrypted = cipher.doFinal(data); | |
encryptedFile.write(textEncrypted); | |
encryptedFile.close(); | |
decryptedFile.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.out.println("Failed to encrypt secrets"); | |
} | |
} | |
private static SecretKey getSecretKey() throws IOException { | |
try { | |
Path masterKeyPath = Paths.get(SECRETS_PATH + "master.key"); | |
if (!Files.exists(masterKeyPath)) { | |
Files.createFile(masterKeyPath); | |
} | |
String encodedKey = System.getProperty("masterKey"); | |
if (encodedKey == null) { | |
encodedKey = Files.readAllLines(masterKeyPath).get(0); | |
} | |
byte[] decodedKey = Base64.getDecoder().decode(encodedKey); | |
SecretKey key = new SecretKeySpec(decodedKey, "AES"); | |
return key; | |
} catch (IOException e) { | |
System.out.println( | |
"Failed to get secret key. Check if master.key file is present." | |
); | |
throw (e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package common.core.secrets; | |
import common.core.configuration.Configuration; | |
import java.io.FileInputStream; | |
import java.nio.file.Paths; | |
import java.util.Properties; | |
public class Secret { | |
private static Properties secrets = new Properties(); | |
public static Boolean load() { | |
CryptoProcessor.decrypt(); | |
try { | |
String secretFilePath = Paths | |
.get(CryptoProcessor.SECRETS_PATH, Configuration.environment + ".secret") | |
.toString(); | |
secrets.load(new FileInputStream(secretFilePath)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return true; | |
} | |
public static void clear() { | |
secrets.clear(); | |
} | |
public static String get(String key) { | |
return secrets.getProperty(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment