java - 从加密文件读取到声明的字符串变量

标签 java android encryption fileinputstream

我有一个加密文件,它是使用 this question 中的引用完成的.我加密了文件。现在我的问题是在尝试读取内容时,从返回的 read() 中得到一个空字符串。下面是我的调用方法和解密密文为字符串变量的方法。

调用方式:

File encryptedCFG = new File(homeDir + "/" + folder_name + "/twCGF.txt");
dc.ReadEncryptedFile(encryptedCFG); 

方法:

public void ReadEncryptedFile(File deInFile) {
        try {
            FileInputStream fis = new FileInputStream(deInFile);
            int length = (int) deInFile.length();
            byte[] filebyte = new byte[length]
            // Decrypt the byte contents from the file using the cipher setup
            byte[] tmpTxT = mDecipher.doFinal(filebyte);
            fis.read(tmpTxT);
            fis.close();

         // Read into a string since we got the contents
         String plaintxt = new String(tmpTxt, "UTF-8");

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

为什么没有正确获取加密文件的内容?

最佳答案

在您解密字节数组的那一行,它仍然是空的。你还没有读入文件。你必须切换操作。

byte[] filebyte = new byte[length]
fis.read(filebyte);
byte[] tmpTxt = mDecipher.doFinal(filebyte);
fis.close();

String plaintxt = new String(tmpTxt, "UTF-8");

关于java - 从加密文件读取到声明的字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413468/

相关文章:

visual-studio-2010 - 使用 Visual Studio 2010 ClickOnce 发布加密文件

Java 双重比较

java - Android Java 多数组初始化

java - editText.getText().toString() 检索字符串全部大写

Android:PullToRefresh ListView 未显示

python - 在运行时保护 Python 程序中的数据

java - 在gradle中生成 key

java - 并非所有异常都映射到 SimpleMappingExceptionResolver 中的 resolveException

java - 使用参数将数据从 Activity 发送到 Fragment (Kotlin/Android)

java - 在 Java 中使用 BigIntegers 进行 BitShifting