简体   繁体   English

如何将两个“.jceks”文件合并为一个?

[英]How can I merge two ".jceks" file into one?

I have two files kestore1.jceks and keystore2.jceks .我有两个文件kestore1.jcekskeystore2.jceks I want to merge them and load into KeyStore object.我想将它们合并并加载到 KeyStore object 中。

KeyStore KEY_STORE = KeyStore.getInstance("JCEKS");

input1 = new FileInputStream("C:\\Users\\tushar.moradiya\\Desktop\\keystore\\keystore1.jceks");
KEY_STORE.load(input1, Constant.JCEKS_KEYSTORE_FILE_PASSWORD.toCharArray());

input2 = new FileInputStream("C:\\Users\\tushar.moradiya\\Desktop\\keystore\\keystore2.jceks");
KEY_STORE.load(input2, Constant.JCEKS_KEYSTORE_FILE_PASSWORD.toCharArray());

but it is not working.但它不工作。 It overrides the last file in object.它覆盖 object 中的最后一个文件。

You can't read them into one object, but you can read them into two objects and then combine the contents, at least where they don't conflict:您不能将它们读入一个 object,但您可以将它们读入两个对象,然后组合内容,至少在它们不冲突的地方:

    KeyStore ks1 = KeyStore.getInstance("jceks"), ks2 = KeyStore.getInstance("jceks");
    char[] pw = args[2].toCharArray(); KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection (pw);
    try( InputStream is1 = new FileInputStream(args[0]) ){ ks1.load(is1,pw); }
    try( InputStream is2 = new FileInputStream(args[1]) ){ ks2.load(is2,pw); }
    for( Enumeration<String> aliases = ks2.aliases(); aliases.hasMoreElements(); ){
        String alias = aliases.nextElement();
        if( ks1.containsAlias(alias) ){ /* conflicting entry -- what to do? */ }
        ks1.setEntry(alias,  ks2.getEntry(alias, pp), pp);
        // could check type (or other details) before deciding whether to include
    }

You can also combine them into a single file using keytool -importkeystore (and them read in that single file) but that isn't a programming solution and thus offtopic, or at least borderline.您还可以使用keytool -importkeystore将它们组合到一个文件中(并且它们会在该单个文件中读取),但这不是编程解决方案,因此不是主题,或者至少是边界。

There is other way to do this as well, using keytool command line utility.还有其他方法可以做到这一点,使用keytool命令行实用程序。 If this is going to be a one time thing, i recommend using the keytool.如果这将是一次性的事情,我建议使用 keytool。 But if this is going to be your business logic, then you can do it this way:但是,如果这将成为您的业务逻辑,那么您可以这样做:

    KeyStore first = KeyStore.getInstance("JCEKS");
    first.load(new FileInputStream(new File("first.jceks")), "password".toCharArray());

    KeyStore second = KeyStore.getInstance("JCEKS");
    second.load(new FileInputStream(new File("second.jceks")), "password".toCharArray());

    Enumeration<String> aliases = first.aliases();

    while (aliases.hasMoreElements())
    {
        String alias = aliases.nextElement();
        System.out.println(alias);

        KeyStore.Entry entry = null;
        try
        {
            entry = first.getEntry(alias, new KeyStore.PasswordProtection("password".toCharArray()));
        }
        catch (UnsupportedOperationException e)
        {
            entry = first.getEntry(alias, null);
        }

        if (entry instanceof TrustedCertificateEntry)
        {
            second.setCertificateEntry(alias, ((TrustedCertificateEntry) entry).getTrustedCertificate());
        }
        else
        {
            second.setEntry(alias, entry, new KeyStore.PasswordProtection("password".toCharArray()));
        }
    }

    second.store(new FileOutputStream(new File("second.jceks")), "password".toCharArray());

This copies all the first keystore entries into the second keystore.这会将所有第一个密钥库条目复制到第二个密钥库中。

PS: Keep a copy of the original keystores. PS:保留原始密钥库的副本。

You can use this command-line tool to copy JCEKS keystore entries from one keystore to another.您可以使用此命令行工具将 JCEKS 密钥库条目从一个密钥库复制到另一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM