【问题标题】:Connect to Wildfly Elytron's Credential Store with Masked Password使用蒙面密码连接到 Wildfly Elytron 的凭证存储
【发布时间】:2022-01-31 07:25:24
【问题描述】:

我有一个使用 Elytron 的工具创建的凭证存储,它提供了一个明文密码:“mypassword”。在我的 Java 程序中,我可以使用以下代码连接到商店;

Password storePassword = ClearPassword.createRaw(ClearPassword.ALGORITHM_CLEAR,"mypassword");
CredentialStore.ProtectionParameter protectionParameter = new CredentialStore.CredentialSourceProtectionParameter(
                    IdentityCredentials.NONE.withCredential(new PasswordCredential(storePassword)));
Provider provider = new WildFlyElytronPasswordProvider();
Security.addProvider(provider);
CredentialStore credentialStore = CredentialStore.getInstance(KeyStoreCredentialStore.KEY_STORE_CREDENTIAL_STORE);
// Configure and Initialise the CredentialStore
String configPath = System.getProperty("jboss.server.data.dir");
Map<String, String> configuration = new HashMap<>();
String path = configPath + File.separator + "credentials" + File.separator + "csstore.jceks";
configuration.put("keyStoreType", "JCEKS");
configuration.put("location", path);
configuration.put("modifiable", "false");
//Initialize credentialStore
credentialStore.initialize(configuration, protectionParameter);

但是,我现在想使用加密密码而不是明文连接到凭据存储。为此,我再次使用 Elytron 的工具通过以下命令创建了“mypassword”的 Masked Passowrd;

elytron-tool.sh mask --salt 12345678 --iteration 123 --secret mypassword;

这里的盐值和迭代值是随机的,可以是任何值。上面的命令给了我掩码的密码;

MASK-38PaKyS.9hHaRq7pAaE5tB;12345678;123

我现在需要一种方法来在我的 Java 程序中使用此屏蔽密码连接到凭证存储。我发现还有一个名为“MaskedPassword”的类,我可能会使用它,但我不知道如何使用。

有什么建议吗?

【问题讨论】:

    标签: java wildfly credentials elytron


    【解决方案1】:

    我们可以使用下面的代码来创建它...

    Password storePassword = MaskedPassword.createRaw(MaskedPassword.ALGORITHM_MASKED_MD5_DES, &lt;CREDENTIAL_STORE_ENTRY_PREFIX&gt;.toCharArray(), 120,"12345678".getBytes(StandardCharsets.UTF_8),"MASK-38PaKyS.9hHaRq7pAaE5tB".getBytes(StandardCharsets.UTF_8)); …… ....

    【讨论】:

    • 您尝试过解决方案吗?我们已经尝试过,但没有奏效。 = "一些无关紧要的任意疯狂字符串"
    • 什么是 CREDENTIAL_STORE_ENTRY_PREFIX ?
    【解决方案2】:

    当您使用 elytron 工具生成掩码密码时,您会得到带有前缀 MASK- 和带有盐和迭代的后缀的字符串 在你的情况下 - MASK-38PaKyS.9hHaRq7pAaE5tB;12345678;123

    您可以使用下面的代码来解密被屏蔽的密码,

    private char[] getUnmaskedPass(String maskedPassword) throws GeneralSecurityException {
            int maskLength = enter code here"MASK-".length();
            if (maskedPassword == null || maskedPassword.length() <= maskLength) {
                throw new GeneralSecurityException();
            }
            String[] parsed = maskedPassword.substring(maskLength).split(";");
            if (parsed.length != 3) {
                throw new GeneralSecurityException();
            }
            String encoded = parsed[0];
            String salt = parsed[1];
            int iteration = Integer.parseInt(parsed[2]);
            PasswordBasedEncryptionUtil encryptUtil = new PasswordBasedEncryptionUtil.Builder().picketBoxCompatibility().salt(salt).iteration(iteration)
                    .decryptMode().build();
    
            return encryptUtil.decodeAndDecrypt(encoded);
        }
    

    现在您可以在您的代码中使用它作为 clearPassword。我希望这会有所帮助。

    来源 - https://github.com/wildfly-security/wildfly-elytron-tool/blob/master/src/main/java/org/wildfly/security/tool/MaskCommand.java static char[] decryptMasked(String maskedPassword)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 2022-06-13
      • 2021-12-25
      • 1970-01-01
      • 2019-11-04
      相关资源
      最近更新 更多