简体   繁体   English

如何使用密钥对字符串进行异或运算并存储在文件中以供以后解密和使用?

[英]How to Xor a String with a key and store in a file to decrypt later and use it?

I have researched a lot on this question and finally understood this, that encrypting and decryption using just XOR is not a suggested way of encryption but as it is an academic question I have to do it. 我对此问题进行了很多研究,终于明白了,仅使用XOR进行加密和解密并不是建议的加密方式,但由于这是我必须做的学术问题。

In my scenario I have to save java objects(space delimited strings with xor encryption by a key of our choice) in a file(.txt file) line by line and retrieve them later on if needed. 在我的场景中,我必须逐行将java对象(用我们选择的键通过xor加密用空格分隔的字符串)逐行保存在文件(.txt文件)中,并在以后需要时检索它们。 So saving a xor string in a file is not a problem but while decrpytion its getting truncated. 因此,将xor字符串保存在文件中不是问题,但解密时会被截断。 Below is a sample similar code which I have implemented for my project. 以下是我为我的项目实现的示例类似代码。

public class TestingXor {
    private static String XorString(String input){
        String key = "computing";
        Charset charSet = Charset.forName("UTF-8");
        byte[] inputBytes = input.getBytes(charSet);
        byte[] keyBytes = key.getBytes(charSet);
        for(int i=0;i<inputBytes.length;i++){
            inputBytes[i] = (byte)(inputBytes[i] ^ keyBytes[i%keyBytes.length]);
        }
        return (new String(inputBytes,charSet));
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            File testFile = new     File("addressBook.txt");
            Scanner scanObj = new Scanner(testFile);
            String testString = "asd asdasd 07/08/2015 asdasdasd 198546125";
            BufferedWriter writer = new BufferedWriter(new FileWriter(testFile));
            writer.write(XorString(testString));
            writer.close();
            System.out.println(XorString(scanObj.nextLine()));          
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

} }

The above code returns "asd as" where as I need the whole string .If I am not saving the file I am able to encrypt and decrypt properly, but If I am saving the string to a file and retrieving the string from the file and decrypting it does'nt work. 上面的代码返回“ asd as”,这里我需要整个字符串。如果我不保存文件,则能够正确加密和解​​密,但是如果我将字符串保存到文件中并从文件中检索字符串,解密是行不通的。 I think it is related to saving of the string(\\0 getting saved in the middle of the line) but what is the work around this. 我认为这与字符串的保存有关(\\ 0在行的中间被保存),但是解决此问题的方法是什么。 I dont know the length of the string when I am reading from a file. 从文件读取时,我不知道字符串的长度。 If this question is already answered you can redirect me to that question. 如果这个问题已经回答,您可以将我重定向到该问题。

The problem is that you XorString method can return String that contain newlines or other other line separator characters (basically \\r\\n\
\
\… and the end of the file. Bad luck for you, your test generated multiple '\\r' which makes the Scanner stop looking for the rest of the string. 问题是您的XorString方法可以返回包含换行符或其他行分隔符(基本上是\\r\\n\
\
\…以及文件末尾)的\\r\\n\
\
\… 。运气不好,您的测试生成了多个'\\ r ',这会使Scanner停止寻找字符串的其余部分。

One way to get away from this is to use some kind of escaping mechanisms. 摆脱这种情况的一种方法是使用某种转义机制。 For example, Properties provide such a mechanism, but feel free to implement your own. 例如, Properties提供了这样一种机制,但是可以随意实现自己的机制。

Small example: 小例子:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Properties;

public class TestingXor {
    private static String xorString(String input) {
        String key = "computing";
        Charset charSet = Charset.forName("UTF-8");
        byte[] inputBytes = input.getBytes(charSet);
        byte[] keyBytes = key.getBytes(charSet);
        for (int i = 0; i < inputBytes.length; i++) {
            inputBytes[i] = (byte) (inputBytes[i] ^ keyBytes[i % keyBytes.length]);
        }
        return new String(inputBytes, charSet);
    }

    public static void main(String[] args) throws IOException {
        File testFile = new File("addressBook.txt");
        store(testFile);
        load(testFile);
    }

    private static void load(File file) throws IOException, FileNotFoundException {
        Properties props = new Properties();
        try (InputStream in = new BufferedInputStream(new FileInputStream(file));) {
            props.load(in);
        }
        for (int i = 1; i < props.size() + 1; i++) {
            System.out.println(xorString(props.getProperty("entry-" + i)));
        }
    }

    private static void store(File file) throws IOException, FileNotFoundException {
        Properties props = new Properties();
        String testString = "asd asdasd 07/08/2015 asdasdasd 198546125";
        props.put("entry-1", xorString(testString));
        try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file));) {
            props.store(out, "Some comment");
        }
    }

}

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

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