我有一个加密文件,它是使用 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/