加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

用java语言模拟硬件上的DES加密过程

发布时间:2020-12-15 00:17:15 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.util.ArrayList;import java.util.Iterator;import java.util.Scanner;public class DES { public static void main(String[] args) { St

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class DES {

    public static void main(String[] args) {

        String k = "0111000 0 0011100 0 1001101 0 1110110 0 0111011 0 1001001 0 1000010 0 1101101 0";
        k = k.replaceAll(" ","");
        System.out.print("请输入要加密的明文:");
        Scanner scn = new Scanner(System.in);
        System.out.println("密文为:"+getCode(scn.next(),k));

    }

    //将ArrayList中的字符串加密
    public static String getCode(String s,String k) {
        ArrayList<String> al = strToBinary(s);
        String str = "";
        for(Iterator<String> it = al.iterator(); it.hasNext();) {
            str += encry(it.next(),k);
        }

        return str;
    }

    //将字符串的每四位转化成二进制代码存放在ArrayList中
    public static ArrayList<String> strToBinary(String s) {
        ArrayList<String> al = new ArrayList<String>();
        String temp = "";
        byte[] buf = s.getBytes();
        for(int i = 0 ; i < buf.length;) {
            temp += byteToBit(buf[i++]);
            if(temp.length() == 64) {
                al.add(temp);
                temp = "";
            }else if(i == buf.length && temp.length() < 64) {
                while(temp.length() < 64) {temp = "0"+temp;}
                al.add(temp);
            }

        }

        return al;
    }

    //将每个byte转化成8个二进制位
    public static String byteToBit(byte b) {
        String s = Integer.toBinaryString(b);
        while(s.length() < 8) {s = "0"+s;}
        return s;
    }

    //初始置换IP表
    final static int[][] initIP = {{58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4},{62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8},{57,49,41,33,25,17,9,1,59,51,43,35,27,19,11,3},{61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7}};
    //密钥置换表
    final static int[][] keyPermu = {{57,58,18},{10,3,36},{63,7,62,22},{14,61,4}};
    //每轮循环左移的位数表
    final static int[] moveBit = {1,1};
    //压缩置换表
    final static int[][] compressPermu = {{14,10},{23,4,8,2 },{41,48},{44,32}};
    //扩展变换表
    final static int[][] extendPermu = {{32,9},{8,17},{16,25},{24,1}};
    //8个S盒
    final static int[][] sBox1 = {{14,7},{0,{4,0},{15,13}};

    final static int[][] sBox2 = {{15,{3,5},15},{13,9}};

    final static int[][] sBox3 = {{10,1},{1,12}};

    final static int[][] sBox4 = {{7,14}};

    final static int[][] sBox5 = {{2,6},14},{11,3}};

    final static int[][] sBox6 = {{12,11},{9,13}};

    final static int[][] sBox7 = {{4,2},{6,12}};

    final static int[][] sBox8 = {{13,{7,{2,11}};

    //P——盒转换表
    final static int[][] pBox = {{16,25}};
    //末尾置换IP逆表
    final static int[][] endIPpermu = {{40,31},{38,29},{36,27},{34,57,25}};

    //初始置换,IP置换
    public static String initPermu(String s) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < initIP.length; i++) {
            for(int j = 0; j < initIP[i].length; j++) {
                sb.append(s.charAt(initIP[i][j]-1));
            }
        }

        return sb.toString();
    }

    //密钥转换
    public static String keyPermu(String s) {

        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < keyPermu.length; i++) {
            for(int j = 0; j < keyPermu[i].length; j++) {
                sb.append(s.charAt(keyPermu[i][j]-1));
            }
        }

        return sb.toString();       
    }

    public static String keyMove(String key,int times) {
        int bit = moveBit[times-1];

        int l = key.length();
        String pre = key.substring(0,l/2);
        String bef = key.substring(l/2,l);
        //密钥移位
        pre = pre.substring(bit)+pre.substring(0,bit);
        bef = bef.substring(bit)+bef.substring(0,bit);

        return pre+bef;

    }

    //压缩置换,从56位密钥中选出48位作为当前加密的轮密钥
    public static String compressPermu(String s) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < compressPermu.length; i++) {
            for(int j = 0; j < compressPermu[i].length; j++) {
                sb.append(s.charAt(compressPermu[i][j]-1));
            }
        }

        return sb.toString();
    }

    //扩展变换,得到的是明文后32位经扩展后的48位
    public static String expanPermu(String s) {
        StringBuilder sb = new StringBuilder();
        //s = s.substring(s.length()/2);
        for(int i = 0; i < extendPermu.length; i++) {
            for(int j = 0; j < extendPermu[i].length; j++) {
                sb.append(s.charAt(extendPermu[i][j]-1));
            }
        }
        return sb.toString();
    }

    //两个字符串异或
    public static String getXor(String s,String k) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < s.length() && i < k.length(); i++) {
            if(s.charAt(i) == k.charAt(i))
                sb.append('0');
            else
                sb.append('1');
        }
        return sb.toString();
    }

    //S——盒替代
    public static String sBoxSub(String s) {

        String s1 = s.substring(0,6);   int a1 = getValue(s1,sBox1);
        String s2 = s.substring(6,12);  int a2 = getValue(s2,sBox2);
        String s3 = s.substring(12,18);  int a3 = getValue(s3,sBox3);
        String s4 = s.substring(18,24);  int a4 = getValue(s4,sBox4);
        String s5 = s.substring(24,30);  int a5 = getValue(s5,sBox5);
        String s6 = s.substring(30,36);  int a6 = getValue(s6,sBox6);
        String s7 = s.substring(36,42); int a7 = getValue(s7,sBox7);
        String s8 = s.substring(42,48); int a8 = getValue(s8,sBox8);

        return getBinary(a1)+getBinary(a2)+getBinary(a3)+getBinary(a4)+
                getBinary(a5)+getBinary(a6)+getBinary(a7)+getBinary(a8);
    }

    //将从sBox中得到的16进制数转换成二进制 
    public static String getBinary(int a) {
        String bin = Integer.toBinaryString(a);
        while(bin.length() < 4) {bin = "0" + bin;}

        return bin;
    }

    //根据六位二进制代码得到
    public static int getValue(String str,int[][] sBox) {
        String s1 = str.charAt(0)+""+str.charAt(str.length()-1); 
        int a1 = Integer.parseInt(s1,2);

        String s2 = str.substring(1,5); 
        int a2 = Integer.parseInt(s2,2);
        //得到sBox中的16进制数
        int value = sBox[a1][a2];

        return value;
    }

    //P——盒置换
    public static String pBoxPermu(String s) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < pBox.length; i++) {
            for(int j = 0; j < pBox[i].length; j++) {
                sb.append(s.charAt(pBox[i][j]-1));
            }
        }

        return sb.toString();
    }

    //末尾置换IP逆
    public static String inverseInitPermu(String s) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < endIPpermu.length; i++) {
            for(int j = 0; j < endIPpermu[i].length; j++) {
                sb.append(s.charAt(endIPpermu[i][j]-1));
            }
        }
        return sb.toString();
    }

    //中间加密过程
    public static String midEncry(String s,String k) {
        String str = "";

        String sLeft = s.substring(0,s.length()/2);  //s的左32位

        String sRight = s.substring(s.length()/2,s.length()); //s的右32位

        String sRigExtend = expanPermu(sRight);//s的右半部分扩展到48位

        String xor = "";
        xor = getXor(sRigExtend,k); //与k异或

        str = sBoxSub(xor);//结过8个s盒

        str = pBoxPermu(str); // 经过p盒置换

        str = getXor(sLeft,str); //与s的左半部分异或得到这轮输出的右半部分,s的右半部分作输出的左半部分

        return sRight + str;

    }

    //加密过程
    public static String encry(String plaintext,String key) {
        String ciphertext = "";
        String k = keyPermu(key);//置换,得到56位

        //1.明文经过IP置换后
        ciphertext = initPermu(plaintext);

        for(int times = 1; times < 17; times++) {

            k = keyMove(k,times);
            key = compressPermu(k);     
            //2.中间加密过程
            ciphertext = midEncry(ciphertext,key);

        }

        //3.末尾置换
        ciphertext = inverseInitPermu(ciphertext);

        return ciphertext;
    }

}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读