How to decrypt file in Java encrypted with openssl command using AES?

前端 未结 4 1722
萌比男神i
萌比男神i 2020-11-22 13:31

I need to decrypt in JAVA a file encrypted in UNIX with the following command:

openssl aes-256-cbc -a -salt -in password.txt -out password.txt.enc
mypass
myp         


        
4条回答
  •  孤城傲影
    2020-11-22 14:01

    Don't use ase-128-cbc, use ase-128-ecb.

    only take first 16 bytes as key because key is 128 bits

    hash output is printed in hex, which every 2 chars presents a byte value

    hashpwd=echo -n $password| openssl sha1 | sed 's#.*=\\s*##g' | cut -c 1-32

    openssl enc -aes-128-ecb -salt -in -out -K $hashpwd

    Java Code is here:

    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    import java.io.*;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    
        //openssl enc -nosalt -aes-128-ecb
        // -in 
        // -out 
        // -K <16 bytes in hex, for example : "abc" can be hashed in SHA-1, the first 16 bytes in hex is a9993e364706816aba3e25717850c26c>
        private final static String TRANSFORMATION = "AES"; // use aes-128-ecb in openssl
    
    public static byte[] encrypt(String passcode, byte[] data) throws CryptographicException {
            try {
                Cipher cipher = Cipher.getInstance(TRANSFORMATION);
                cipher.init(Cipher.ENCRYPT_MODE, genKeySpec(passcode));
                return cipher.doFinal(data);
            } catch (Exception ex) {
                throw new CryptographicException("Error encrypting", ex);
            }
        }
    
    
        public static String encryptWithBase64(String passcode, byte[] data) throws CryptographicException {
            return new BASE64Encoder().encode(encrypt(passcode, data));
        }
    
        public static byte[] decrypt(String passcode, byte[] data) throws CryptographicException {
            try {
                Cipher dcipher = Cipher.getInstance(TRANSFORMATION);
                dcipher.init(Cipher.DECRYPT_MODE, genKeySpec(passcode));
                return dcipher.doFinal(data);
            } catch (Exception e) {
                throw new CryptographicException("Error decrypting", e);
            }
        }
    
    
        public static byte[] decryptWithBase64(String passcode, String encrptedStr) throws CryptographicException {
            try {
                return decrypt(passcode, new BASE64Decoder().decodeBuffer(encrptedStr));
            } catch (Exception e) {
                throw new CryptographicException("Error decrypting", e);
            }
        }
    
        public static SecretKeySpec genKeySpec(String passcode) throws UnsupportedEncodingException, NoSuchAlgorithmException {
            byte[] key = passcode.getBytes("UTF-8");
            MessageDigest sha = MessageDigest.getInstance("SHA-1");
            key = sha.digest(key);
            key = Arrays.copyOf(key, 16); // use only first 128 bit
            return new SecretKeySpec(key, TRANSFORMATION);
        }
    

    Tested and passed in jdk6 and jdk8.

提交回复
热议问题