import java.util.Scanner; public class SDES { static int[] P10 = {3, 5, 2, 7, 4, 10, 1, 9, 8, 6}; static int[] P8 = {6, 3, 7, 4, 8, 5, 10, 9}; static int[] IP = {2, 6, 3, 1, 4, 8, 5, 7}; static int[] IP_INV = {4, 1, 3, 5, 7, 2, 8, 6}; static int[] EP = {4, 1, 2, 3, 2, 3, 4, 1}; static int[] P4 = {2, 4, 3, 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} }; static String K1, K2; static String permute(String input, int[] table) { StringBuilder result = new StringBuilder(); for (int position : table) { result.append(input.charAt(position - 1)); } return result.toString(); } static String leftShift(String input, int shifts) { shifts %= input.length(); return input.substring(shifts) + input.substring(0, shifts); } static String xor(String a, String b) { StringBuilder result = new StringBuilder(); for (int i = 0; i < a.length(); i++) { result.append(a.charAt(i) == b.charAt(i) ? '0' : '1'); } return result.toString(); } static void generateKeys(String key) { String p10 = permute(key, P10); String left = p10.substring(0, 5); String right = p10.substring(5); left = leftShift(left, 1); right = leftShift(right, 1); K1 = permute(left + right, P8); left = leftShift(left, 2); right = leftShift(right, 2); K2 = permute(left + right, P8); } static String sBox(String input, int[][] box) { int row = Integer.parseInt( "" + input.charAt(0) + input.charAt(3), 2 ); int col = Integer.parseInt( "" + input.charAt(1) + input.charAt(2), 2 ); int value = box[row][col]; return String.format("%2s", Integer.toBinaryString(value)).replace(' ', '0'); } static String functionF(String right, String key) { String expanded = permute(right, EP); String xorResult = xor(expanded, key); String left4 = xorResult.substring(0, 4); String right4 = xorResult.substring(4); String s0Result = sBox(left4, S0); String s1Result = sBox(right4, S1); return permute(s0Result + s1Result, P4); } static String fk(String bits, String key) { String left = bits.substring(0, 4); String right = bits.substring(4); String fResult = functionF(right, key); String newLeft = xor(left, fResult); return newLeft + right; } static String switchHalves(String bits) { return bits.substring(4) + bits.substring(0, 4); } static String encrypt(String plaintext) { String temp = permute(plaintext, IP); temp = fk(temp, K1); temp = switchHalves(temp); temp = fk(temp, K2); return permute(temp, IP_INV); } static String decrypt(String ciphertext) { String temp = permute(ciphertext, IP); temp = fk(temp, K2); temp = switchHalves(temp); temp = fk(temp, K1); return permute(temp, IP_INV); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter 10-bit key: "); String key = sc.nextLine(); System.out.print("Enter 8-bit plaintext: "); String plaintext = sc.nextLine(); if (!key.matches("[01]{10}") || !plaintext.matches("[01]{8}")) { System.out.println("Invalid input."); return; } generateKeys(key); System.out.println("\nK1 = " + K1); System.out.println("K2 = " + K2); String ciphertext = encrypt(plaintext); System.out.println("Plaintext = " + plaintext); System.out.println("Ciphertext = " + ciphertext); String decrypted = decrypt(ciphertext); System.out.println("Decrypted = " + decrypted); sc.close(); } }