I have a Java library to read and decrypt password. 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. I can't figure out the correct command using openssl to do the job.
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.
You should be able to create the hmac using: echo "secret" | openssl dgst -sha1 -hmac "key"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.