Test:
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.SecureRandom;
import java.util.Enumeration;
import sun.security.tools.keytool.CertAndKeyGen;
import sun.security.x509.X500Name;
public class WinKeyStoreTest {
public static void main(String[] args) throws Exception {
KeyStore keyStore = KeyStore.getInstance("Windows-MY");
keyStore.load(null, null);
System.out.println("keystore loaded");
listAlias(keyStore);
String alias = "test";
int number = 2;
System.out.println("add alias...");
CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
ckg.setRandom(new SecureRandom());
for (int i = 0; i < number; i++) {
ckg.generate(1024);
keyStore.setCertificateEntry(alias,
ckg.getSelfCertificate(new X500Name("CN=TEST"), 1000));
}
System.out.println("alias added");
listAlias(keyStore);
keyStore.load(null, null);
System.out.println("keystore reloaded");
listAlias(keyStore);
}
private static void listAlias(KeyStore keyStore) throws KeyStoreException {
System.out.println("===== alias list =====");
for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements();) {
String alias = e.nextElement();
System.out.println(alias);
}
System.out.println("======================");
}
}
Test output:
keystore loaded
===== alias list =====
======================
add alias...
alias added
===== alias list =====
test
======================
keystore reloaded
===== alias list =====
test
test (1)
======================
The test adds two entries with the same alias to Windows-MY keystore, before reload the keystore, it lists only one alias.