Skip to content

Instantly share code, notes, and snippets.

@zeroflag
Created March 13, 2024 13:52
Show Gist options
  • Save zeroflag/39babfb1c539e973b384c60acb7ca2c5 to your computer and use it in GitHub Desktop.
Save zeroflag/39babfb1c539e973b384c60acb7ca2c5 to your computer and use it in GitHub Desktop.
import javax.crypto.SecretKey;
import java.io.FileInputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.KeyStore;
public class ShowKey {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java ShowKey </path/to/jceks> <password> <alias>");
System.out.println("Example java ShowKey /var/lib/knox/gateway/data/security/keystores/knoxsso-credentials.jceks knoxsecret pac4j.password");
System.exit(1);
}
String fileName = args[0];
char[] password = args[1].toCharArray();
String alias = args[2];
try (FileChannel fileChannel = FileChannel.open(Paths.get(fileName),StandardOpenOption.READ)) {
fileChannel.lock(0L, Long.MAX_VALUE, true);
}
KeyStore ks = KeyStore.getInstance("JCEKS");
try (FileInputStream fis = new FileInputStream(fileName)) {
ks.load(fis, password);
SecretKey secretKey = (SecretKey) ks.getKey(alias, password);
System.out.println(new String(secretKey.getEncoded()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment