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

📄 des.java

📁 DSS数字签名
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package encryption;
//Des.java

public class Des {
  //选择函数
  //表s1
  protected static byte[][] s1 = {
  {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 }};

  //表s2
  protected static byte[][] s2 = {
  {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 }};

  //表s3
  protected static byte[][] s3= {
  {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
  {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
  {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
  {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 }};

  //表s4
  protected static byte[][] s4 = {
  {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 }};

  //表s5
  protected static byte[][] s5 = {
  {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
  {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
  {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
  {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 }};

 //表s6
  protected static byte[][] s6 = {
  {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
  {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
  {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
  {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 }};

  //表s7
  protected static byte[][] s7 = {
  {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
  {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
  {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
  {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}};

 //表s8
  protected static byte[][] s8 = {
  {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
  {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
  {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
  {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}};
  //用于 S盒查找以确定对应的4位二进制数据
  protected static byte[] binary={
        0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1,
        0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1,
        1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,
        1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 };

  //存储将要加密的明文或将要解密的密文
  protected byte[] tempData=new byte[64];
  protected String Srcdata;
  protected String Tagdata;

  //用于数据变换中转
  protected byte[] bufout=new byte[64];
  //最终生成的密文或解密所得的明文
  protected byte[] output=new byte[64];
  protected byte[] Ln=new byte[32];
  protected byte[] Rn=new byte[32];
  protected byte[] LR=new byte[32];
  protected byte[] ER=new byte[48];
  protected byte[] temp=new byte[32];
  protected byte[] result=new byte[8];
  protected byte[] ki;
  protected String key;
  protected int readLen;
  protected boolean encFlag;   //1为加密,0为解密
  protected SubKey subKey;


  protected void Char4Tobit64(String data){
        for (int i = 0; i<64; i++) {
                tempData[i]=0;
        }
        //将4个字符的unicode码数据转换成64位数据
        for (int i = 0; i < readLen; i++) {
                int j=data.charAt(i);
                tempData[16*i+0]=(byte)((j/32768)%2);
                tempData[16*i+1]=(byte)((j/16384)%2);
                tempData[16*i+2]=(byte)((j/8192)%2);
                tempData[16*i+3]=(byte)((j/4096)%2);
                tempData[16*i+4]=(byte)((j/2048)%2);
                tempData[16*i+5]=(byte)((j/1024)%2);
                tempData[16*i+6]=(byte)((j/512)%2);
                tempData[16*i+7]=(byte)((j/256)%2);
                tempData[16*i+8]=(byte)((j/128)%2);
                tempData[16*i+9]=(byte)((j/64)%2);
                tempData[16*i+10]=(byte)((j/32)%2);
                tempData[16*i+11]=(byte)((j/16)%2);
                tempData[16*i+12]=(byte)((j/8)%2);
                tempData[16*i+13]=(byte)((j/4)%2);
                tempData[16*i+14]=(byte)((j/2)%2);
                tempData[16*i+15]=(byte)(j%2);
         }
  }

  protected void Bit64To4Char(){
        int j;
        char ch;
        StringBuffer strbuf=new StringBuffer();
        for (int i = 0; i<4; i++) {
                j=0;
                j=32768*output[16*i+0]+16384*output[16*i+1]+8192*output[16*i+2]+4096*output[16*i+3]+
                2048*output[16*i+4]+1024*output[16*i+5]+512*output[16*i+6]+256*output[16*i+7]+
                128*output[16*i+8]+64*output[16*i+9]+32*output[16*i+10]+16*output[16*i+11]+
                8*output[16*i+12]+4*output[16*i+13]+2*output[16*i+14]+output[16*i+15];
                ch=(char)j;
                strbuf.append(ch);
        }
        Tagdata=strbuf.toString();
}

  protected void IP(){
        /* 初始数据置换 */
        bufout[ 0] = tempData[57];
        bufout[ 1] = tempData[49];
        bufout[ 2] = tempData[41];
        bufout[ 3] = tempData[33];
        bufout[ 4] = tempData[25];
        bufout[ 5] = tempData[17];
        bufout[ 6] = tempData[ 9];
        bufout[ 7] = tempData[ 1];
        bufout[ 8] = tempData[59];
        bufout[ 9] = tempData[51];
        bufout[10] = tempData[43];
        bufout[11] = tempData[35];
        bufout[12] = tempData[27];
        bufout[13] = tempData[19];
        bufout[14] = tempData[11];
        bufout[15] = tempData[ 3];
        bufout[16] = tempData[61];
        bufout[17] = tempData[53];
        bufout[18] = tempData[45];
        bufout[19] = tempData[37];
        bufout[20] = tempData[29];
        bufout[21] = tempData[21];
        bufout[22] = tempData[13];
        bufout[23] = tempData[ 5];
        bufout[24] = tempData[63];
        bufout[25] = tempData[55];
        bufout[26] = tempData[47];
        bufout[27] = tempData[39];
        bufout[28] = tempData[31];
        bufout[29] = tempData[23];
        bufout[30] = tempData[15];
        bufout[31] = tempData[ 7];
        bufout[32] = tempData[56];
        bufout[33] = tempData[48];
        bufout[34] = tempData[40];
        bufout[35] = tempData[32];
        bufout[36] = tempData[24];
        bufout[37] = tempData[16];
        bufout[38] = tempData[ 8];
        bufout[39] = tempData[ 0];
        bufout[40] = tempData[58];
        bufout[41] = tempData[50];
        bufout[42] = tempData[42];
        bufout[43] = tempData[34];
        bufout[44] = tempData[26];
        bufout[45] = tempData[18];
        bufout[46] = tempData[10];
        bufout[47] = tempData[ 2];
        bufout[48] = tempData[60];
        bufout[49] = tempData[52];
        bufout[50] = tempData[44];
        bufout[51] = tempData[36];
        bufout[52] = tempData[28];
        bufout[53] = tempData[20];
        bufout[54] = tempData[12];
        bufout[55] = tempData[ 4];
        bufout[56] = tempData[62];
        bufout[57] = tempData[54];
        bufout[58] = tempData[46];
        bufout[59] = tempData[38];
        bufout[60] = tempData[30];
        bufout[61] = tempData[22];
        bufout[62] = tempData[14];
        bufout[63] = tempData[ 6];

  }

  protected void XOR(byte[] op1,byte[] op2){
        int len=op1.length;
        for (int i = 0; i<len; i++) {
                op1[i]=(byte)(op1[i] ^ op2[i]);
        }
  }

  protected void expand32To48bit(byte[] op){
        /* Permute - E */
        ER[ 0] = op[31];
        ER[ 1] = op[ 0];
        ER[ 2] = op[ 1];
        ER[ 3] = op[ 2];
        ER[ 4] = op[ 3];
        ER[ 5] = op[ 4];
        ER[ 6] = op[ 3];
        ER[ 7] = op[ 4];
        ER[ 8] = op[ 5];
        ER[9] = op[ 6];
        ER[10] = op[ 7];
        ER[11] = op[ 8];
        ER[12] = op[ 7];
        ER[13] = op[ 8];
        ER[14] = op[ 9];
        ER[15] = op[10];
        ER[16] = op[11];
        ER[17] = op[12];
        ER[18] = op[11];
        ER[19] = op[12];
        ER[20] = op[13];
        ER[21] = op[14];
        ER[22] = op[15];
        ER[23] = op[16];
        ER[24] = op[15];
        ER[25] = op[16];
        ER[26] = op[17];
        ER[27] = op[18];
        ER[28] = op[19];
        ER[29] = op[20];
        ER[30] = op[19];
        ER[31] = op[20];
        ER[32] = op[21];
        ER[33] = op[22];
        ER[34] = op[23];
        ER[35] = op[24];
        ER[36] = op[23];
        ER[37] = op[24];
        ER[38] = op[25];
        ER[39] = op[26];
        ER[40] = op[27];
        ER[41] = op[28];
        ER[42] = op[27];
        ER[43] = op[28];
        ER[44] = op[29];
        ER[45] = op[30];
        ER[46] = op[31];
        ER[47] = op[ 0];
  }
  protected void sBox(){
        int valindex;
        valindex = s1[2*ER[ 0]+ER[ 5]][2*(2*(2*ER[ 1]+ER[ 2])+ ER[ 3])+ER[ 4]];
        valindex = valindex * 4;
        temp[ 0] = (byte)binary[0+valindex];
        temp[ 1] = (byte)binary[1+valindex];
        temp[ 2] = (byte)binary[2+valindex];
        temp[ 3] = (byte)binary[3+valindex];
        valindex = s2[2*ER[ 6]+ER[11]][2*(2*(2*ER[ 7]+ER[ 8])+ ER[ 9])+ER[10]];
        valindex = valindex = valindex * 4;
        temp[ 4] = (byte)binary[0+valindex];
        temp[ 5] = (byte)binary[1+valindex];
        temp[ 6] = (byte)binary[2+valindex];
        temp[ 7] = (byte)binary[3+valindex];
        valindex = s3[2*ER[12]+ER[17]][2*(2*(2*ER[13]+ER[14])+ ER[15])+ER[16]];
        valindex = valindex = valindex * 4;
        temp[ 8] = (byte)binary[0+valindex];
        temp[ 9] = (byte)binary[1+valindex];
        temp[10] = (byte)binary[2+valindex];

⌨️ 快捷键说明

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