如何加密字符串然后解密它们

时间:2017-10-07 07:00:52

标签: java encryption

我试图在我的游戏中实施加密系统,以保护玩家的密码和其他游戏数据。

我已经拿出一点测试来熟悉这一切的逻辑。

我只是想知道通过阅读java并搜索我需要的逻辑(以及找到编写它的方法)来解决这个问题的最佳方法。所以说,我不是在寻找一些完全适合我的游戏的片段。我正在寻找更好的逻辑然后我拥有的东西。

这只适用于4个字符。

到目前为止我所知道的

import java.util.Random;

public class EncodingThenDecoding {
private String stringToHash;
private boolean running = false;
private String originalString;
private String encodedString;
private boolean decoding = false;
public EncodingThenDecoding() {
    init();
}

public void initInit() {
    running = true;
    encodeString("abcd");
    System.out.println("Original String: " + originalString);
    delay();
    decodeStrings();

}

private void encodeString(String sth) {
    String[] subStrings = new String[25];
    originalString = sth;
    for(int subStr = 0;subStr < sth.length();subStr++ ) {
        subStrings[subStr] = sth.substring(subStr);
    }
    for(int i = 0;i < sth.length();i++) {
        Random ran = new Random();
        StringBuilder encoder = new StringBuilder();
        encoder.append( (char)('a' + ran.nextInt('z'-'a')));
        subStrings[i] = encoder.toString();
        //System.out.println(subStrings[i]);
    }

    sth = subStrings[0] + subStrings[1] + subStrings[2] + subStrings[3];
    encodedString = sth;
    System.out.println(sth);
    delay();
}

private void decodeString() {
    int tries = 0;
    int ii = 0;
    running = true;
    long sd = System.nanoTime();
    long minutes = 0;
    while(running) {
        tries++;
        String strToDecode = encodedString;
        String[] usedDecodedStrings = new String[1000000];
        String[] decodedSubStrings = new String[25];
        String decodedString = null;

        //store hashed string's chars into an array.
        for(int i = 0; i < strToDecode.length();i++) {
            decodedSubStrings[i] = strToDecode.substring(i);
        }
        //stores a random letter between z-a and replaces the array items         above.
        for(int i = 0;i < decodedSubStrings.length;i++) {
            Random ran = new Random();
            StringBuilder encoder = new StringBuilder();
            encoder.append( (char)('a' + ran.nextInt('z'-'a')));
            decodedSubStrings[i] = encoder.toString();

        }
        //stores the string containing all the new characters assigned above, 
        decodedString = decodedSubStrings[0] + decodedSubStrings[1] + decodedSubStrings[2]  + decodedSubStrings[3];


        long nanoseconds = System.nanoTime() - sd;
        long miliseconds = nanoseconds/1000000;
        long seconds = miliseconds/1000;

            System.out.println("its been " + seconds + " seconds");
        if(decodedString.equalsIgnoreCase(originalString)) {
            System.out.println("Decoding username was succesful!");
            System.out.println("it took " + seconds/60 + " minutes and " + tries + " tries to Decode " + strToDecode + " back into "  + originalString);
            return;
        }
        else if(!decodedString.equalsIgnoreCase(originalString)) {
            System.out.println("Attempt #" + tries + ": " + decodedString);
        }

    }


}

public void delay() {
    try {
        Thread.sleep(3000);
    }
    catch(InterruptedException e) {

    }
}

public static void main(String[] args) {
      EncodingThenDecoding encryptThenDecrypt = new   EncodingThenDecoding();
}

}

下面是输出

的示例

已经过了1386秒

尝试#339016:bsev

已经过了1386秒

尝试#339017:qycu

已经过了1386秒

解码用户名成功!

花了23分钟和339018次尝试将vlbc解码回abcd

1 个答案:

答案 0 :(得分:1)

答:不要自己实施加密。

Java语言中已经有许多标准加密算法可用。一些流行的算法是 RSA(不对称) AES(对称)

查看Java Cryptography Architecture.

如果要存储密码,则应使用某些散列算法(如 SHA-256 )或更好的 SHA-512 存储密码。在散列之前添加一些安全的 Salt 会使攻击者更加困难。