从oracle密钥库中提取存储的密码

问题描述 投票:0回答:1

我们的客户抱怨Tomcat context.xml中存储的加密密钥是纯文本(嗯,他现在肯定是正确的)。并且他想使用外部密钥库来存储此加密密钥。

我能够使用以下命令创建密钥库并在其中放置对称密钥:

keytool -importpassword -alias encryption-key -keystore your.keystore -storetype pkcs12

此密钥库具有“PSCS12”类型,实际上可以存储对称密钥。我存储的密码有一个别名,即'encryption-key'。 'your.keystore'是一个密钥库文件。

但我有一个问题 - 我无法提取它。

如果我将尝试从java代码中提取if - 那么我将需要提供salt和迭代计数,如下所示:

final SecretKey secretKey = (SecretKey) keyStore.getKey(alias, password.toCharArray());
    System.out.println("[*] Encryption algorithm: " + secretKey.getAlgorithm());


    Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
    AlgorithmParameterSpec algorithmParameterSpec = new PBEParameterSpec(SALT, ITERATION_COUNT);

    cipher.init(Cipher.DECRYPT_MODE, secretKey, algorithmParameterSpec);
    String decryptedData = Arrays.toString(cipher.doFinal(secretKey.getEncoded()));
    System.out.println("Decrypted Key: " + decryptedData);

但我不确定我应该提供哪些值,因为我使用命令行存储我的密码。

正在使用的加密算法是PBEWithMD5AndDES。我可以在调试器会话中看到我存储的密码短语,我实际上甚至可以看到密码长度,但我无法解密它。

那么,我的选择是什么?客户希望拥有标准实施(JCA)。我怎样才能提取上面用命令生成的密码?

java cryptography keystore keytool privacy
1个回答
0
投票

忘掉它,我很蠢。事实证明,我总是拥有正确的价值,它只是采用HEX格式。

所以,如果你想拥有一个密钥库并放置一些值(只是一个字符串,而不是密钥对),那么你需要:

$ keytool -importpassword -alias encryption-key -keystore your.keystore -storetype pkcs12 -storepass testtest#创建一个密钥库并存储单个值

其中-importpassword用于存储单密码

-alias是您密码的别名

-keystore显然是一个密钥库文件

- storetype pkcs12用于存储对称密钥(只是密码,而不是密钥对)

-storepass是密钥库的密码(不是您的密码)

然后,您可以使用以下代码示例来提取密钥:

import javax.crypto.SecretKey;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;

public class Main {
    private static final String WORKING_DIRECTORY = "/path/to/directory/where/keystore/is/placed/";
    private static final String FILE_NAME = "your.keystore";
    private static final String KEYSTORE_PASSWORD = "testtest";
    private static final String SECRET_KEY_ALIAS = "encryption-key";

    public static void main(String[] argv) throws Exception {
        final FileInputStream is = new FileInputStream(WORKING_DIRECTORY + FILE_NAME); // load a keystore from file
        final KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); // initialize a keystore
        keystore.load(is, KEYSTORE_PASSWORD.toCharArray()); // authorize in the keystore

        extract(SECRET_KEY_ALIAS, KEYSTORE_PASSWORD, keystore); // extract stored password from the keystore
    }

    static void extract(final String alias, final String password, final KeyStore keyStore) throws Exception {
        final SecretKey secretKey = (SecretKey) keyStore.getKey(alias, password.toCharArray());
        System.out.println("[*] Encryption algorithm: " + secretKey.getAlgorithm());

        System.out.println("[*] Converting stored key from HEX to string");
        System.out.println("[+] Stored key: " + new String(secretKey.getEncoded(), StandardCharsets.UTF_8));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.