在JCE中解密文本文件的问题

时间:2015-02-11 13:38:22

标签: java encryption java-6 jce

我必须使用JCE(Java SE 1.6)对文本文件进行加密。为此我写了一个方法aes256CBCEncrypt,它返回CipherOutputstream,我在文件中写了加密测试'。现在,当我尝试使用方法aes256CBCDecrypt对此文件进行解密(名为' encryptedtest')时,它会返回我CipherInputStream我正在写的' decryptedtest&# 39;验证其内容。令人惊讶的是,这个文件是空的。

有人可以帮我解决我的代码出了什么问题。

代码段:

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


    class MyTest{
     public static OutputStream aes256CBCEncrypt(OutputStream os, String passPhrase) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, InvalidKeyException, InvalidAlgorithmParameterException
    {

        //  MessageDigest md = MessageDigest.getInstance("SHA-256");
        //  md.update(passPhrase.getBytes());
        //  byte[] key = md.digest();

            Cipher aesCipher = Cipher.getInstance("AES/CBC/ISO10126Padding");

            SecureRandom secureRandom = new SecureRandom();
            secureRandom.setSeed(System.currentTimeMillis());
            byte[] bb = new byte[16];
            secureRandom.nextBytes(bb);
            os.write(bb);

            aesCipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(passPhrase.getBytes(), "AES"), new IvParameterSpec(
                    bb));
            return new CipherOutputStream(os, aesCipher);


    }

    public static InputStream aes256CBCDecrypt(File f, String passPhrase)
            throws FileNotFoundException
    {
        FileInputStream fis = null;
        try
        {
            //MessageDigest md = MessageDigest.getInstance("SHA-256");
        //  md.update(passPhrase.getBytes());
        //  byte[] key = md.digest();

            Cipher aesCipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
            fis = new FileInputStream(f);
            byte[] bb = new byte[16];
            fis.read(bb);
            aesCipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(passPhrase.getBytes(), "AES"), new IvParameterSpec(
                    bb));
            return new CipherInputStream(fis, aesCipher);
        }
        catch (final Exception e)
        {


        }
        return null;
    }

    public static void main(String args[]) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IOException{
    String keyFile = "C:\\contentProducer" + File.separator + "test";
        String encryptedFile = "C:\\contentProducer" + File.separator + "encryptedtest";
        String decryptedFile = "C:\\contentProducer" + File.separator + "decryptedtest";

        FileInputStream in = new FileInputStream(keyFile);
        FileOutputStream bos = new FileOutputStream(new File(encryptedFile));

    //Call method for Encryption
        OutputStream encryptedBos = aes256CBCEncrypt(bos,"0123456789abcdef");
        int inByte;
        while ((inByte = in.read()) != -1 ) {
            encryptedBos.write(inByte);
        }
        in.close();
        bos.close();
        encryptedBos.close();

    //Call Method for Decryption

        InputStream inputStream = aes256CBCDecrypt(new File(encryptedFile), "0123456789abcdef");

        FileOutputStream deos = new FileOutputStream(new File(decryptedFile));
        while ((inByte = inputStream.read()) != -1 ) {
            deos.write(inByte);
        }
        inputStream.close();
        deos.close();

        }

}

1 个答案:

答案 0 :(得分:0)

在关闭FileOutputStream之前,您正在关闭CipherOutputStream。这可以防止后者完成其工作并将加密数据写入磁盘。

bos.close();
encryptedBos.close();

应改为:

encryptedBos.close();
bos.close();