Digit Permutation Cipher | ISC Computer Science Sample Paper 2026

A Digit Permutation Cipher is a simple form of number encryption where the digits of a number are rearranged based on a given key where (1 ≤ key ≤ size of the number). The key is a sequence of integers that defines the new positions of the digits.

If number = 2613 and key = 4213, then the encrypted number will be 1632 by positioning the first digit to the 4th place, second digit to the 2nd place, third digit to the 1st place and the fourth digit to the 3rd place as per the key given.

Write a program to enter a number and a permutation key (a sequence of digits which is greater than 0 and less than or equal to the size of the number). The program should encrypt the number by permuting its digits according to the key. The number of digits in the key must match the number of digits in the number to be encrypted.

Test your program with the following data and some random data:

Example 1:
INPUT:
Number: 12345
Key: 31524
OUTPUT: The encrypted number is 24153

Example 2:
INPUT:
Number: 9876
Key: 4132
OUTPUT: The encrypted number is 8679

Example 3:
INPUT:
Number: 5239
Key: 4765
OUTPUT: INVALID KEY DIGITS

Example 4:
INPUT:
Number: 123
Key: 2134
OUTPUT: INVALID KEY SIZE

import java.util.Scanner;
class DigitCipher{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Number: ");
        int num = in.nextInt();
        System.out.print("Key: ");
        int key = in.nextInt();
        int ln = String.valueOf(num).length();
        int lk = String.valueOf(key).length();
        if(ln != lk){
            System.out.println("INVALID KEY SIZE");
            return;
        }
        String k = String.valueOf(key);
        for(int i = 0; i < k.length(); i++){
            String d = k.substring(i, i + 1);
            if(k.substring(0, i).indexOf(d) >= 0){
                System.out.println("REPEATED KEYS");
                return;
            }
            int digit = Integer.parseInt(d);
            if(digit < 1 || digit > ln){
                System.out.println("INVALID KEY DIGITS");
                return;
            }
        }
        char digits[] = new char[ln];
        String n = String.valueOf(num);
        for(int i = 0; i < digits.length; i++){
            int pos = Integer.parseInt(k.substring(i, i + 1));
            digits[pos - 1] = n.charAt(i);
        }
        String enc = "";
        for(int i = 0; i < digits.length; i++)
            enc += digits[i];
        int encrypted = Integer.parseInt(enc);
        System.out.println("The encrypted number is " + encrypted);
    }
}