显示密钥库证书的别名密码

时间:2013-08-22 08:58:00

标签: java security

我使用下面的代码显示与我的密钥库证书关联的别名,它工作正常,现在如何显示别名passowrd?有什么方法吗?

public class keyaliasfinder {

    public static void main(String args[])
    {
        FileInputStream is = null;
        try {

            File file = new File("c:\\my_keystore");
            is = new FileInputStream(file);
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            String password = "FnvUKHdr6b4343dfdf";
            keystore.load(is, password.toCharArray());

            PrivateKey p=new PrivateKey() {

                @Override
                public String getFormat() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public byte[] getEncoded() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public String getAlgorithm() {
                    // TODO Auto-generated method stub
                    return null;
                }
            };


            Enumeration enumeration = keystore.aliases();
            while(enumeration.hasMoreElements()) {
                String alias = (String)enumeration.nextElement();
                System.out.println("alias name: " + alias);
                Certificate certificate = keystore.getCertificate(alias);
                System.out.println(certificate.toString());

            }

        } catch (java.security.cert.CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != is)
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我对你的问题有点困惑。如果您尝试重新检索别名密码,则密码不会以明文密码的形式存在。密码用于通过基于密码的派生函数生成加密密钥,密钥用于保护您的私钥。但是,如果您要查找API以传递别名密码,一旦您使用密钥库密码加载密钥库(这就是您所做的),您可以传递别名密码,如下所示。

Key privateKey = keystore.getKey(String alias, char[] aliasPassword)

KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(aliasPassword);

//Get my private key
KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry)ks.getEntry("privateKeyAlias", protParam);
PrivateKey myPrivateKey = pkEntry.getPrivateKey();