来自文件故障的AES密钥

时间:2014-03-26 00:10:57

标签: java encryption aes des

程序正在运行。它运行但随后崩溃。在我的文件夹中,它创建一个加密文件,但它是空白的。它根本不会产生解密文件。我还将位大小更改为128.为了正确实现AES,我还缺少什么?

这是修改过的程序

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;

import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.InvalidAlgorithmParameterException;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;





import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.DESKeySpec;

public class MyCiphers {

    public static void main(String[] args) {
        try {



            BufferedReader br = new BufferedReader(new FileReader("key.txt"));
            String key = br.readLine();
            br.close();
            FileInputStream fis = new FileInputStream("original.txt");
            FileOutputStream fos = new FileOutputStream("encrypted.txt");
            encrypt(key, fis, fos);

            FileInputStream fis2 = new FileInputStream("encrypted.txt");
            FileOutputStream fos2 = new FileOutputStream("decrypted.txt");
            decrypt(key, fis2, fos2);

        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
        encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
    }

    public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
        encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
    }

    public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {


      SecretKeySpec dks = new SecretKeySpec(key.getBytes(),"AES");
        Cipher cipher = Cipher.getInstance("AES"); 

        if (mode == Cipher.ENCRYPT_MODE) {
            cipher.init(Cipher.ENCRYPT_MODE, dks);
            CipherInputStream cis = new CipherInputStream(is, cipher);
            doCopy(cis, os);        
        } else if (mode == Cipher.DECRYPT_MODE) {
            cipher.init(Cipher.DECRYPT_MODE, dks);
            CipherOutputStream cos = new CipherOutputStream(os, cipher);
            doCopy(is, cos);
        }


    }

    public static void doCopy(InputStream is, OutputStream os) throws IOException {
        byte[] bytes = new byte[128];
        int numBytes;
        while ((numBytes = is.read(bytes)) != -1) {
            os.write(bytes, 0, numBytes);
        }
        os.flush();
        os.close();
        is.close();
    }

}

1 个答案:

答案 0 :(得分:0)

正如错误消息所示,InputStream类型没有getBytes()方法。 您需要读取密钥并将其存储为String。

例如。 而不是FileInputStream fisk ...

BufferedReader br = new BufferedReader(new FileReader("Key.txt"));
String key = br.readLine();
br.close();

(假设你的密钥文件只包含一行的密钥)

然后更改您的加密/解密和encryptOrDecrypt方法,将密钥作为String而不是InputStream接收 例如。 encrypt(String key, InputStream is, OutputStream os)

然后你可以拨打SecretKeySpec(key.getBytes(),"DES");

你也意识到这是DES,而不是你说的AES吗?如果您想使用AES,则需要 128 位密钥(或192/256位)