2

我想我正在寻找某种基本的文件加密,但不知道从哪里开始。

我正在找人告诉我从哪里开始寻找,或者更好的是,提供一些代码。

我编写了一个游戏,目前将数据保存到通用文本文件中。当然,任何愿意这样做的人都可以改变这一点。

我需要的是创建一个可以存储整数和字符串的文件,即使不是不可能在游戏之外进行编辑也很难。

在我的搜索中,我遇到了 .dat 文件,但它们似乎比我正在寻找的更复杂。

感谢所有帮助,亚历克斯。

4

4 回答 4

2

您可以将数据写入 ByteBuffer,然后您可以通过简单的算法扭曲数据。例如,假设您要保存的数据是一个 String 数组,您可以这样做:

String[] data; // the data you want to save
int byteLength = 0;
byte[][] bytes = new byte[data.length][];

// Calculate the length of the content.
for(int i=0; i<data.length; i++) {
    bytes[i] = data[i].getBytes();
    byteLength += bytes[i].length;
    byteLength += 4; // this is for an integer, which is for the length of the String
}

// Transfer the content to a ByteBuffer object
ByteBuffer buffer = ByteBuffer.allocate(byteLength);
for(int i=0; i<bytes.length; i++) {
    // Put the length of the current byte array
    buffer.putInt(bytes[i].length);
    for(int j=0; j<bytes[i].length; j++) {
        // Reverse the byte so that it can't be understood
        buffer.put((byte)(~bytes[i][j])); 
    }
}

将所有内容写入 ByteBuffer 对象后,您可以获取生成的字节数组并将其写入文件。

FileOutputStream fos = new FileOutputStream("YourFileName.anyExtension");
fos.write(buffer.array());
fos.close();

在读回文件时,您应该首先读取一个整数,这是您应该作为字节数组读取的数据的长度,然后您应该读取这个字节数组。

FileInputStream fis = new FileInputStream("YourFileName.anyExtension");
DataInputStream dis = new DataInputStream(fis);
ArrayList<String> list = new ArrayList<String>();
byte[] bytes;
while(dis.available()) {
    int length = dis.readInt();
    bytes = new byte[length];
    for(int i=0; i<length; i++) {
        // Those bytes were reversed, right?
        bytes[i] = (byte)(~dis.readByte());
    }
    // Convert byte array to String
    String str = new String(bytes);
    list.add(str);
}

现在你有一个字符串数据的 ArrayList。

当然,这不是最好、最安全、最快的算法。您总是可以更快地找到或创建。但我认为这是做这类事情的一个很好的例子。

于 2012-08-09T23:25:28.667 回答
0

如果您使用 Java,您可以尝试创建一个实现 Serializable 的类。这样,您可以创建一个对象,其中存储了所有元信息,将其序列化,当您想加载它时,只需再次反序列化它。

虽然它不是很安全,因为你只需要知道它是用哪个类来反序列化的。但这是开始的事情。

于 2012-08-09T23:09:31.920 回答
0

查看数字签名,特别是 HMAC。这些几乎正是您所需要的,Java Crypto 框架应该使事情变得相当简单。这是一个可能相关的 SO 条目:如何在 Java 中生成与 Python 示例等效的 HMAC?

于 2012-08-09T23:10:29.283 回答
0

你可以通过你的文件写入流CipherOutputStream

生成一个随机字符串,或数字或任何东西。获取它的字节数组,生成一个密钥,然后用它来加密你的文件。

    byte password[] = (WHAT YOUR WANT. STRING, NUMBER, etc.).getBytes();
    DESKeySpec desKeySpec;
    try {

        desKeySpec = new DESKeySpec(password);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(desKeySpec);

        Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher.init(Cipher.ENCRYPT_MODE, key);

        // Create stream
        FileOutputStream fos = new FileOutputStream("Your file here");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
    }

现在您可以使用写入文件cos

使用SecretKey对象以相同的方式读取文件

SecretKey key = loadKey(); // Deserialize your SecretKey object
try {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            FileInputStream fis = new FileInputStream("Your file here");
            BufferedInputStream bis = new BufferedInputStream(fis);
            CipherInputStream cis = new CipherInputStream(bis, cipher);

现在你可以阅读使用cis

缺点是您需要保留SecretKey对象(序列化它或其他东西),任何低级黑客获取数据都不是问题(因为密钥存储在设备上)但它不允许只更改您的数据使用文本编辑器。

于 2012-08-09T23:20:07.277 回答