import java.util.*;

public class SDESKeyGeneration {
    // P10 and P8 permutations as constants
    private static final int[] P10 = {3, 5, 2, 7, 4, 10, 1, 9, 8, 6};
    private static final int[] P8 = {6, 3, 7, 4, 8, 5, 10, 9};

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input: Prompt for a 10-bit binary key
        System.out.print("Enter a 10-bit binary key: ");
        String key = scanner.nextLine();

        // Validate the input
        if (key.length() != 10 || !key.matches("[01]+")) {
            System.out.println("Invalid input! Please enter a valid 10-bit binary string.");
            return;
        }

        // Generate and display the keys
        String[] keys = generateKeys(key);
        System.out.println("Generated Key K1: " + keys[0]);
        System.out.println("Generated Key K2: " + keys[1]);
    }

    // Method to generate keys K1 and K2
    private static String[] generateKeys(String key) {
        // Apply P10 permutation
        String permutedKey = permute(key, P10);

        // Split into left and right halves
        String leftHalf = permutedKey.substring(0, 5);
        String rightHalf = permutedKey.substring(5);

        // Perform 1-bit left shifts
        leftHalf = leftShift(leftHalf);
        rightHalf = leftShift(rightHalf);

        // Generate K1 using P8
        String K1 = permute(leftHalf + rightHalf, P8);

        // Perform 2 additional left shifts
        leftHalf = leftShift(leftHalf);
        rightHalf = leftShift(rightHalf);
        leftHalf = leftShift(leftHalf);
        rightHalf = leftShift(rightHalf);

        // Generate K2 using P8
        String K2 = permute(leftHalf + rightHalf, P8);

        // Return the generated keys
        return new String[]{K1, K2};
    }

    // Method to permute a key using a given permutation array
    private static String permute(String key, int[] permutation) {
        char[] permutedKey = new char[permutation.length];
        for (int i = 0; i < permutation.length; i++) {
            permutedKey[i] = key.charAt(permutation[i] - 1);
        }
        return new String(permutedKey);
    }

    // Method to perform a 1-bit left shift
    private static String leftShift(String half) {
        return half.substring(1) + half.charAt(0);
    }
}