使用CipherInputStream和CipherOutputStream加密和解密文件 [英] Encrypting and decrypting a file using CipherInputStream and CipherOutputStream

查看:207
本文介绍了使用CipherInputStream和CipherOutputStream加密和解密文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在AES中编写一个加密文件,并随后使用JCA中提供的密码流对其解密.但是,在读取文件时遇到了问题,因为解密越来越麻烦了.

I've been trying to write an encrypted file in AES and decrypt it subsequently by using Cipher Streams provided in JCA. However, I'm having problems while reading the file, as the decryption is going haywire.

public class CipherStreams {
public static void main(String[] args) {
    try {
        KeyGenerator keygen = KeyGenerator.getInstance("AES");
        Key k = keygen.generateKey();

        Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
        aes.init(Cipher.ENCRYPT_MODE, k);
        FileOutputStream fs = new FileOutputStream("Encrypyed.txt");
        CipherOutputStream out = new CipherOutputStream(fs, aes);
        out.write("[Hello:Okay]\nOkay".getBytes());
        out.close();

        Cipher aes2 = Cipher.getInstance("AES/ECB/PKCS5Padding");
        aes2.init(Cipher.DECRYPT_MODE, k);

        FileInputStream fis = new FileInputStream("Encrypyed.txt");
        CipherInputStream in = new CipherInputStream(fis,aes2);
        byte[] b = new byte[8];
        int i = in.read(b);
        while(i!=-1) {
            System.out.print((char)i);
            i = in.read(b);
        }
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException ex) {
        Logger.getLogger(CipherStreams.class.getName()).log(Level.SEVERE, null, ex);
    }
}
}

我收到的单字节输出为5.有人可以帮助指出问题吗?

I'm receiving a single byte output as 5. Can anyone please help point out the problem?

推荐答案

您不是要写入读取的字节,而是要写入要读取的字节数.

You're not writing the bytes read, you're writing the number of bytes being read.

您还假设默认的平台编码只是将每个字符转换为一个字节.

You're also assuming that the default platform encoding just transforms each character to a byte.

只需执行与编写时相反的操作:读取所有内容,并将读取的字节数组转换为String,然后打印该字符串:

Just do the reverse of what you did when writing: read everything, and transform the read byte array to a String, then print that string:

public class CipherStreams {
    public static void main(String[] args) {
        try {
            KeyGenerator keygen = KeyGenerator.getInstance("AES");
            Key k = keygen.generateKey();

            Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
            aes.init(Cipher.ENCRYPT_MODE, k);
            String fileName = "Encrypted.txt";
            FileOutputStream fs = new FileOutputStream(fileName);
            CipherOutputStream out = new CipherOutputStream(fs, aes);
            out.write("[Hello:Okay]\nOkay".getBytes());
            out.flush();
            out.close();

            Cipher aes2 = Cipher.getInstance("AES/ECB/PKCS5Padding");
            aes2.init(Cipher.DECRYPT_MODE, k);

            FileInputStream fis = new FileInputStream(fileName);
            CipherInputStream in = new CipherInputStream(fis, aes2);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            byte[] b = new byte[1024];
            int numberOfBytedRead;
            while ((numberOfBytedRead = in.read(b)) >= 0) {
                baos.write(b, 0, numberOfBytedRead);
            }
            System.out.println(new String(baos.toByteArray()));
        }
        catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException ex) {
            ex.printStackTrace();
            ;
        }
    }
}

这篇关于使用CipherInputStream和CipherOutputStream加密和解密文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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