简体   繁体   English

Java CipherInputStream将奇怪的字符放在行首

[英]Java CipherInputStream puts strange character at beginning of line

I'm testing the encryption of text in Java. 我正在测试Java中的文本加密。 The problem is that I get some strange characters at the beginning of the line and I don't understand why. 问题是我在行首出现一些奇怪的字符,但我不明白为什么。 When I remove encryption everything goes smoothly. 当我删除加密时,一切都会顺利进行。

When copied into Notepad++, the output looks like: 复制到Notepad ++后,输出如下所示:

Hello

<SOH>dear

<STX><STX>world

Why am I getting the strange control characters? 为什么会出现奇怪的控制字符?

Code: 码:

public class Test {
        private static File file;
        private static final byte[] STAT_KEY = { -1, -2, 3, 4, -5, -6, -7, 8 };
        static {
            file = new File("MyFile.txt");
        }

        private static Cipher getCipher(int mode) throws InvalidKeyException, NoSuchAlgorithmException,
                InvalidKeySpecException, NoSuchPaddingException {
            DESKeySpec dks = new DESKeySpec(STAT_KEY);
            SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = skf.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(mode, desKey);
            return cipher;
        }

        private static void appendToFile(String item) throws Exception {
            CipherOutputStream cos = null;
            try {
                cos = new CipherOutputStream(new FileOutputStream(file, true), getCipher(Cipher.ENCRYPT_MODE));
                cos.write((item + String.format("%n")).getBytes());
            } finally {
                cos.close();
            }
        }

        private static void readFromFile() throws Exception {
            CipherInputStream cis = null;
            try {
                cis = new CipherInputStream(new FileInputStream(file), getCipher(Cipher.DECRYPT_MODE));
                int content;
                while ((content = cis.read()) != -1) {
                    System.out.print((char) content);
                }
            } finally {
                cis.close();
            }
        }

        public static void main(String[] args) throws Exception {
            String[] items = { "Hello", "dear", "world" };
            for (String item : items) {
                appendToFile(item);
            }
            readFromFile();
        }
    }

PD: Excuse the way I deal with exceptions :) PD:对不起,我处理异常的方式:)

Much like an ObjectOutputStream , the CipherOutputStream is not written in a way that allows direct appending. 类似于ObjectOutputStreamCipherOutputStream的编写方式不允许直接附加。

append(data1) + append(data2) + append(data3) != append(data1+data2+data3) append(data1)+ append(data2)+ append(data3)!= append(数据1+数据2+数据3)

You need to add your own method of delimiting different blocks of data. 您需要添加自己的定界不同数据块的方法。 The funny ? 有趣? characters are the private control characters used by the CipherOutputStream . 字符是CipherOutputStream使用的私有控制字符。

Life may be easier if you just encrypt the data normally (ie using a Cipher object) and write the output to your file surrounded by suitable delimiters. 如果您只是正常地加密数据(即使用Cipher对象)并将输出写入由合适的定界符包围的文件中,则生活可能会更轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM