java - 文本文件的字节加密

标签 java file formatting

我有一个应用程序,我必须在其中读取 .txt 文件,以便我可以存储一些值并保留它们。这工作得很好,除了我想让这些值对于外部用户来说不可读或“不可理解”。

我的想法是将文件内容转换为十六进制或二进制,并在读取过程中将其改回字符。问题是,由于我的编译器,我无法访问 String.Format 等方法。

以下是我目前读取和保存这些值的方式:

                byte[] buffer = new byte[1024];
                int len = myFile.read(buffer);
                String data = null;
                int i=0;
                data = new String(buffer,0,len);

打开和操作文件的类:

public class File {
    private boolean debug = false;
    private FileConnection fc = null;
    private OutputStream os = null;
    private InputStream is = null;
    private String fileName = "example.txt";
    private String pathName = "logs/";
    final String rootName = "file:///a:/";

    public File(String fileName, String pathName) {
        super();
        this.fileName = fileName;
        this.pathName = pathName;
        if (!pathName.endsWith("/")) {
            this.pathName += "/"; // add a slash
        }
    }


    public boolean isDebug() {
        return debug;
    }

    public void setDebug(boolean debug) {
        this.debug = debug;
    }


    public void write(String text) throws IOException {
        write(text.getBytes());
    }


    public void write(byte[] bytes) throws IOException {
        if (debug)
            System.out.println(new String(bytes));
        os.write(bytes);
    }


    private FileConnection getFileConnection() throws IOException {
        // check if subfolder exists
        fc = (FileConnection) Connector.open(rootName + pathName);
        if (!fc.exists() || !fc.isDirectory()) {
            fc.mkdir();
            if (debug)
                System.out.println("Dir created");
        }
        // open file
        fc = (FileConnection) Connector.open(rootName + pathName + fileName);
        if (!fc.exists())
            fc.create();
        return fc;
    }

    /**
     * release resources
     */
    public void close() {
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        is = null;
        if (os != null)
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        os = null;
        if (fc != null)
            try {
                fc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        fc = null;

    }

    public void open(boolean writeAppend) throws IOException {
        fc = getFileConnection();
        if (!writeAppend)
            fc.truncate(0);
        is = fc.openInputStream();
        os = fc.openOutputStream(fc.fileSize());

    }

    public int read(byte[] buffer) throws IOException {

        return is.read(buffer);
    }

    public void delete() throws IOException {
        close();
        fc = (FileConnection) Connector.open(rootName + pathName + fileName);
        if (fc.exists())
                fc.delete();


    }

}

我想知道如何阅读此内容的简单方法。二进制或十六进制,两者都适合我。

最佳答案

那么,在对这个问题有一定了解的情况下,我相信您真的在寻找一种混淆形式?正如评论中提到的,最简单的方法可能是密码形式。

考虑这个移位密码的示例实现:

常见

int shift = 11;

写作

// Get the data to be wrote to file.
String data = ...

// cipher the data.
char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; ++i) {
    chars[i] = (char)(chars[i] + shift);
}
String cipher = new String(chars);

// Write the data to the cipher file.
...

阅读

// Read the cipher file.
String data = ...

// Decipher the data.
char[] chars = data.toCharArray();
for (int i = 0; i < chars.length; ++i) {
    chars[i] = (char)(chars[i] - shift);
}
String decipher = new String(chars);

// Use data as required.
...

这是一个example implementation on Ideone 。输出:

Data    : I can read this IP 192.168.0.1
Cipher  : T+nly+}plo+st~+T[+<D=9<AC9;9<
Decipher: I can read this IP 192.168.0.1

为了满足 Java 3 的要求,我尝试将其保持在尽可能低的级别。

请注意,无论如何这都安全。移位密码(就像气泡中的大多数密码一样)很容易被恶意实体破解。如果确实担心安全问题,请不要使用此功能。

关于java - 文本文件的字节加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40847363/

相关文章:

c - 从文件中读取位并将其存储到 C 中的 char 数组中

html - 如何在输入类型文件中选择的 Node js中获取文件路径

java - 一种指定驱动任意对象解析和格式化的模式字符串的方法?

html - 如何以网格或 block 格式显示文本和图像?

JAVA 将大文件拆分为多部分 ZIP。如何?

java - 需要帮助在 JAVA 中反向打印单词

java - 如何使用 pom.xml 在单个类中执行测试?

java - .setBounds() 不适用于嵌套 JLabels

java - 检测空白字符串

android - 阻止 Android Studio 包装方法和构造函数