⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encryption.java~140~

📁 用JAVA编写的用密码学中DES算法加密解密文件的程序
💻 JAVA~140~
📖 第 1 页 / 共 2 页
字号:
    for(int i=0; i<16; i++)    {      passIP+=textInBi.charAt(IP[i]-1);    }    System.out.println("****************************passIP is "+passIP);    g1=passIP.substring(0,8);    g2=passIP.substring(8,16);    //first pass F and combine    passF=functionF(g1,subkey1);    System.out.println("**********************pass function F1 is "+passF);    passCombine1=combine(passF,g2);    System.out.println("*************************passCombine1 is "+passCombine1);    //second pass F and combine    passF=functionF(passCombine1,subkey2);    System.out.println("****************************pass function F2 is "+passF);    passCombine2=combine(passF,g1);    System.out.println("******************************passCombine2 is "+passCombine2);    //concate two subString with 8 bits    beforeIP_1=passCombine1+passCombine2;    System.out.println("the length of before IP-1 is "+beforeIP_1.length());    for(int i=0; i<16; i++)    {      passIP_1+=beforeIP_1.charAt(IP_1[i]-1);    }    System.out.println("passIP_1 is"+passIP_1);    subEncry=biToText(passIP_1);    return subEncry;  }  public String biToText(String bi)  {    String text="";    String subBi="";    int biToD=0;    System.out.println("bi is "+bi+" the length is "+bi.length());    for(int i=0; i<bi.length();i++)    {      subBi=bi.substring(i,i+8);      System.out.println("subString "+i+" is "+subBi);      i+=7;      biToD=Integer.parseInt(convertBToD(subBi));      System.out.println("decimal "+i+" is"+biToD);      text+=(char)biToD;      System.out.println("subtext is "+text);    }    return text;  }  public String textToBi(String subText)  {    String bi="";    String sub1="";    String sub2="";    int c1=subText.charAt(0);    int c2=subText.charAt(1);    sub1=convertDToB(c1,8);    System.out.println("sub1 is "+sub1);    sub2=convertDToB(c2,8);    bi=sub1+sub2;    System.out.println("&&&&&&&&&&&&&&&&&&&&&&&key is "+bi);    return bi;  }  public String functionF(String R, String subkey)  {    String E="812345456781";    String P="64735182";    String passE="";    String passCombine="";    String passSBox1="";    String passSBox2="";    String passSBox="";    String passP="";    //R pass E    for(int i=0; i<12; i++)    {      passE+=R.charAt(Integer.parseInt(E.substring(i,i+1))-1);    }    //E pass combination    passCombine+=combine(passE, subkey);    //get two substirng with 6 digits    String beforeSBox1=passCombine.substring(0,6);    String beforeSBox2=passCombine.substring(6,12);    //get two substring with 4 digits    passSBox1=passSBox(1,beforeSBox1);    passSBox2=passSBox(2,beforeSBox2);    //print out combine    System.out.println("box1 is ");    for(int i=0; i<passSBox1.length();i++)    {      System.out.print(passSBox1.charAt(i));    }    //print out combine    System.out.println("SBox2 is ");    for(int i=0; i<passSBox2.length();i++)    {      System.out.print(passSBox2.charAt(i));    }    //concate two substring into one    passSBox=passSBox1+passSBox2;    //pass P    for(int i=0; i<passSBox.length(); i++)    {      passP+=passSBox.charAt(Integer.parseInt(P.substring(i,i+1))-1);    }    return passP;  }  public String combine(String G1, String G2)  {    String combine="";    if(G1.length()==G2.length())    {      for(int i=0; i<G1.length(); i++)      {        combine+=XOR(G1.charAt(i),G2.charAt(i));      }    }    return combine;  }  public char XOR(char a, char b)  {    char c;    if(a==b)        c='0';    else        c='1';    return c;  }  //subString with 6 bits to pass box to get a string with 4 digits  public String passSBox(int box, String beforeBox)  {    String afterBox="";    //get row number in binary    String row=beforeBox.substring(0,1);    row+=beforeBox.charAt(5);    //get colum number in binary    String colum="";    for(int i=1; i<=4;i++)    {      colum+=beforeBox.charAt(i);    }    //convert binary to decimal to get row and colum in s-box    String inBoxRow=convertBToD(row);    String inBoxColum=convertBToD(colum);    int iRow=Integer.parseInt(inBoxRow);    int iColum=Integer.parseInt(inBoxColum);    afterBox=inSBox(box,iRow,iColum);    return afterBox;  }  //convert binary to decimal  public String convertBToD(String a)  {    int ctr=1, decimal=0;    for (int i = a.length()-1; i>=0; i--)    {      if(a.charAt(i)=='1')        decimal+=ctr;      ctr*=2;    }    String converted = "" + decimal;    return converted;  }  //find the number in S-Box  public String inSBox(int box, int row, int colum)  {    int boxNum=0;    String bin;    int[][] SBox1=    {      {14, 4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7},      {0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8},      {4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0},      {15, 12, 8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13},    };    int[][] SBox2=    {      {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},    };    if(box==1)      boxNum=SBox1[row][colum];    if(box==2)      boxNum=SBox2[row][colum];    //get binary with 4 digits in string from s-box    bin=convertDToB(boxNum,4);    return bin;  }  //convert decimal number into binary in String  public String convertDToB (int deci,int bits)  {    String bi="";    //convert decimal into binary    while (deci>=1)    {      if(deci%2==0)        bi+="0";      if(deci%2==1)        bi+="1";      deci/=2;    }    if(bits==4)    {      //let String bi has four digits      while (bi.length() != 4)      {        bi += "0";      }    }    if(bits==8)     {       //let String bi has four digits       while (bi.length() != 8)       {         bi += "0";       }     }    bi=inverse(bi);    return bi;  }//inverse binary order  public String inverse(String oldS)  {    String newS="";    for(int i=oldS.length()-1; i>=0; i--)    {      newS+=oldS.charAt(i);    }    return newS;  }  //calculate two subkeys  public  String[] calSubKey(String Key) {   String[] subKey = new String[2];  //PC1 condition  int[] PC1 = {11,15,4,13,7,9,3,               2,5,14,6,10,12,1};  int[] PC2 = {6,11,4,8,13,3,               12,5,1,10,2,9};  //the key After PC1  String passKey1= "";  //the subKey1 and subKey2  String subKey1 = "";  String subKey2 = "";  //the result of key after PC1  for(int i=0;i<PC1.length;i++)  {    passKey1 += Key.charAt(PC1[i]-1);  }  //Divide the result into left bits(cKey) and right bits(dKey)  String cKey = passKey1.substring(0,7);  String dKey = passKey1.substring(7);  //Calculate subKey1-------------------------------  //Shift to the right at 2 positions for both c and d  String c1Key = cKey.substring(5)+cKey.substring(0,5);  String d1Key = dKey.substring(5)+dKey.substring(0,5);  //combine c1 and d1  String tempKey1 = "";  tempKey1 = c1Key + d1Key;  //after PC2,get K1  for(int i=0;i<PC2.length;i++)  {    subKey1+=tempKey1.charAt(PC2[i]-1);  }  //Calculate subKey2----------------------------------  //Shift to the right at 2 positions for both c1 and d1  String c2Key = c1Key.substring(5)+c1Key.substring(0,5);  String d2Key = d1Key.substring(5)+d1Key.substring(0,5);  //combine c2 and d2  String tempKey2 = "";  tempKey2 = c2Key + d2Key;  //pass PC2 , get k2  for(int i=0;i<PC2.length;i++)  {    subKey2+=tempKey2.charAt(PC2[i]-1);  }  subKey[0] = subKey1;  subKey[1] = subKey2;  return subKey; }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -