Decrypt AES256 encrypted bytes

后端 未结 2 1665
星月不相逢
星月不相逢 2021-01-03 16:56

I\'ve never worked with encryption before. Actually I know nothing about encryption. I have a file encrypted with openssl tool using params:

openssl a

相关标签:
2条回答
  • 2021-01-03 17:27

    Here's class I have written to decrypt a string encoded with params above (if I remmeber it correct):

    public class CipherUtils {
        public static byte[] getKey(String password, byte[] salt) {
            try {
                byte[] passwordSalt = EncodingUtils.getAsciiBytes(password);
                passwordSalt = concatenateByteArrays(passwordSalt, salt);
    
                byte[] hash1 = getHashForHash(null, passwordSalt);
                byte[] hash2 = getHashForHash(hash1, passwordSalt);
                byte[] key = concatenateByteArrays(hash1, hash2);
    
                return key;
            } catch (Exception e) {
                return null;
            }
    
        }
    
        public static byte[] getIV(String password, byte[] salt) {
            try {
                byte[] passwordSalt = EncodingUtils.getAsciiBytes(password);
                passwordSalt = concatenateByteArrays(passwordSalt, salt);
                byte[] hash1 = getHashForHash(null, passwordSalt);
                byte[] hash2 = getHashForHash(hash1, passwordSalt);
                byte[] hash3 = getHashForHash(hash2, passwordSalt);
                return hash3;
            } catch (Exception e) {
                return null;
            }
    
        }
    
        private static byte[] getHashForHash(byte[] hash, byte[] passwordSalt) {
            try {
                byte[] hashMaterial = concatenateByteArrays(hash, passwordSalt);
                MessageDigest md = MessageDigest.getInstance("MD5");
                return md.digest(hashMaterial);
            } catch (Exception e) {
                return null;
            }
        }
    
        private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
            if (a == null)
                return b;
            if (b == null)
                return a;
            byte[] result = new byte[a.length + b.length];
            System.arraycopy(a, 0, result, 0, a.length);
            System.arraycopy(b, 0, result, a.length, b.length);
            return result;
        }
    }
    

    Salt is an empty bytearray in this case. It uses apache-commons-compress.jar.

    Here's usage example:

    byte[] key = CipherUtils.getKey(password, null);
    byte[] IV = CipherUtils.getIV(password, null);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"),
            new IvParameterSpec(IV));
    cis = new CipherInputStream(is, cipher);
    

    Where is is an InputStream of encrypted data.

    0 讨论(0)
  • 2021-01-03 17:42

    this may helps you

    public void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
            // Here you read the cleartext.
            FileInputStream fis = new FileInputStream("data/cleartext");
            // This stream write the encrypted text. This stream will be wrapped by
            // another stream.
            FileOutputStream fos = new FileOutputStream("data/encrypted");
    
            // Length is 16 byte
            SecretKeySpec sks = new SecretKeySpec("yourkey".getBytes(), "AES");
            // Create cipher
            Cipher cipher = Cipher.getInstance("AES/CBC");
            cipher.init(Cipher.ENCRYPT_MODE, sks);
            // Wrap the output stream
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            // Write bytes
            int b;
            byte[] d = new byte[8];
            while ((b = fis.read(d)) != -1) {
                cos.write(d, 0, b);
            }
            // Flush and close streams.
            cos.flush();
            cos.close();
            fis.close();
        }
    

    Decrypt

    public  void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
            FileInputStream fis = new FileInputStream("data/encrypted");
    
            FileOutputStream fos = new FileOutputStream("data/decrypted");
            SecretKeySpec sks = new SecretKeySpec("yourkey".getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC");
            cipher.init(Cipher.DECRYPT_MODE, sks);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            int b;
            byte[] d = new byte[8];
            while((b = cis.read(d)) != -1) {
                fos.write(d, 0, b);
            }
            fos.flush();
            fos.close();
            cis.close();
        }
    
    0 讨论(0)
提交回复
热议问题