Kingdom of Saudi Arabia الـمـمـلـكـة الـعـربـيـة الـسـعـوديـة
Ministry of High Education وزارة الـتـعـلـيـم الـعـالـي
Northern Border University جـامـعـة الـحـدود الـشـمـالـيـة
Faculty of Computing and
كـلـيـة الـحـاسـبـات وتـقـنـيـة الـمـعـلـومـات
Information Technology
COURSE NAME: INFORMATION SECURITY
COURSE CODE: CPIT 425- CPCS 425-CPIS 312
TEACHER: Dr. Ben Slimane Jihane
ASSIGNMENT1- LAB
Second Term
Academic Year 1444/1445
STUDENT MARKS
10
STUDENT
Name: Hasna Salem
University ID:202100166
Classic Ciphers: Substitution Encryption with
Caesar Cipher
Page 1 of 4
1- Write the following program that allows the encryption using Caesar cipher.
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the plaintext: ");
String plaintext = scanner.nextLine();
System.out.print("Enter the key (shift value): ");
int key = scanner.nextInt();
scanner.close();
String ciphertext = encrypt(plaintext, key);
System.out.println("Ciphertext: " + ciphertext);
}
public static String encrypt(String plaintext, int key) {
StringBuilder ciphertext = new StringBuilder();
for (int i = 0; i < plaintext.length(); i++) {
char ch = plaintext.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isUpperCase(ch)) {
ch = (char) ((ch + key - 'A') % 26 + 'A');
} else {
ch = (char) ((ch + key - 'a') % 26 + 'a');
}
}
ciphertext.append(ch);
}
return ciphertext.toString();
}
}
2- Encrypt the plaintext 'Encryption' using the Caesar cipher with a key value of 3.
Page 2 of 4
Page 3 of 4
3- Modify the previous program to enable the decryption of the last ciphertext using the same key
value. Ensure that the program can correctly ensure the decryption using the Caesar cipher
algorithm.
Page 4 of 4