繁体   English   中英

等效的openssl命令来加密密码

[英]equivalent openssl command to encrypt password

我有一个Java库可以读取和解密密码。 我遵循它如何读取密码以生成密码文件的方式,并且工作正常。 现在,我想弄清楚如何使用openssl命令生成密码文件,因为这是我们支持使用的标准。 我无法使用openssl找出正确的命令来完成这项工作。

这是我生成密码文件的测试代码。 它工作正常。

import org.apache.commons.io.IOUtils;

public class CryptoTest extends TestCase {

    public void testEncryption() throws Exception {

        String DEFAULT_ALG = "AES/ECB/PKCS5Padding";
        String DEFAULT_SALT = "SALT";
        int DEFAULT_ITERATIONS = 10000;
        int DEFAULT_KEY_LEN = 128;

        String alg = DEFAULT_ALG;
        String salt = DEFAULT_SALT;
        int iterations = DEFAULT_ITERATIONS;
        int keyLen = DEFAULT_KEY_LEN;

        SecretKeyFactory factory = null;
        String passPhrase = "password";
        String algOnly = alg.split("/")[0];
        String password = "CDE#VFR$";

        try {
            factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("Can't load SecretKeyFactory", e);
        }

        SecretKeySpec key = null;
        try {
            key = new SecretKeySpec(
                    factory.generateSecret(
                            new PBEKeySpec(passPhrase.toCharArray(), salt.getBytes(), iterations, keyLen)).getEncoded(),
                    algOnly);
        } catch (Exception e) {
            throw new IOException("Can't generate secret key", e);
        }

        Cipher crypto = null;

        try {
            crypto = Cipher.getInstance(alg);
        } catch (Exception e) {
            throw new IOException("Can't initialize the decryptor", e);
        }

        byte[] encryptedBytes;

        try {
            crypto.init(Cipher.ENCRYPT_MODE, key);
            encryptedBytes = crypto.doFinal(password.getBytes());

            OutputStream os = new FileOutputStream("encrypted.txt");
            IOUtils.write(encryptedBytes, os);

        } catch (Exception e) {
            throw new IOException("Can't decrypt the password", e);
        }
    }
}

我想使用openssl生成crypto.txt文件以实现相同的结果。

您应该能够使用以下命令创建hmac:echo“ secret” | openssl dgst -sha1 -hmac“密钥”

暂无
暂无

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

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