无法解密加密文件?

时间:2013-11-04 02:08:35

标签: java android cryptography

我尝试通过这种方式加密我的文件:

加密

static void encrypt(String strInput , String strOutput) throws IOException,
    NoSuchAlgorithmException,NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(strInput);
    FileOutputStream fos = new FileOutputStream(strOutput);

    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
            "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

并通过以下方式解密:

解密:

static String decrypt(String strInput) throws IOException, NoSuchAlgorithmException,
    NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(strInput);

    int endFile = strInput.length() - 4;
    String strOut = strInput.substring(0, endFile) + "xx.jpg"; 

    FileOutputStream fos = new FileOutputStream(strOut);

    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(),
              "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];

    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
    return strOut;
}

但是,结果文件的大小为0 kb,当我尝试在解密时对b = cis.read(d)进行故障排除时,总是返回-1,同时cis.available()总是返回0.有人可以告诉我我的代码的哪一部分是错的?

注意:我可以确保要解密的文件始终存在。

1 个答案:

答案 0 :(得分:1)

我认为此问题是因为您尝试解密未加密(或未正确加密)的数据。

decrypt()方法中,CipherOutputStream隐藏了Cipher类可能抛出的所有异常。见javadoc for CipherOutputStream

  

此外,此类捕获其祖先类未抛出的所有异常。

要公开此问题,您可能需要手动实施密码使用。这是一个简单的例子:

static String decrypt(String strInput) throws IOException,
    NoSuchAlgorithmException, NoSuchPaddingException,
    InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    FileInputStream fis = new FileInputStream(strInput);

    int endFile = strInput.length() - 4;
    String strOut = strInput.substring(0, endFile) + "xx.txt"; 

    FileOutputStream fos = new FileOutputStream(strOut);

    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);

    int b;
    byte[] d = new byte[8];
    while ((b = fis.read(d)) != -1) {
        fos.write(cipher.update(d));
    }
    fos.write(cipher.doFinal());

    fos.flush();
    fos.close();
    fis.close();
    return strOut;
}

您在问题中发布的算法似乎适用于有效输入。例如,让我们假设以下主要内容:

public static void main(String[] argv)  {
    try {
        encrypt("test.txt", "XXX.txt");
        decrypt("XXX.txt");
    }
    catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

使用它,并使用文本文件和JPG文件进行测试,您的算法执行完美。但是,当使用无效输入到解密算法时,您描述的问题就开始出现了。

对于测试,让我们想象一下,我们在尝试解密明确的文件时出现“错误”(只需更改decrypt()中传递给main的参数:

encrypt("test.txt", "XXX.txt");
decrypt("test.txt");

然后当然对decrypt()方法的输入填充是错误的,我们应该得到一个例外。

然而,使用您的decrypt()版本,没有例外。我们得到的只是一个空文件。

使用上面显示的decrypt()方法的修改版本,我们得到以下异常:

javax.crypto.BadPaddingException: Given final block not properly padded
javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
    at javax.crypto.Cipher.doFinal(Cipher.java:1970)
    at MainTest.decrypt(MainTest.java:71)
    at MainTest.main(MainTest.java:21)
相关问题