【问题标题】:Problem at XOR encrypting a String of bits that contains empty spacesXOR 加密包含空格的位字符串时出现问题
【发布时间】:2019-01-29 18:59:41
【问题描述】:

我这里有一个密码作为二进制字符串:

01110101 01101000 01010100 01000100 01000011 01000100 00110111 00111000 00110110 00110010 00110100 01100111 00100110 00110000 00111001 00111000 00111101 ...

以及二进制字符串形式的消息:

01001000 01100001 01101100 01101100 01101111 01101100 01100101 01101100 01100101 01100001 01110011 01100100 01100110

我尝试了许多不同的方法来异或这两个字符串,这是我最近的尝试:

public static String encrypt(String str) {
        String pwBinary = getBinaryString(pw);
        String msgBinary = getBinaryString(str);
        StringBuilder temp = new StringBuilder();


        int count = 0;


        for(int i = 0; i < msgBinary.length(); i++) {


            if(!(i%8==count)) {

                if(msgBinary.charAt(i) == pwBinary.charAt(i)) {

                    temp.append(0);
                }else {

                    temp.append(1);
                }


            }else if(i!=0) {
                temp.append(" ");
                count++;
                i++;
            }

        }
        return temp.toString();
    }

输出总是在中间的某个地方开始混乱......

0111101 0001001 0111000 0101000 0101100 0101000 1010010 1010100 10100110010100110010001110000000110010000000

我不知道如何正确填写空格:( 在此先感谢:-)

【问题讨论】:

  • 请注意,每个位块的长度为 9,而不是 8,因为空间是块宽度的一部分。这说明i % 8错了,应该是i % 9
  • if(!(msgBinary.charAt(i) != ' '))... 假设它们具有相同的模式。
  • 我想我试过了,但它说我不能比较一个字符和一个字符串

标签: java encryption bit-manipulation bit


【解决方案1】:

我找到了一个简单的解决方案,尽管我花了一段时间才发现我的推理错误:

public static String encrypt(String str) {
        String pwBinary = getBinaryString(pw);
        String msgBinary = getBinaryString(str);
        StringBuilder temp = new StringBuilder();

        int x = 8;

        for(int i = 0; i < msgBinary.length(); i++) {


            if(i == x) {
                temp.append(" ");
                x+=9;

            }else if(msgBinary.charAt(i) == pwBinary.charAt(i)) {

            temp.append(0);
            }else {

            temp.append(1);
            }


        }
        return temp.toString();
    }

【讨论】:

    【解决方案2】:

    您的解决方案的工作版本(注释不合逻辑的部分)是:

    public static String encrypt(String str) {
        String pwBinary = getBinaryString(pw);
        String msgBinary = getBinaryString(str);
        StringBuilder temp = new StringBuilder();
        //no idea what count was supposed to do!!!
        for(int i = 0; i < msgBinary.length(); i++) {
            if(((i+1)%9!=0)) {//the space occurs once in every 9 times
                if(msgBinary.charAt(i) == pwBinary.charAt(i)) {
                    temp.append(0);
                }else {
                    temp.append(1);
                }
            }else {
                temp.append(" ");
                //why should we increment i? the for loop does it automatically!
            }
        }
        return temp.toString();
    }
    

    但实现目标的更快(cpu 工作负载)方法是:

    public static String encrypt(String str) {
        String pw = "pass";
        byte[] pwb=pw.getBytes(),msb=str.getBytes(),xor_result;
        xor_result=new byte[msb.length];
        int j=0;
        while(j<pwb.length){
            xor_result[j]=(byte)(0xff &(pwb[j]^msb[j]));//you see in this code at least 8 bits are xored in one instruction depending on the processor(or am i wrong!?)
            j++;
        }
        return getBinaryString(new String( xor_result));
    }
    

    【讨论】:

    • 你在那里编码了一个“0xff &”......自从 11111111 和 anotherNumber 没有改变任何东西之后,这不是已经过时了吗?
    • 删除结果 int 的前 3 个字节,但我不确定 java 是否自己做。
    猜你喜欢
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多