我如何在Java中解密openssl? [英] How could i decrypt openssl in Java?

查看:111
本文介绍了我如何在Java中解密openssl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过传递密钥来解密Java中的openssl加密文件.

I need to decrypt openssl encrypted file in Java by passing key.

我之前已经从下面的链接中进行了检查,但是它不包括显式键参数传递,并且逐行读取文件. 如何使用使用AES的openssl命令吗?

I have previously checked from the below link, but it does not include explicit key parameter passing and reads file line by line. How to decrypt file in Java encrypted with openssl command using AES?

与众不同的是,我的文件是整体加密的,而不是行加密的,并且我有一个显式密钥来解密文件.

As differently, my files encrypted as a whole rather than line encryption and I have an explicit key to decrypt the file.

另一个问题是我的文件太大,因此我不确定第一步中将文件整体保存在内存中的最佳方法.

The other issue is my file size is so big and I am not sure about the best method of saving the file in memory as a whole in the first step.

谢谢.

推荐答案

我需要通过传递密钥来解密Java中的openssl加密文件.

I need to decrypt openssl encrypted file in Java by passing key.

openssl enc -d -aes-256-cbc -in myfile.csv.enc -out myoutputfile.csv -pass key.bin

openssl enc -d -aes-256-cbc -in myfile.csv.enc -out myoutputfile.csv -pass key.bin

此处提供的是密码文件,而不是密钥.根据密码和随机盐计算出密钥和IV.

Here you provide a password file, not a key. Key and IV are computed from the password and random salt.

与众不同的是,我的文件是整体加密的,而不是行加密的,并且我有一个显式密钥来解密文件.

As differently, my files encrypted as a whole rather than line encryption and I have an explicit key to decrypt the file.

        Cipher cipher = Cipher.getInstance("AES/CBC/Pkcs5Padding");
        byte[] passwordBytes = readPasswordBytes();
        InputStream in = new BufferedInputStream(new FileInputStream("notes.enc"));
        byte[] saltedString = new byte[8];
        in.read(saltedString); // read the "Salted__" prefix
        byte[] salt         = new byte[8];
        in.read(salt);
        // see the EVP_BytesToKey and parameters from the linked question
        byte[][] keyAndIv = EVP_BytesToKey(
                KEY_SIZE_BITS / Byte.SIZE,
                cipher.getBlockSize(),
                md5,
                salt,
                passwordBytes,
                ITERATIONS);
        byte[] key = keyAndIv[0];
        byte[] iv  = keyAndIv[1];

        SecretKeySpec secKeySpec = new SecretKeySpec(key, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, secKeySpec, ivSpec);

        // here we will use 4kB buffer to read and decrypt data
        byte[] buffer = new byte[4096];

        OutputStream out = new BufferedOutputStream(new FileOutputStream("notes2.txt"));
        for(int readBytes = in.read(buffer); readBytes>-1; readBytes = in.read(buffer)) {
            byte[] decrypted = cipher.update(buffer, 0, readBytes);
            out.write(decrypted);
        }
        byte[] decrypted = cipher.doFinal();
        out.write(decrypted);
        out.flush();
        out.close();
        in.close();

这篇关于我如何在Java中解密openssl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆