The DES algorithm Java simulation realizes the __ algorithm

Source: Internet
Author: User

Sophomore next semester Applied Cryptography Course Design synchronized GitHub project: Url:https://github.com/ericli404/java-demos/tree/master/des-demo Git:https://github.com/ericli404/java-demos.git

Implementation of the DES algorithm, the specific output of each step are recorded, did not join the working mode, supporting the encryption input for the English and the number of mixed strings, fill the way to use the infinite padding 0 of the practice. Below is the source code.

Main class, contains some debugging information, various output package cryptology;

Import Java.util.Scanner;
 /** * Created by Leif on 2016/3/26 0026. */public class des {public static void main (string[] args) {System.out.println ("***************************
        *******************************");
        System.out.println ("Des plus decryption step-by-step demo");
        System.out.println ("**********************************************************");
        System.out.println ();
        @SuppressWarnings ("resource") Scanner in = new Scanner (system.in);
        System.out.print ("Please enter clear text (supporting alphanumeric combinations of any length):");
        String mmmmmm = In.nextline ();
        System.out.print ("Please enter the key (support arbitrary format input)");
        String kkkkkk = In.nextline ();
        string[] M = util.all2string (mmmmmm);
        char[] K = Util.keypre (KKKKKK);
        Descore Descore = new Descore ();
        String mimi = Descore.en (M, K);
        String Demimi = descore.de (Util.all222string (Mimi), K); String demimistring = util.bYte2string (Demimi);
        System.out.println ("*************************************************");
        System.out.println ("********************** result ***********************");
        System.out.println ("plaintext:" +mmmmmm);
        System.out.println ("------------------------------------------------------------");
        SYSTEM.OUT.PRINTLN ("Encrypted result:");
        System.out.println ("binary:" + Mimi);
        System.out.println ("Hex:" +util.two2hex (Mimi));
        System.out.println ("------------------------------------------------------------");
        System.out.println ("Decryption result:");
        System.out.println ("binary:" +demimi);
        System.out.println ("Hex:" +util.two2hex (Demimi));




    SYSTEM.OUT.PRINTLN ("Encoded after:" +demimistring); }


}
Core algorithm class package cryptology;
 /** * Created by Leif on 2016/3/26 0026.  * * public class Descore {/** * initial switch * @param M char[64] * @return char[56] * * Private char[]
        Initreplace (char[] M) {char[] TM = new char[64]; for (int i = 0; i < i++) {Tm[i] = M[data.
        IP[I]-1];
    } return TM; /** * Key Replacement 1 * @param k char[64] * @return char[56] * * Private char[] KeyReplace1 (char[) k)
        {char[] TK = new char[64]; for (int i = 0; i < i++) {Tk[i] = K[data.
        PC_1[I]-1];
    } return TK;  /** * Generate Sub Key * @param K char[56] * @param offset left shift number int * @return CHAR[56] * * Private
        Char[] Loopkey (char[] K, int offset) {String K1 = new String ();
        String K2 = new string ();
        String k = new String (k);
        K1 = (k.substring (0,28) +k.substring (0,2)). substring (offset,offset+28); K2 = (K.substriNg (28,56) +k.substring (28,30)). substring (offset,offset+28);
    Return (K1+K2). ToCharArray (); /** * Key Replacement 2 * @param k char[56] * @return char[48] * * Private char[] KeyReplace2 (char[) k)
        {char[] TK = new char[48]; for (int i = 0; i < i++) {Tk[i] = K[data.
        PC_2[I]-1];
    } return TK;
        /** * Generate 16 Wheel key * @param k char[64] * @return/private char[][] Getkey (char[) k) {
        char[][] TK = new char[16][48];
        char[][] TtK = new char[16][48];
        char[] K1 = new char[56];
        K1 = KeyReplace1 (K); Tk[0] = Loopkey (k1,data.
        Leftmove[0]); for (int i = 1; i < i++) {Tk[i] = Loopkey (tk[i-1],data.
        Leftmove[i]);
        for (int i = 0; i < i++) {Ttk[i] = KeyReplace2 (tk[i));
    return TtK; /** * E table permutation (extension substitution) * @param R char[32] * @return char[48] * * Private CHAr[] Ereplace (char[] R) {char[] TR = new char[48]; for (int i = 0;i < i++) {Tr[i] = R[data.
        E[I]-1];
    } return TR; /** * XOR exclusive OR operation * @param tR char[48] * @param K char[48] Wheel Key * @return * * Private char[
        ] XOR (char[] TR, char[] K) {string x = new String ();
        for (int i = 0; i < tr.length i++) {x + + tr[i] ^ k[i];
    return X.tochararray (); /** * s Box replacement * @param xorr char[48] * @return CHAR[32] * * Private char[] Sreplace (char[) XO
        RR) {char[][] TR = new CHAR[8][6];/* String nn = new string (R);
        System.out.println (Nn+nn.length ()); */String sr = new string ();
            for (int i = 0; i < 8; i++) {for (int j = 0; J < 6; J +) {Tr[i][j] = xorr[i*6+j];
            int p = util.two2ten (string.valueof (tr[i][0)) +string.valueof (tr[i][5)); int Q= Util.two2ten (string.valueof (tr[i][1)) +string.valueof (tr[i][2]) +string.valueof (tr[i][3]) +String.valueOf (tR[i][
             4])); SR + + String.Format ("%04d", Integer.parseint (data.
S_BOX[I][P][Q])); SR + + String.Format ("%04d", Integer.parseint (data. s_box[i][(Tr[i][0] << 1) + tr[i][5]][(tr[i][1) << 3) + (Tr[i][2] << 2) + (Tr[i][3] << 1) + tr[i][4
        ]])));
    return Sr.tochararray ();
        /** * P Table replacement * @param sR char[32] * @return CHAR[32] * * Private char[] Preplace (char[] sR) {
        char[] PR = new CHAR[32]; for (int i = 0; i < i++) {Pr[i] = Sr[data.
        P[I]-1];
    } return PR; /** * End permutation * @param r Char[64] * @return char[64] * * Private char[] Init_1replace (char[) r
        {char[] TR = new char[64]; for (int i = 0; i < i++) {Tr[i] = R[data.
        IP_1[I]-1];
 }       return TR;  /** * Wheel Replacement * @param R char[32] * @param K char[32] * @return CHAR[32] * * Public char[]
        Core (char[] R, char[] K) {char[] ER = Ereplace (R);
        System.out.println ("etable permutation result---" +new string (er) + "-----" +util.two2hex (New String (er));
        System.out.println ("Wheel Key---" +new string (k) + "-----" +util.two2hex (new String (k));
        char[] Xorr = XOR (er,k);
        System.out.println ("xor-1 Result---" +new string (Xorr) + "-----" +util.two2hex (new String (Xorr));
        char[] SR = Sreplace (Xorr);
        System.out.println ("Sbox permutation result---" +new string (SR) + "-----" +util.two2hex (New String (SR));
        char[] PR = Preplace (SR);
        System.out.println ("ptable permutation result---" +new string (pr) + "-----" +util.two2hex (new String (PR));
        @SuppressWarnings ("unused") string pp = new string (PR);
    return PR;
        Public String EnCode (char[] M, char[] K) {System.out.println ("#########################"); System.ouT.println ("===== = = = = = = = Start Encryption");
        System.out.println ("bit stream is---" +new string (m) + "-----" +util.two2hex (new String (m));
        System.out.println ("Key is---" +new string (k) + "-----" +util.two2hex (new String (k));
        char[][] L = new CHAR[17][32];
        char[][] R = new CHAR[17][32];
        char[][] key = new CHAR[16][48];
        char[] res = new CHAR[64];
        Char[] r = new CHAR[64];
        m = Initreplace (m);
        System.out.println ("IP permutation result---" +new string (m) + "-----" +util.two2hex (new String (m));
        Key = Getkey (K);
            for (int i = 0; i < i++) {L[0][i] = M[i];
        R[0][i] = m[i+32]; for (int i = 1; i < i++) {System.out.println ("===================" +i+ "round ==================
            ");
            char[] Xorr = XOR (L[i-1],core (r[i-1],key[i-1));
            System.out.println ("xor-2 result" +new string (Xorr) + "-----" +util.two2hex (new String (Xorr));
         for (int j = 0; J < j) {       L[I][J] = R[i-1][j];
            R[I][J] = Xorr[j];
            for (int i = 0; i < i++) {Res[i] = R[16][i];
        RES[I+32] = L[16][i];
        } r = Init_1replace (res);
        String rr = new string (r);
        SYSTEM.OUT.PRINTLN (end permutation result---+new string (RR) + "-----" +util.two2hex (new String (RR));
    return RR;
        Public String DeCode (char[] M, char[] K) {System.out.println ("#########################");
        System.out.println ("===== = = = = = = = Start decryption");
        System.out.println ("bit stream is---" +new string (m) + "-----" +util.two2hex (new String (m));
        System.out.println ("Key is---" +new string (k) + "-----" +util.two2hex (new String (k));
        char[][] L = new CHAR[17][32];
        char[][] R = new CHAR[17][32];
        char[][] key = new CHAR[16][48];
        char[] res = new CHAR[64];
        Char[] r = new CHAR[64];
        m = Initreplace (m); System.out.println ("IP permutation result---" +new String (M) +"-----" +util.two2hex (new String (M));
        Key = Getkey (K);
            for (int i = 0; i < i++) {L[0][i] = M[i];
        R[0][i] = m[i+32]; for (int i = 1; i < i++) {System.out.println ("===================" +i+ "Wheel =================="
            );
            char[] Xorr = XOR (L[i-1],core (r[i-1],key[16-i));
            System.out.println ("xor-2 result" +new string (Xorr) + "-----" +util.two2hex (new String (Xorr));
                for (int j = 0; J < + j) {L[i][j] = r[i-1][j];
            R[I][J] = Xorr[j];
            for (int i = 0; i < i++) {Res[i] = R[16][i];
        RES[I+32] = L[16][i];
        } r = Init_1replace (res);
        String rr = new string (r);
    return RR;
        } public String en (string[] mm, char[] kk) {String mi = "";
            for (int i = 0; i < mm.length i++) {char[] charrr = Mm[i].tochararray (); Mi + + enCode (cHarrr, KK);
    } return mi;
        Public String de (string[] mm, char[] kk) {String mi = "";
            for (int i = 0; i < mm.length i++) {char[] charrr = Mm[i].tochararray ();
        Mi + + deCode (charrr, KK);
    } return mi; }
}
//des algorithm replacement process need to use some permutation table package cryptology;
 /** * Created by Leif on 2016/3/26 0026.

    */public class Data {//Declare constant byte array static int length = 0;
    public static int GetLength () {return length;
    public static void SetLength (int length) {data.length = length;  /** * Initial replacement/static final int[] IP = {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, 9, 1, all, Wuyi, 3,----------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.