ISC 2026 Practical Questions Solved

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

Algorithm:
1. Input:
• Step 1: Receive an integer num (the number to encrypt).
• Step 2: Receive an integer key.
2. Key Validation:
• Step 3: Calculate the length of the number (ln) by converting
it to a string and getting its length.
• Step 4: Calculate the length of the key (lk).
• Step 5: Check if ln is equal to lk.
• If ln != lk: Print "INVALID KEY SIZE" and exit.
3. Key Digit Validation:
• Step 6: Iterate through each digit of the key.
• For each digit d in key:
• Step 7: Check if d appears earlier in the key.
• If key.substring(0, i).indexOf(d) >= 0 (meaning
d appears before the current index):
• Print "REPEATED KEYS" and exit.
• Step 8: Check if d is within the valid range of 1 to
ln.
• If d < 1 || d > ln: Print "INVALID KEY DIGITS"
and exit.
4. Digit Shifting (Encryption):
• Step 9: Convert the number num into a string n.
• Step 10: Create a character array digits with a size equal to
ln (the length of the number).
• Step 11: Iterate through each digit of the key.
• For each digit d in key:
• Step 12: Get the integer value of d.
• Step 13: Find the position (index + 1) of the digit
d within the string n.
• Step 14: Set the character at the position pos - 1
in the digits array to the character at the position
i in the string n.
5. Output:
• Step 15: Create an empty string enc.
• Step 16: Iterate through the digits array.
• For each character c in the digits array:
• Step 17: Append the character c to the string enc.
• Step 18: Convert the string enc into an integer.
• Step 19: Print the encrypted integer. 
Code
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);
 }
}
Scroll to Top