在 Eclipse 中创建一个 .jks 文件来存储 Keystore

Creating a .jks file to store Keystore in Eclipse

现在我正在尝试创建一个 Java 程序,它将能够使用密钥 encrypt/decrypt 数据。我目前的问题是:

  1. 如何在 Eclipse 中创建一个 .jks 文件,以便任何运行它的计算机都会自动创建该文件?

    和 2,这是我目前用来创建新密钥库的代码:

    java.io.FileInputStream fis = null;
    try {
        fis = new java.io.FileInputStream("keyStoreName"); // This is where 
                                                           // the empty .jks file would go
    } finally {
        if (fis != null) {
            fis.close();
        }
    }
    keystore.load(fis, keyPassword);
    

Eclipse 是否会在项目文件夹中查找具有字符串名称的任何 .jks 文件? (由 keyStorename 代表)

您可以使用 KeyStore class 轻松创建 JKS 密钥库:

        File f = new File("test.jks");
        KeyStore keyStore = KeyStore.getInstance("JKS");
        keyStore.load(null, null); // you have to initialize it

        //keyStore.set...args // add what you want

        try (FileOutputStream fout = new FileOutputStream(f)) {
            keyStore.store(fout, "changeit".toCharArray());
        }