gpt4 book ai didi

java - 文本文件的字节加密

转载 作者:行者123 更新时间:2023-12-01 09:12:14 26 4
gpt4 key购买 nike

我有一个应用程序,我必须在其中读取 .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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com