java写入文件结果中文字母

时间:2014-12-10 00:49:37

标签: java encoding character-encoding utf file-writing

我正在解密常规文本文件并遇到我无法解决的问题, 我的代码是这样的, 我收到一个带字母,数字和符号+和/(这是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();
        }
    }

0 个答案:

没有答案