Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 游戏数据的基本加密_Java_Encryption - Fatal编程技术网

Java 游戏数据的基本加密

Java 游戏数据的基本加密,java,encryption,Java,Encryption,我想我正在寻找一些基本的文件加密,但不知道从哪里开始 我正在找人告诉我从哪里开始寻找,或者,更好的是,提供一些代码 我写了一个游戏,目前将数据保存到通用文本文件中。当然,任何人都可以改变这一点 我需要的是创建一个文件,可以存储整数和字符串,这是困难的,如果不是不可能在游戏之外编辑 在我的搜索中,我遇到了.dat文件,但它们似乎比我要查找的更复杂 非常感谢您的帮助,Alex。如果您正在使用Java,您可以尝试创建一个实现可序列化的类,这样您就可以创建一个包含所有元信息的对象,并对其进行序列化,当您

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

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

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

我需要的是创建一个文件,可以存储整数和字符串,这是困难的,如果不是不可能在游戏之外编辑

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


非常感谢您的帮助,Alex。

如果您正在使用Java,您可以尝试创建一个实现可序列化的类,这样您就可以创建一个包含所有元信息的对象,并对其进行序列化,当您想加载它时,再对其进行反序列化


但这并不十分安全,因为您只需要知道它所使用的类,就可以反序列化它。但这是一个开始。

看看,特别是HMAC。这些正是您所需要的,Java加密框架应该使事情变得相当简单。这里有一个潜在的相关SO条目:

您可以通过
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
对象(序列化它或其他东西)。对于任何低级黑客来说,获取数据都不是问题(因为密钥存储在设备上)但它不允许只使用文本编辑器更改数据。

您可以将数据写入ByteBuffer,然后通过简单的算法扭曲数据。例如,假设要保存的数据是字符串数组,可以执行以下操作:

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])); 
    }
}
在读回文件时,应首先读取一个整数,即应读取为字节数组的数据长度,然后读取此字节数组

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);
}
FileInputStream-fis=newfileinputstream(“YourFileName.anyExtension”);
DataInputStream dis=新的DataInputStream(fis);
ArrayList=新建ArrayList();
字节[]字节;
while(dis.available()){
int length=dis.readInt();
字节=新字节[长度];

对于(int i=0;我知道你在搜索吗?谷歌和其他搜索引擎都提供了大量的点击率。这就是我的出发点。是的,我发现的内容包括非常详细的加密,与隐藏基本信息无关。我没有发现与游戏直接相关的内容,我希望从有经验的人那里听到一些信息。游戏数据是不相关的“基本信息”和“信息”没有区别。谷歌搜索中的第一个答案涵盖了从XOR到ROT-13再到实际加密的所有内容。你说“即使不是不可能在游戏之外编辑也很困难”——你认为如果没有“非常详细的加密”,你会怎么做?我想我是乐观的,这是游戏数据的事实意味着它不需要非常安全,但也不能被普通人改变。@Alexmask声明仍然有效——搜索SO和谷歌;有大量的例子,从简单到复杂。我不确定你所说的“不可能”除了“真的,真的很难”。见鬼,你可以把它拉上拉链,换个分机。
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);
}