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

📄 calculation.java

📁 S-DES加、解密程序
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package s_des;/** * * @author Administrator */public class Calculation {private int[][] S0;    private int[][] S1;    public int[] plain;    public int[] IP;        public int[] step1EP;    public int[] step1XOR1;    public int[] step1S;    public int[] step1P4;    public int[] step1XOR2;        public int[] change;        public int[] step2EP;    public int[] step2XOR1;    public int[] step2S;    public int[] step2P4;    public int[] step2XOR2;    public int[] cipherText;          Calculation(){        plain = new int[8];        IP = new int[8];                step1EP = new int[8];        step1XOR1 = new int[8];        step1S = new int[4];        step1P4 = new int[4];        step1XOR2 = new int[4];        change = new int[8];                step2EP = new int[8];        step2XOR1 = new int[8];        step2S = new int[4];        step2P4 = new int[4];        step2XOR2 = new int[4];        cipherText = new int[8];                        S0 = new int[4][4];                S0[0][0] = 1; S0[0][1] = 0; S0[0][2] = 3; S0[0][3] = 2;        S0[1][0] = 3; S0[1][1] = 2; S0[1][2] = 1; S0[1][3] = 0;        S0[2][0] = 0; S0[2][1] = 2; S0[2][2] = 1; S0[2][3] = 3;        S0[3][0] = 3; S0[3][1] = 1; S0[3][2] = 3; S0[3][3] = 2;                S1 = new int[4][4];        S1[0][0] = 0; S1[0][1] = 1; S1[0][2] = 2; S1[0][3] = 3;        S1[1][0] = 2; S1[1][1] = 0; S1[1][2] = 1; S1[1][3] = 3;        S1[2][0] = 3; S1[2][1] = 0; S1[2][2] = 1; S1[2][3] = 0;        S1[3][0] = 2; S1[3][1] = 1; S1[3][2] = 0; S1[3][3] = 3;            }        public void calculateCipher(int[] inf,int[] K1,int[] K2){        getInf(inf,plain);        step1IP(plain,IP);        step1EP(IP,step1EP);        step1XOR1(step1EP,step1XOR1,K1);        step1S(step1XOR1,step1S);        step1P4(step1S,step1P4);        step1XOR2(IP,step1P4,step1XOR2);        step1Switch(IP,step1XOR2,change);                step2EP(change,step2EP);        step2XOR1(step2EP,step2XOR1,K2);        step2S(step2XOR1,step2S);        step2P4(step2S,step2P4);        step2XOR2(change,step2P4,step2XOR2);        step2IP(change,step2XOR2,cipherText);                disPlay(plain);disPlay(IP);                disPlay(step1EP);        disPlay(step1XOR1);disPlay(step1S);disPlay(step1P4);        disPlay(step1XOR2);disPlay(change);                    disPlay(step2EP);                disPlay(step2XOR1);disPlay(step2S);disPlay(step2P4);         disPlay(step2XOR2);disPlay(cipherText);    }    public void calculatePlain(int[] inf,int[] K1,int[] K2){        getInf(inf,cipherText);        step1IP(cipherText,IP);                step1EP(IP,step1EP);        step1XOR1(step1EP,step1XOR1,K2);        step1S(step1XOR1,step1S);        step1P4(step1S,step1P4);        step1XOR2(IP,step1P4,step1XOR2);                step1Switch(IP,step1XOR2,change);                step2EP(change,step2EP);        step2XOR1(step2EP,step2XOR1,K1);        step2S(step2XOR1,step2S);        step2P4(step2S,step2P4);        step2XOR2(change,step2P4,step2XOR2);                step2IP(change,step2XOR2,plain);                disPlay(plain);disPlay(IP);                disPlay(step1EP);        disPlay(step1XOR1);disPlay(step1S);disPlay(step1P4);        disPlay(step1XOR2);disPlay(change);                    disPlay(step2EP);                disPlay(step2XOR1);disPlay(step2S);disPlay(step2P4);         disPlay(step2XOR2);disPlay(cipherText);    }    private void getInf(int[] inf ,int[] vary){        for(int i = 0; i < 8; i++)vary[i] = inf[i];    }        private void step1IP(int[] inf ,int[] vary){        vary[0] = inf[1];        vary[1] = inf[5];        vary[2] = inf[2];        vary[3] = inf[0];        vary[4] = inf[3];        vary[5] = inf[7];        vary[6] = inf[4];        vary[7] = inf[6];    }    private void step1EP(int[] inf, int[] vary){        int[] temp = new int[4];        for(int i = 0; i < 4; i++)temp[i] = inf[i + 4];        vary[0] = temp[3];        vary[1] = temp[0];        vary[2] = temp[1];        vary[3] = temp[2];        vary[4] = temp[1];        vary[5] = temp[2];        vary[6] = temp[3];        vary[7] = temp[0];    }    private void step1XOR1(int[] inf,int[] vary,int[] K1){        for(int i = 0; i < 8; i++){            if(inf[i] == K1[i])vary[i] = 0;            else vary[i] = 1;        }    }    private void step1S(int[] inf,int[] vary){        int[] temp = new int[4];        if(inf[0] == 0 && inf[3] == 0) temp[0] = 0;        if(inf[0] == 0 && inf[3] == 1) temp[0] = 1;        if(inf[0] == 1 && inf[3] == 0) temp[0] = 2;        if(inf[0] == 1 && inf[3] == 1) temp[0] = 3;                if(inf[1] == 0 && inf[2] == 0) temp[1] = 0;        if(inf[1] == 0 && inf[2] == 1) temp[1] = 1;        if(inf[1] == 1 && inf[2] == 0) temp[1] = 2;        if(inf[1] == 1 && inf[2] == 1) temp[1] = 3;                if(inf[4] == 0 && inf[7] == 0) temp[2] = 0;        if(inf[4] == 0 && inf[7] == 1) temp[2] = 1;        if(inf[4] == 1 && inf[7] == 0) temp[2] = 2;        if(inf[4] == 1 && inf[7] == 1) temp[2] = 3;               if(inf[5] == 0 && inf[6] == 0) temp[3] = 0;        if(inf[5] == 0 && inf[6] == 1) temp[3] = 1;        if(inf[5] == 1 && inf[6] == 0) temp[3] = 2;        if(inf[5] == 1 && inf[6] == 1) temp[3] = 3;                int[] temp1 = new int[2];        temp1[0] = S0[temp[0]][temp[1]];        temp1[1] = S1[temp[2]][temp[3]];                if(temp1[0] == 0){            vary[0] = 0;            vary[1] = 0;        }        if(temp1[0] == 1){            vary[0] = 0;            vary[1] = 1;        }        if(temp1[0] == 2){            vary[0] = 1;            vary[1] = 0;        }        if(temp1[0] == 3){            vary[0] = 1;            vary[1] = 1;        }        if(temp1[1] == 0){            vary[2] = 0;            vary[3] = 0;        }        if(temp1[1] == 1){            vary[2] = 0;            vary[3] = 1;        }        if(temp1[1] == 2){            vary[2] = 1;            vary[3] = 0;        }        if(temp1[1] == 3){            vary[2] = 1;            vary[3] = 1;        }             }    private void step1P4(int[] inf,int[] vary){        vary[0] = inf[1];        vary[1] = inf[3];        vary[2] = inf[2];        vary[3] = inf[0];    }    private void step1XOR2(int[] inf1,int[] inf2,int[] vary){        for(int i = 0; i < 4; i++){            if(inf1[i] == inf2[i])vary[i] = 0;            else vary[i] = 1;        }    }    private void step1Switch(int[] inf1,int[] inf2,int[] vary){        for(int i = 0; i < 4; i++){            vary[i] = inf1[i+4];            vary[i+4] = inf2[i];        }    }    //step2    private void step2EP(int[] inf,int[] vary){        int[] temp = new int[4];        for(int i = 0; i < 4; i++)temp[i] = inf[i + 4];        vary[0] = temp[3];        vary[1] = temp[0];        vary[2] = temp[1];        vary[3] = temp[2];        vary[4] = temp[1];        vary[5] = temp[2];        vary[6] = temp[3];        vary[7] = temp[0];    }        private void step2XOR1(int[] inf1,int[] vary,int[] K2){        for(int i = 0; i < 8; i++){            if(inf1[i] == K2[i])vary[i] = 0;            else vary[i] = 1;        }    }        private void step2S(int[] inf,int[] vary){        int[] temp = new int[4];        if(inf[0] == 0 && inf[3] == 0) temp[0] = 0;        if(inf[0] == 0 && inf[3] == 1) temp[0] = 1;        if(inf[0] == 1 && inf[3] == 0) temp[0] = 2;        if(inf[0] == 1 && inf[3] == 1) temp[0] = 3;                if(inf[1] == 0 && inf[2] == 0) temp[1] = 0;        if(inf[1] == 0 && inf[2] == 1) temp[1] = 1;        if(inf[1] == 1 && inf[2] == 0) temp[1] = 2;        if(inf[1] == 1 && inf[2] == 1) temp[1] = 3;                if(inf[4] == 0 && inf[7] == 0) temp[2] = 0;        if(inf[4] == 0 && inf[7] == 1) temp[2] = 1;        if(inf[4] == 1 && inf[7] == 0) temp[2] = 2;        if(inf[4] == 1 && inf[7] == 1) temp[2] = 3;               if(inf[5] == 0 && inf[6] == 0) temp[3] = 0;        if(inf[5] == 0 && inf[6] == 1) temp[3] = 1;        if(inf[5] == 1 && inf[6] == 0) temp[3] = 2;        if(inf[5] == 1 && inf[6] == 1) temp[3] = 3;                int[] temp1 = new int[2];        temp1[0] = S0[temp[0]][temp[1]];        temp1[1] = S1[temp[2]][temp[3]];                if(temp1[0] == 0){            vary[0] = 0;            vary[1] = 0;        }        if(temp1[0] == 1){            vary[0] = 0;            vary[1] = 1;        }        if(temp1[0] == 2){            vary[0] = 1;            vary[1] = 0;        }        if(temp1[0] == 3){            vary[0] = 1;            vary[1] = 1;        }        if(temp1[1] == 0){            vary[2] = 0;            vary[3] = 0;        }        if(temp1[1] == 1){            vary[2] = 0;            vary[3] = 1;        }        if(temp1[1] == 2){            vary[2] = 1;            vary[3] = 0;        }        if(temp1[1] == 3){            vary[2] = 1;            vary[3] = 1;        }             }        private void step2P4(int[] inf,int[] vary){        vary[0] = inf[1];        vary[1] = inf[3];        vary[2] = inf[2];        vary[3] = inf[0];    }        private void step2XOR2(int[] inf1,int[] inf2,int[] vary){        for(int i = 0; i < 4; i++){            if(inf1[i] == inf2[i])vary[i] = 0;            else vary[i] = 1;        }    }    private void step2IP(int[] inf1,int[] inf2,int[] vary){        int[] temp = new int[8];        for(int i = 0; i < 4;i++){            temp[i + 4] = inf1[i + 4];            temp[i] = inf2[i];        }        vary[0] = temp[3];        vary[1] = temp[0];        vary[2] = temp[2];        vary[3] = temp[4];        vary[4] = temp[6];        vary[5] = temp[1];        vary[6] = temp[7];        vary[7] = temp[5];    }    private void disPlay(int[] inf){        for(int i = 0;i < inf.length;i++){            System.out.print(" " + inf[i]);        }        System.out.println();    }}

⌨️ 快捷键说明

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