Java 需要帮助实现CipherInputStream和CipherOutputStream吗

Java 需要帮助实现CipherInputStream和CipherOutputStream吗,java,encryption,cryptography,Java,Encryption,Cryptography,我在为我的3DES加密算法将FileInputStream和FileOutputStream转换为CipherInputStream和CipherOutputStream时遇到一些问题。有人能告诉我怎么做吗?我最终得到了错误:“CipherInputStream无法解析为类型”。对不起,如果这是显而易见的 // author Alexander Matheakis public static void main(String[] args) throws Exception { //

我在为我的3DES加密算法将FileInputStream和FileOutputStream转换为CipherInputStream和CipherOutputStream时遇到一些问题。有人能告诉我怎么做吗?我最终得到了错误:“CipherInputStream无法解析为类型”。对不起,如果这是显而易见的

// author Alexander Matheakis
public static void main(String[] args) throws Exception {


    // file to be encrypted
    FileInputStream inputFile = new FileInputStream("plainfile.txt");

    // encrypted file
    FileOutputStream outputFile = new FileOutputStream("C:\\Users\\islan\\OneDrive\\Documents\\Encryptor\\plainfile.des");

    // password to encrypt the file
    String passKey = "tkfhkggovubm";


    byte[] salt = new byte[8];
    SecureRandom r = new SecureRandom();    
    r.nextBytes(salt);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
    SecretKeyFactory secretKeyFactory = SecretKeyFactory
            .getInstance("PBEWithSHA1AndDESede");
    SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);


    PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
    Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede"); 
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
    outputFile.write(salt);

    byte[] input = new byte[64];
    int bytesRead;
    while ((bytesRead = inputFile.read(input)) != -1) {
        byte[] output = cipher.update(input, 0, bytesRead);
        if (output != null)
            outputFile.write(output);
    }

    byte[] output = cipher.doFinal();
    if (output != null)
        outputFile.write(output);

    inputFile.close();
    outputFile.flush();
    outputFile.close();

// author Alexander Matheakis
这对我很有用:

public class Cypher {

    // password to encrypt the file
    private static final String passKey = "tkfhkggovubm";

    public static void main(String[] args) throws Exception {

        try (FileInputStream inputFile = new FileInputStream("plainfile.txt");
                FileOutputStream outputFile = new FileOutputStream("plainfile.des");) {

            byte[] salt = new byte[8];
            SecureRandom r = new SecureRandom();
            r.nextBytes(salt);

            outputFile.write(salt);

            PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
            SecretKeyFactory secretKeyFactory = SecretKeyFactory
                    .getInstance("PBEWithSHA1AndDESede");
            SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);

            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
            Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);

            try (CipherOutputStream cis = new CipherOutputStream(outputFile, cipher);) {
                IOUtils.copy(inputFile, cis);
            }
        }

        try (FileInputStream fis = new FileInputStream("plainfile.des");
                FileOutputStream outputFile = new FileOutputStream("plainfile.txt2");) {
            byte[] salt = new byte[8];
            int saltBytes = fis.read(salt);
            if (saltBytes!=salt.length)
                throw new Exception("Huh???");

            PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
            SecretKeyFactory secretKeyFactory = SecretKeyFactory
                    .getInstance("PBEWithSHA1AndDESede");
            SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);

            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
            Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);

            try (CipherInputStream cis = new CipherInputStream(fis, cipher);) {
                IOUtils.copy(cis, outputFile);
            }
        }
    }
}
执行main之后,
plainfile.txt
plainfile.txt2
相等。
plainfile.des
已加密

IOUtils.copy
是中的一种方法,它在内部保留一个缓冲区,并将所有字节从一个流写入另一个流。 它只是将
字节[]输入=…
提取到方法中,再加上使用。
这使代码简单易读。

您没有提供用于读取/CipherInputStream的代码。您在创建CipherInputStream之前读过盐吗?谢谢@SirFartALot。我可以问你为什么要去掉
byte[]input=newbyte[64];int字节读取;而((bytesRead=inputFile.read(input))!=-1)
用于加密方法?
是否尝试(CipherOutputStream cis=new-CipherOutputStream(outputFile,cipher);{IOUtils.copy(inputFile,cis);
执行相同的操作?@adot710:请参阅答案。