简体   繁体   English

等效的openssl命令来加密密码

[英]equivalent openssl command to encrypt password

I have a Java library to read and decrypt password. 我有一个Java库可以读取和解密密码。 I follow how it reads password to generate the password file and it works fine. 我遵循它如何读取密码以生成密码文件的方式,并且工作正常。 Now I want to figure out how to use openssl command to generate the password file as that is our standard for support to use. 现在,我想弄清楚如何使用openssl命令生成密码文件,因为这是我们支持使用的标准。 I can't figure out the correct command using openssl to do the job. 我无法使用openssl找出正确的命令来完成这项工作。

Here is my test code to generate the password file. 这是我生成密码文件的测试代码。 it works fine. 它工作正常。

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);
        }
    }
}

I want to use openssl to generate the encrypted.txt file to achieve the same result. 我想使用openssl生成crypto.txt文件以实现相同的结果。

You should be able to create the hmac using: echo "secret" | 您应该能够使用以下命令创建hmac:echo“ secret” | openssl dgst -sha1 -hmac "key" openssl dgst -sha1 -hmac“密钥”

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

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