做XOR后为什么一直得到“11111111”?

时间:2013-03-17 15:58:48

标签: java

这是我的代码:

import java.util.*;

public class DES {

static int S1[][]={{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10}, 
                   {3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
                   {0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
                   {13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}};

static int S2[][]={{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
                   {13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
                   {10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
                   {3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}};

static String[] L0;
static String[] R0;
static String[] L1;
static String[] R1;
static String[] L2;
static String[] R2;
static String[] L3;
static String[] R3;
static String[] L4;
static String[] R4;

static String[] K1={"1","0","1","0","1","0","1","0","1","0","1","0"};

static HashMap<String, Integer> NumMap = new HashMap<String, Integer>();

private static void Init()
{
    NumMap.put("a", 0);
    NumMap.put("b", 1);
    NumMap.put("c", 2);
    NumMap.put("d", 3);
    NumMap.put("e", 4);
    NumMap.put("f", 5);
    NumMap.put("g", 6);
    NumMap.put("h", 7);
    NumMap.put("i", 8);
    NumMap.put("j", 9);
    NumMap.put("k", 10);
    NumMap.put("l", 11);
    NumMap.put("m", 12);
    NumMap.put("n", 13);
    NumMap.put("o", 14);
    NumMap.put("p", 15);
    NumMap.put("q", 16);
    NumMap.put("r", 17);
    NumMap.put("s", 18);
    NumMap.put("t", 29);
    NumMap.put("u", 20);
    NumMap.put("v", 21);
    NumMap.put("w", 22);
    NumMap.put("x", 23);
    NumMap.put("y", 24);
    NumMap.put("z", 25);
    NumMap.put(" ", 26);
    NumMap.put(".", 27);
    NumMap.put(",", 28);
    NumMap.put("?", 29);
    NumMap.put("(", 30);
    NumMap.put(")", 31);
    NumMap.put("", 32);
}

public static int BiToDe(String[] Bi)
{
    int De=0;
    for(int i=0;i<Bi.length;i++)
    {
        if(Bi[i]=="1")
        {
            De+=(int)Math.pow(2,Bi.length-1-i);
        }
    }
    return De;
}

public static String[] Concat(String[] A, String[] B)
{
    int ALen=A.length;
    int BLen=B.length;
    String[] C=new String[ALen+BLen];
    System.arraycopy(A, 0, C, 0, ALen);
    System.arraycopy(B,0,C,ALen,BLen);
    return C;
}

public static String[] GetTail(String[] A)
{
    String[] Tail=new String[A.length/2];
    int j=0;
    for(int i=0;i<A.length;i++)
    {
        if(i%2==0)
        {
            Tail[j]=A[i];
            j++;
        }
    }
    return Tail;
}

public static String[] Xor(String[] A, String[] B)
{
    int ALen=A.length;
    String[] C=new String[ALen];
    for(int i=0;i<ALen;i++)
    {
        if(A[i]==B[i])
        {
            C[i]="0";
        }
        else
        {
            C[i]="1";
        }
    }
    return C;
}

public static String[] FunctionF(String[] InputBits, String[] RoundKey)
{
    String[] Tail=GetTail(InputBits);
    String[] ExpandedInput=Concat(InputBits, Tail);
    String[] Xored=Xor(ExpandedInput, RoundKey);

    String[] B1=Arrays.copyOfRange(Xored, 0, Xored.length/2);
    String[] B2=Arrays.copyOfRange(Xored, Xored.length/2, Xored.length);

    String[] Row1Array={B1[0],B1[B1.length-1]};

    String[] Column1Array=Arrays.copyOfRange(B1,1,B1.length-1);

    String[] Row2Array={B2[0],B2[B2.length-1]};

    String[] Column2Array=Arrays.copyOfRange(B2,1,B2.length-1);

    int CheckRow1=BiToDe(Row1Array);

    int CheckColumn1=BiToDe(Column1Array);

    int CheckRow2=BiToDe(Row2Array);

    int CheckColumn2=BiToDe(Column2Array);

    int NumOutput1=S1[CheckRow1][CheckColumn1];
    int NumOutput2=S2[CheckRow2][CheckColumn2];

    String[] Output={Integer.toBinaryString(0x10|NumOutput1).substring(1),
                     Integer.toBinaryString(0x10|NumOutput2).substring(1)};

    String[] FinalOutput=new String[Output.length*4];

    for(int i=0;i<Output.length;i++)
    {
        for(int j=0;j<4;j++)
        {
            FinalOutput[i*4+j]=Output[i].substring(j,j+1);
        }
    }

    return FinalOutput;
}

public static String[][] Divide(String[] A)
{
    int Blocks=(int)A.length/16+1;
    String[] temp;
    String[][] Divided= new String[Blocks][16];
    for(int i=0;i<Blocks;i++)
    {
        if(16*(i+1)<=A.length)
        {
            temp=Arrays.copyOfRange(A, 16*i, 16+16*i);
            for(int j=0;j<16;j++)
            {
                String t;
                t=temp[j];
                Divided[i][j]=t;
            }
        }
        else
        {
            temp=Arrays.copyOfRange(A, 16*i, A.length);
            for(int j=0;j<A.length;j++)
            {
                //String t;
                //t=temp[j];
                //Divided[i][j]=t;
            }
        }
    }

    return Divided;
}

public static void Encryption(String[][] Divided)
{
    L0=Arrays.copyOfRange(Divided[0], 0, 8);
    R0=Arrays.copyOfRange(Divided[0], 8, 16);
    L1=R0;
    String[] temp1=FunctionF(R0,K1);
    R1=Xor(L0,temp1);
    System.out.print("L0: \n");
    for(int i=0;i<8;i++)
    {
        System.out.print(L0[i]);
    }
    System.out.print("\n");
    System.out.print("R0: \n");
    for(int i=0;i<8;i++)
    {
        System.out.print(R0[i]);
    }
    System.out.print("\ntemp1: \n");
    for(int i=0;i<8;i++)
    {
        System.out.print(temp1[i]);
    }

    System.out.print("\n");

    for(int i=0;i<8;i++)
    {
        System.out.print(R1[i]);
    }
}

public static void main(String[] args) 
{
    Init();

    String PlainText = "how do you like computer science";
    String[] SplitText=PlainText.split("");
    Integer[] NumText=new Integer[SplitText.length];

    for(int i=1;i<SplitText.length;i++)
    {
        NumText[i]=NumMap.get(SplitText[i]);
    }

    Integer[] NewNumText=Arrays.copyOfRange(NumText, 1, NumText.length);

    String[] BinaryText=new String[NewNumText.length];

    for(int i=0;i<BinaryText.length;i++)
    {
        BinaryText[i]=Integer.toBinaryString(0x20|NewNumText[i]).substring(1);
    }

    String[] BitString=new String[5*BinaryText.length];

    for(int i=0;i<BinaryText.length;i++)
    {
        for(int j=0;j<5;j++)
        {
            BitString[i*5+j]=BinaryText[i].substring(j,j+1);
        }
    }


     String[][] DividedText=Divide(BitString);
     for(int i=0;i<(int)BitString.length/16;i++)
     {
         System.out.print("\n");
         for(int j=0;j<16;j++)
         {
             System.out.print(DividedText[i][j]);
         }

     }
     System.out.print("\n");
     Encryption(DividedText);

}


}

问题是,按功能Encryption(DividedText);,我得到L0=00111011temp1=10011110,但两者的Xor结果为11111111。我真的无法弄清楚发生了什么。 FunctionF中的Xor工作正常。

2 个答案:

答案 0 :(得分:9)

问题是在Xor方法中,您要将字符串与==进行比较。请改用.equals()。字符串上的==运算符测试对象标识,而不是值的相等。

public static String[] Xor(String[] A, String[] B)
{
    int ALen=A.length;
    String[] C=new String[ALen];
    for(int i=0;i<ALen;i++)
    {
        if(A[i].equals(B[i])) // NOT A[i]==B[i]
        {
            C[i]="0";
        }
        else
        {
            C[i]="1";
        }
    }
    return C;
}

答案 1 :(得分:0)

你总是记得不使用==来比较两个字符串,使用equals方法 在XOR函数中编辑== to equals方法