Skip to content

Instantly share code, notes, and snippets.

@d-smith
Last active October 24, 2019 11:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save d-smith/c6a9d84e33466a530ca8 to your computer and use it in GitHub Desktop.
Save d-smith/c6a9d84e33466a530ca8 to your computer and use it in GitHub Desktop.
Read and write passwords to a Java keystore file
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;
}
}
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());
}
}
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