Simpledes is a simplified algorithm used to explain the DES algorithm.
Download algorithm text instructions from the front Link
Http://homepage.smc.edu/morgan_david/vpn/C-SDES.pdf
The algorithm comes from reference 1. For ease of learning, I added annotations.
Upload the files to facilitate viewing.
Figure 1 (Figure C.1 simplified des Scheme ):
Figure 2 (Figure C.2 key generation for simplified des ):
Figure 3 (Figure C.3 simplified DES encryption detail ):
The simpledestest. Java code is as follows:
Import Java. io. ioexception; import Java. util. arrays; public class simpledestest {// two keys K1 and k2static int k1 = 0; static int k2 = 0; /* Some parameters */static int P10 [] = new int [] {3, 5, 2, 7, 4, 10, 1, 9, 8, 6 }; static int P8 [] = new int [] {6, 3, 7, 4, 8, 5, 10, 9 }; static int P4 [] = new int [] {2, 4, 3, 1}; static int IP [] = new int [] {2, 6, 3, 1, 4, 8, 5, 7}; static int IPI [] = new int [] {4, 1, 3, 5, 7, 2, 8, 6 }; static int EP [] = new int [] {4, 1, 2, 3, 2, 3, 4, 1}; static int S0 [] [] = {1, 0, 3, 2}, {3, 2, 1, 0}, {0, 2, 1, 3}, {3, 1, 3, 2 },}; static int S1 [] [] = {0, 1, 2, 3}, {2, 0, 1, 3}, {3, 0, 1, 0 }, {2, 1, 0, 3},}; // exchange static int permute (INT num, int P [], int Pmax) {int result = 0; system. out. println ("START permute"); system. out. println ("Num:" + integer. tostring (Num, 2); system. out. println ("P:" + arrays. tostring (p); system. out. println ("Pmax:" + integer. tostring (Pmax, 2); For (INT I = 0; I <p. length; I ++) {result <= 1; // system. out. println ("" + I + ": Result <= 1," + // integer. tostring (result, 2) + "," + integer. tostring (P [I], 2); Result | = (Num> (Pmax-P [I]) & 1; // system. out. println ("result | = (Num> (Pmax-P [I]) & 1," + // integer. tostring (result, 2);} system. out. println ("Result:" + integer. tostring (result, 2); system. out. println ("End permute"); return result;} // generates K1, k2static void sdes (string key) {int K = integer. parseint (Key, 2); system. out. println ("START generate K1"); k = permute (K, P10, 10); system. out. println ("K:" + integer. tostring (K, 2); int th = 0, TL = 0; Th = (k> 5) & 0x1f; // obtain the key's 5-bit high TL = K & 0x1f; // obtain the key's 5-bit low system. out. println ("K top 5:" + integer. tostring (Th, 2); system. out. println ("K low 5:" + integer. tostring (TL, 2); // LS-1th = (th & 0xf) <1) | (th & 0x10)> 4 ); // shift one TL = (TL & 0xf) <1) | (TL & 0x10)> 4); // shift one system to the left of the loop. out. println ("K top 5 LS-1:" + integer. tostring (Th, 2); system. out. println ("K low 5 LS-1:" + integer. tostring (TL, 2); k1 = permute (th <5) | TL, p8, 10); // generate k1system. out. println ("K1:" + integer. tostring (K1, 2); system. out. println ("End generate K1"); system. out. println ("START generate K2"); // LS-2System.out.println ("K top 5:" + integer. tostring (Th, 2); system. out. println ("K low 5:" + integer. tostring (TL, 2); Th = (th & 0x07) <2) | (th & 0x18)> 3 ); // shift two places left in a loop TL = (TL & 0x07) <2) | (TL & 0x18)> 3); // shift two places left in a loop system. out. println ("K top 5 LS-2:" + integer. tostring (Th, 2); system. out. println ("K low 5 LS-2:" + integer. tostring (TL, 2); k2 = permute (th <5) | TL, p8, 10); // generate k2system. out. println ("K2:" + integer. tostring (K2, 2); system. out. println ("End generate K2");} // F function static int F (int r, int K) {system. out. println ("start f"); system. out. println ("R:" + integer. tostring (R, 2); system. out. println ("K:" + integer. tostring (K, 2); int T = permute (R, EP, 4) ^ K; system. out. println ("Permute (R, EP, 4) ^ K:" + integer. tostring (T, 2); int T0 = (T> 4) & 0xf; int T1 = T & 0xf; system. out. println ("f Top 4:" + integer. tostring (T0, 2); system. out. println ("f low 4:" + integer. tostring (T1, 2); int X1 = (T0 & 0x8)> 2) | (T0 & 1); int Y1 = (T0> 1) & 0x3; int X2 = (T1 & 0x8)> 2) | (T1 & 1); int y2 = (T1> 1) & 0x3; T0 = S0 [X1] [Y1]; T1 = S1 [X2] [y2]; system. out. println ("f Top 4 S0 [" + integer. tostring (x1, 2) + "] [" + integer. tostring (Y1, 2) + "]:" + integer. tostring (T0, 2); system. out. println ("f low 4 S1 [" + integer. tostring (X2, 2) + "] [" + integer. tostring (Y2, 2) + "]:" + integer. tostring (T1, 2); t = permute (T0 <2) | T1, P4, 4); system. out. println ("Permute (T0 <2) | T1, P4, 4):" + integer. tostring (T, 2); system. out. println ("end f"); Return t;} // FK function static int FK (INT input, int K) {system. out. println ("input:" + integer. tostring (input, 2); system. out. println ("K:" + integer. tostring (K, 2); int L = (input> 4) & 0xf; int r = input & 0xf; system. out. println ("FK Top 4:" + integer. tostring (L, 2); system. out. println ("FK low 4:" + integer. tostring (R, 2); Return (L ^ F (R, k) <4) | r;} // switch functionstatic int SW (int x) {return (X & 0xf) <4) | (x> 4) & 0xf) ;}// encrypt static string encrypt (string input) {int M = integer. parseint (input, 2); M = permute (M, IP, 8); system. out. println ("Permute (M, IP, 8) =" + integer. tostring (m, 2); M = FK (M, K1); system. out. println ("FK (M, K1) =" + integer. tostring (m, 2); M = Sw (m); system. out. println ("SW (m) =" + integer. tostring (m, 2); M = FK (M, K2); system. out. println ("FK (M, K2) =" + integer. tostring (m, 2); M = permute (M, IPI, 8); system. out. println ("Permute (M, IPI, 8) =" + integer. tostring (m, 2); Return integer. tostring (m, 2);} // decrypt static string decrypt (string input) {int M = integer. parseint (input, 2); system. out. println ("start ip"); M = permute (M, IP, 8); system. out. println ("Permute (M, IP, 8) =" + integer. tostring (m, 2); system. out. println ("end IP"); system. out. println ("start fk K2"); M = FK (M, K2); system. out. println ("FK (M, K2) =" + integer. tostring (m, 2); system. out. println ("End FK K2"); system. out. println ("start sw"); M = Sw (m); system. out. println ("SW (m) =" + integer. tostring (m, 2); system. out. println ("End SW"); system. out. println ("start fk K1"); M = FK (M, K1); system. out. println ("FK (M, K1) =" + integer. tostring (m, 2); system. out. println ("End FK K1"); system. out. println ("start iip"); M = permute (M, IPI, 8); system. out. println ("Permute (M, IPI, 8) =" + integer. tostring (m, 2); system. out. println ("End IIP"); system. out. println ("End decrypt"); Return integer. tostring (m, 2);} public static void main (string [] ARGs) throws ioexception {string plaintext, ciphertext, key; Java. util. repeated scan = new Java. util. using (system. in); string command = "1: encryption (encrypt), 2: decryption (decrypt), 3: exit (exit), enter a number to select :"; string inputplaintext = "Enter plaintext (8 bit plaintext):"; string inputciphertext = "Enter ciphertext (8 bit ciphertext ):"; string inputkey = "Enter the key (10 bit key):"; system. out. println (command); int mode = scan. nextint (); While (true) {If (mode = 1) {system. out. println (inputplaintext); plaintext = scan. next (); system. out. println (inputkey); Key = scan. next (); sdes (key); ciphertext = encrypt (plaintext); // If the ciphertext is less than 8 characters, supplement the eight-digit if (ciphertext. length () <8) {for (INT I = 0; I <8-ciphertext. length (); I ++) ciphertext = "0" + ciphertext;} system. out. println ("the encrypted ciphertext (8 bit ciphertext) is:" + ciphertext);} else if (mode = 2) {system. out. println (inputciphertext); ciphertext = scan. next (); system. out. println (inputkey); Key = scan. next (); sdes (key); plaintext = decrypt (ciphertext); If (plaintext. length () <8) {for (INT I = 0; I <8-plaintext. length (); I ++) plaintext = "0" + plaintext;} system. out. println ("the decrypted plaintext (8 bit plaintext) is:" + plaintext);} else if (mode = 3) break; system. out. println (command); mode = scan. nextint ();}}}
The decryption process for ciphertext 10100010 and key 0111111101 is as follows:
1: encryption (encrypt), 2: decryption (decrypt), 3: exit (exit), enter a number to select: 2 enter the ciphertext (8 bit ciphertext ): 10100010 enter the key (10 bit key): 0111111101 start generate k1start permutenum: 111111101 P: [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] Pmax: 1010 result: 1111110011end permutek: 1111110011 K top 5: 11111 K low 5: 10011 K top 5 LS-1: 11111 K low 5 LS-1: 111 start permutenum: 1111100111 P: [6, 3, 7, 4, 8, 5, 10, 9] Pmax: 1010 result: 1011111end permutek1: 1011111end generate k1start generate k2k top 5: 11111 K low 5: 111 K top 5 LS-2: 11111 K low 5 LS-2: 11100 start permutenum: 1111111100 P: [6, 3, 7, 4, 8, 5, 10, 9] Pmax: 1010 result: 11111100end permutek2: 11111100end generate k2start ipstart permutenum: 10100010 P: [2, 6, 3, 1, 4, 8, 5, 7] Pmax: 1000 result: 110001end permutepermute (M, IP, 8) = 110001end ipstart FK k2input: 110001 K: 11111100fk Top 4: 11fk low 4: 1 start fr: 1 K: 11111100 start permutenum: 1 P: [4, 1, 2, 3, 2, 3, 4, 1] Pmax: 100 result: 10000010end permutepermute (R, EP, 4) ^ K: 1111110f Top 4: 111f low 4: 1110f Top 4 S0 [1] [11]: 0f low 4 S1 [10] [11]: 0 start permutenum: 0 p: [2, 4, 3, 1] Pmax: 100 result: 0end permutepermute (T0 <2) | T1, P4, 4): 0end ffk (M, K2) = 110001end FK k2start swsw (m) = 10011end swstart FK k1input: 10011 K: 1011111fk Top 4: 1fk low 4: 11 start fr: 11 K: 1011111 start permutenum: 11 P: [4, 1, 2, 3, 2, 3, 4, 1] Pmax: 100 result: 10010110end permutepermute (R, EP, 4) ^ K: 11001001f Top 4: 1100f low 4: 1001f Top 4 S0 [10] [10]: 1f low 4 S1 [11] [0]: 10 start permutenum: 110 P: [2, 4, 3, 1] Pmax: 100 result: 1010end permutepermute (T0 <2) | T1, P4, 4): 1010end ffk (M, K1) = 10110011end FK k1start iipstart permutenum: 10110011 P: [4, 1, 3, 5, 7, 2, 8, 6] Pmax: 1000 result: 11101010end permutepermute (M, IPI, 8) = 11101010end iipend decrypt the decrypted plaintext (8-bit plaintext) is: 111010101: Encrypted (encrypt), 2: decrypted (decrypt), 3: Exited (exit ), enter a number to select:
The encryption process for plaintext 11101010 and key 011111101 is as follows:
1: encryption (encrypt), 2: decryption (decrypt), 3: exit (exit), enter a number to select: 1, enter the plaintext (8 bit plaintext ): 11101010 enter the key (10 bit key): 0111111101 start generate k1start permutenum: 111111101 P: [3, 5, 2, 7, 4, 10, 1, 9, 8, 6] Pmax: 1010 result: 1111110011end permutek: 1111110011 K top 5: 11111 K low 5: 10011 K top 5 LS-1: 11111 K low 5 LS-1: 111 start permutenum: 1111100111 P: [6, 3, 7, 4, 8, 5, 10, 9] Pmax: 1010 result: 1011111end permutek1: 1011111end generate k1start generate k2k top 5: 11111 K low 5: 111 K top 5 LS-2: 11111 K low 5 LS-2: 11100 start permutenum: 1111111100 P: [6, 3, 7, 4, 8, 5, 10, 9] Pmax: 1010 result: 11111100end permutek2: 11111100end generate k2start permutenum: 11101010 P: [2, 6, 3, 1, 4, 8, 5, 7] Pmax: 1000 result: 10110011end permutepermute (M, IP, 8) = 10110011 input: 10110011 K: 1011111fk Top 4: 1011fk low 4: 11 start fr: 11 K: 1011111 start permutenum: 11 P: [4, 1, 2, 3, 2, 3, 4, 1] Pmax: 100 result: 10010110end permutepermute (R, EP, 4) ^ K: 11001001f Top 4: 1100f low 4: 1001f Top 4 S0 [10] [10]: 1f low 4 S1 [11] [0]: 10 start permutenum: 110 P: [2, 4, 3, 1] Pmax: 100 result: 1010end permutepermute (T0 <2) | T1, P4, 4): 1010end ffk (M, K1) = 10011sw (m) = 110001 input: 110001 K: 11111100fk Top 4: 11fk low 4: 1 start fr: 1 K: 11111100 start permutenum: 1 P: [4, 1, 2, 3, 2, 3, 4, 1] Pmax: 100 result: 10000010end permutepermute (R, EP, 4) ^ K: 1111110f Top 4: 111f low 4: 1110f Top 4 S0 [1] [11]: 0f low 4 S1 [10] [11]: 0 start permutenum: 0 p: [2, 4, 3, 1] Pmax: 100 result: 0end permutepermute (T0 <2) | T1, P4, 4): 0end ffk (M, K2) = 110001 start permutenum: 110001 P: [4, 1, 3, 5, 7, 2, 8, 6] Pmax: 1000 result: 10100010end permutepermute (M, IPI, 8) = 10100010 encrypted ciphertext (8 bit ciphertext): 101000101: encryption (encrypt), 2: decryption (decrypt), 3: exit. Enter a number to select:
References
1. Java implementation of simple-DES algorithm. http://blog.csdn.net/jason314/article/details/5425248
2. cryptography and network security principles and practice. Appendix C simplified des. http://homepage.smc.edu/morgan_david/vpn/C-SDES.pdf