JCE中的文本文件解密问题



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

有人能帮我找出我的代码出了什么问题吗。

代码段:

 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();
        }
}

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

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

应更改为:

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

最新更新