文本文件的字节加密

吕坤

我有一个必须读取.txt文件的应用程序,以便可以存储一些值并保留它们。除我要使外部用户不可读或无法理解的值外,这工作得很好。

我的想法是将文件内容转换为十六进制或二进制,然后在读取过程中将其更改回Char。事实是,由于我的编译器,我无法访问诸如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.
...

这是在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的要求,我尝试将其保持在尽可能低的水平。

请注意,这是以任何方式固定。移位密码(就像气泡中的大多数密码一样)很容易被恶意实体破坏。如果安全是一个真正的问题,请不要使用它。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

加密文本文件的简便方法

来自分类Dev

文本文件的加密和解密

来自分类Dev

纯文本文件或加密文件(C编程)

来自分类Dev

加密和解密文本文件的内容(Java)

来自分类Dev

如何使用按位XOR加密文本文件?

来自分类Dev

C ++中的简单文本文件加密

来自分类Dev

在C#中读取加密的文本文件

来自分类Dev

如何使用旋转加密文本文件?

来自分类Dev

Spark:文本文件到 RDD[字节]

来自分类Dev

将图像的字节写入文本文件

来自分类Dev

使用Python加密文本文件,最好使用模块加密

来自分类Dev

给定文本文件的前N个字节,如何确定这些字节是否为XML?

来自分类Dev

打印到文本文件-加密的文本显示为问号

来自分类Dev

从文本文件python写入文本文件

来自分类Dev

附加文本文件

来自分类Dev

从文本文件读取

来自分类Dev

文本文件的行数

来自分类Dev

解析文本文件

来自分类Dev

ASCII文本文件

来自分类Dev

从文本文件读取

来自分类Dev

读写文本文件

来自分类Dev

处理文本文件

来自分类Dev

修改文本文件

来自分类Dev

文本文件排序

来自分类Dev

文本文件的格式?

来自分类Dev

用Java加密文本文件的最简单方法

来自分类Dev

C ++加密文本文件,允许通过ifstream使用解密

来自分类Dev

使用AES加密/解密并将信息存储在文本文件中

来自分类Dev

使用AES加密/解密并将信息存储在文本文件中