-
-
Save adrianprecub/2b2103e14cf856b7bdb8b39a85808d83 to your computer and use it in GitHub Desktop.
Read and write passwords to a Java keystore file
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 keystuff; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.security.KeyStore; | |
public class KeyUtils { | |
public static FileInputStream getFileInputStreamFromArg(String filePath) throws FileNotFoundException { | |
File file = new File(filePath); | |
return new FileInputStream(file); | |
} | |
public static KeyStore loadKeyStoreFromFile(String pathToFile, String keystorePassword) | |
throws Exception { | |
KeyStore keyStore = KeyStore.getInstance("JCEKS"); | |
keyStore.load(getFileInputStreamFromArg(pathToFile), keystorePassword.toCharArray()); | |
return keyStore; | |
} | |
} |
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 keystuff; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.PBEKeySpec; | |
import java.security.KeyStore; | |
import static keystuff.KeyUtils.loadKeyStoreFromFile; | |
public class ReadPasswordFromKeyStore { | |
public static void main(String[] args) throws Exception { | |
checkArgs(args); | |
String pathToKeyStore = args[0]; | |
String keystorePassword = args[1]; | |
String passwordPassword = args[2]; | |
String passwordAlias = args[3]; | |
KeyStore keyStore = loadKeyStoreFromFile(pathToKeyStore, keystorePassword); | |
System.out.println("read password " + readPasswordFromKeyStore(keyStore, passwordPassword, passwordAlias)); | |
} | |
private static void checkArgs(String[] args) { | |
if(args.length != 4) { | |
throw new IllegalArgumentException("Usage: ReadPasswordFromKeyStore <full path to keystore> <keystore password> <password password> <key alias>"); | |
} | |
} | |
private static String readPasswordFromKeyStore(KeyStore keyStore, String passwordPassword, String passwordAlias) throws Exception { | |
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(passwordPassword.toCharArray()); | |
KeyStore.SecretKeyEntry ske = | |
(KeyStore.SecretKeyEntry)keyStore.getEntry(passwordAlias, keyStorePP); | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE"); | |
PBEKeySpec keySpec = (PBEKeySpec)factory.getKeySpec( | |
ske.getSecretKey(), | |
PBEKeySpec.class); | |
return new String(keySpec.getPassword()); | |
} | |
} |
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 keystuff; | |
import javax.crypto.SecretKey; | |
import javax.crypto.SecretKeyFactory; | |
import javax.crypto.spec.PBEKeySpec; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.security.KeyStore; | |
import static keystuff.KeyUtils.loadKeyStoreFromFile; | |
public class WritePasswordToKeyStore { | |
private static void checkArgs(String[] args) { | |
if(args.length != 5) { | |
throw new IllegalArgumentException("Usage: WritePasswordToKeyStore <full path to keystore> <keystore password> <password password> <key alias> <password to store>"); | |
} | |
} | |
private static void writePasswordToKeyStore(String pathToKeyStore, String keyStorePassword, String passwordPassword, String alias, String password) | |
throws Exception { | |
KeyStore keyStore = loadKeyStoreFromFile(pathToKeyStore, keyStorePassword); | |
KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(passwordPassword.toCharArray()); | |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE"); | |
SecretKey generatedSecret = | |
factory.generateSecret(new PBEKeySpec( | |
password.toCharArray(), | |
"oh we're salty allright".getBytes(), | |
13 | |
)); | |
keyStore.setEntry(alias, new KeyStore.SecretKeyEntry( | |
generatedSecret), keyStorePP); | |
FileOutputStream outputStream = new FileOutputStream(new File(pathToKeyStore)); | |
keyStore.store(outputStream, keyStorePassword.toCharArray()); | |
} | |
public static void main(String[] args) throws Exception{ | |
checkArgs(args); | |
String pathToKeyStore = args[0]; | |
String keystorePassword = args[1]; | |
String passwordPassword = args[2]; | |
String passwordAlias = args[3]; | |
String passwordToStore = args[4]; | |
writePasswordToKeyStore(pathToKeyStore, keystorePassword, passwordPassword, passwordAlias, passwordToStore); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment