我正在解密一个常规文本文件并遇到一个我无法解决的问题,我的代码是这样工作的,我收到一个带有字母、数字和符号 + 和 /(即 Radix64)的 txt 文件,然后我从文件中读取 12 个字符,将它们转换为字符串,然后使用 base64.decoder 将文本解码为 8 字节数组。在字节数组和我构建的字符串上使用 system.out.print 显示我的读数是正确的,并且我从文件中得到了我需要的东西。现在我将字节向右或向左移动几次,并希望将字节数组写入一个新文件,该文件是解密的消息。
我正在使用 FileOutputStream 写入一个新文件,现在问题来了,我打开文件及其全中文字母。
我不知道这怎么可能,因为我唯一要做的就是将字节写入文件。我试图查找有关此主题的信息但没有成功,只有相关主题与 python/ruby 相关,并指出它是 UTF-8/UTF-16 问题,但没有解决方法的答案。
任何帮助将不胜感激,如果发布我的部分代码会有所帮助,请让我知道究竟需要什么。
private static void Decryption() {
InputStream byteReader = null;
FileOutputStream fop = null;
int byteByByte = 0;
char[] radixToByte = new char[12];
byte[] block = new byte[8];
byte[] decryptedBlock = new byte[8];
long key = 0;
//the file which will contain the plain text will be p.txt in the same directory c.txt is
try {
fop = new FileOutputStream(new File(fileName.substring(0, fileName.length()-5)+"p.txt"));
} catch (FileNotFoundException e) {
System.out.println("File was not found");
e.printStackTrace();
}
//open key file and read the key.
try {
byteReader = new FileInputStream(keyFileName);
} catch (FileNotFoundException e) {
System.out.println("File was not found");
System.exit(0);
}
//reading 56 bits in radix format and building the key accordingly
char[] keyByteArray = new char[12];
for(int k = 0; k < 12; k++){
try {
byteByByte = byteReader.read();
} catch (IOException e) {
e.printStackTrace();
}
keyByteArray[k] = (char) byteByByte;
}
Base64.Decoder decoder = Base64.getDecoder();
byte[] decodedByteArray = decoder.decode(new String(keyByteArray));
for(int k = 0; k < 7; k++){
key <<= 8;
key |= (decodedByteArray[k] & 0x00000000000000FF);
}
//open cipher text to decrypt.
try {
byteReader = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
System.out.println("File was not found");
System.exit(0);
}
//while haven't reached end of cipher text message keep reading byte by byte and decrypting
//them block by block (each block 64 bits).
while(isFileReadingFinished == false){
for(int i = 0; i < 12; i++){
try {
byteByByte = byteReader.read();
} catch (IOException e) {
e.printStackTrace();
}
if(byteByByte == -1){
isFileReadingFinished = true;
break;
}
radixToByte[i] = (char)byteByByte;
}
//this is the block after radix but still decrypted so we will save it for CBC
block = decoder.decode(new String(radixToByte));
//encrypt via feistel
decryptedBlock = feistel(block, key);
//after decryption we still need to xor with previous encrypted data or IV.
for(int i = 0; i < 8; i++){
decryptedBlock[i] = (byte) (decryptedBlock[i] ^ initializationVector[i]);
}
//next decrypted text will need to be xored with the current encrypted text after decryption.
for(int i = 0; i < 8; i++){
initializationVector[i] = block[i];
}
try {
fop.write(decryptedBlock);
fop.flush();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
fop.close();
} catch (IOException e) {
e.printStackTrace();
}
}