2014-03-26 45 views
-1

程序正在运行。它运行,但然后崩溃。在我的文件夹中,它创建一个加密文件,但它是空白的。它也根本不会生成解密文件。我还将位大小更改为128.为了正确实现AES,我还缺少了什么?来自文件故障的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(); 
    } 

} 

回答

0

作为错误消息指出,存在该类型的InputStream没有方法的getBytes()。 您需要读取密钥并将其存储为字符串。

例如。 而不是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,你将需要一个位密钥(或192/256位)

+0

谢谢你的帮助!当我运行程序时,程序崩溃了,但没有收到错误信息。请看我上面的新代码!谢谢 – user3294617

+0

您还在另一个问题中发布了这个问题,并表示它的工作原理?那么这是否解决了你的问题?如果是这样的话,请勾选它 –