0% found this document useful (0 votes)
6 views

Ceaser_Cipher_Java_Cryptanalysis

Chapter 5 introduces classical cryptography, focusing on the Caesar cipher, a substitution cipher used since the Roman Empire. The chapter explains how to encrypt and decrypt messages using a key that shifts letters in the alphabet, providing examples and a Java implementation of the cipher. It emphasizes the importance of keeping the key secret for secure communication.

Uploaded by

synragamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Ceaser_Cipher_Java_Cryptanalysis

Chapter 5 introduces classical cryptography, focusing on the Caesar cipher, a substitution cipher used since the Roman Empire. The chapter explains how to encrypt and decrypt messages using a key that shifts letters in the alphabet, providing examples and a Java implementation of the cipher. It emphasizes the importance of keeping the key secret for secure communication.

Uploaded by

synragamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 5 CLASSICAL CRYPTOGRAPHY

introduction to cryptography, especially for beginners in this field. You can find more
background information about the ciphers in this chapter in many cryptography courses
and books, such as [1], [2], and [3].

Caesar Cipher
The Caesar cipher is estimated to date back to the first century BC in the Roman
Empire. Its name comes from the Roman Emperor Julius Caesar, who used this cipher
to encrypt military messages and strategies [1], and it is estimated to be broken in about
the fifth century AD. The Caesar cipher is a substitution cipher, in which each letter of
the alphabet is moved a certain number of characters to the right. For example, if the
established number is 5, then A will become F, B will become G, C will become H, etc.
In this cipher, the key is represented by the number that the letters are shifted. Note that
the key should be secret, known only by the sender (to encrypt the messages) and by the
receiver (to decrypt the messages). The Caesar cipher is important because it is the basis
for other classical ciphers, such as Vigenère. The Caesar cipher with key k = 13 is also
known as Rot13.
Let’s consider an example. The key for the cipher is set to k = 6; therefore, all letters
are shifted six characters (see Table 5-1).

Table 5-1. Caesar Representation with k = 6


0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
G H I J K L M N O P Q R S T U V W X Y Z A B C D E F

Note that there are 26 letters in the alphabet, and in Table 5-1 the numbering starts
with 0 and ends with 25 because it is related to 26 = {0, 1, …, 25}. Sometimes, in other
documentation, the numbering starts with 1 and ends with 26, because it seems more
natural.
Considering the plain message as “CAESAR CIPHER EXAMPLE” and the key as k = 6,
the encrypted message will be “IGKYGX IOVNKX KDGSVRK.” The encrypted message
was obtained by replacing C with the corresponding I, A with the corresponding G, E
with the corresponding K, and so on.

48
CHAPTER 5 CLASSICAL CRYPTOGRAPHY

To decrypt the message, the letters from the second row of the table are shifted
to the left for k positions. Therefore, the decryption of “IGKYGX IOVNKX KDGSVRK”
(with the key k = 6) is “CAESAR CIPHER EXAMPLE,” obtained by applying a similar
logic as for encryption: I is replaced with C, G is replaced with A, and so on. Note that
sometimes the space character is removed from the message; therefore, in the example,
the plain text would be “CAESARCIPHEREXAMPLE,” while the encrypted text would be
“IGKYGXIOVNKXKDGSVRK.”
From a mathematical point of view, the operations are addition for encryption and
subtraction for decryption via a modulo operation. The first step is to “convert” the
characters to numbers, as follows: A → 1, B → 2, …, Z → 26. The encryption function
enck will take as input one parameter, i.e., the number corresponding to the letter that
needs to be encrypted at the current step, and will output the encrypted letter:

enc k ! c " # ! c $ k " mod 26,

where c is the plain character and k is the key. The decryption function needs to shift
back the letters and, therefore, has the following representation:

dec k " c ! # $ " c ! % k # mod 26,

where c is the encrypted character that needs to be decrypted and k is the key.

Implementation
Listing 5-1 shows the implementation of the Caesar cipher; Figure 5-1 shows the result.

Listing 5-1. Implementation of the Caesar Cipher


1 import java.util.Scanner;
2
3 public class CaesarCipher {
4
5 public static String Encrypt(String text, int positions) {
6 String toEncrypt = "", result = "";
7 //format the text to be encrypted
8 for (int i = 0; i < text.length(); i++) {
9 //remove space characters

49
CHAPTER 5 CLASSICAL CRYPTOGRAPHY

10 if (text.charAt(i) == ' ')


11 continue;
12 else {
13 //if the character is lowercase, make it
uppercase
14 if (Character.isLowerCase(text.charAt(i)))
15 toEncrypt += Character.toUpperCase
(text.charAt(i));
16 //otherwise keep the uppercase character
17 else
18 toEncrypt += text.charAt(i);
19 }
20 }
21 for (int i = 0; i < toEncrypt.length(); i++) {
22 //shift the current letter of the message with given
positions to right
23 char shiftedLetter = (char) (toEncrypt.charAt(i) +
positions);
24
25 //if the ASCII code exceeds Z, then bring it back in
the interval A..Z
26 if (shiftedLetter > 'Z')
27 shiftedLetter = (char) (shiftedLetter +
'A' - 'Z' - 1);
28
29 result += shiftedLetter;
30 }
31
32 return result;
33 }
34
35 public static String Decrypt(String text, int positions) {
36 //the encrypted code is already uppercase,
37 //therefore there is no need of formatting
38 String result = "";

50
CHAPTER 5 CLASSICAL CRYPTOGRAPHY

39 for (int i = 0; i < text.length(); i++) {


40
41 //shift the current letter of the message with given
positions to left
42 char shiftedLetter = (char) (text.charAt(i) -
positions);
43
44 //if the ASCII code exceeds A, then bring it back in
the interval A..Z
45 if (shiftedLetter < 'A')
46 shiftedLetter = (char) (shiftedLetter -
'A' + 'Z' + 1);
47
48 result += shiftedLetter;
49
50 }
51
52 return result;
53 }
54
55 public static void main(String[] args) {
56 System.out.println("CAESAR CIPHER\n");
57
58 Scanner sc = new Scanner(System.in);
59
60 // Reading the input: plain message and the secret key
61 System.out.print("Type the message: ");
62 String message = sc.nextLine();
63 System.out.print("Type the key: ");
64 int key = sc.nextInt();
65 sc.close();
66
67 // Encrypting the plain message
68 System.out.println("\nEncrypting...");
69 String encryptedMessage = Encrypt(message, key);

51
CHAPTER 5 CLASSICAL CRYPTOGRAPHY

70 System.out.println("The encrypted message is: " +


encryptedMessage);
71
72 // Decrypting the encrypted message
73 System.out.println("\nDecrypting...");
74 String recoveredMessage = Decrypt(encryptedMessage, key);
75 System.out.println("The decrypted message is: " +
recoveredMessage);
76
77 }
78 }

The code is pretty simple and just follows the encryption and decryption formula.
Before encrypting the message, it is formatted by eliminating the space characters and
making all the letters uppercase.

Figure 5-1. The result of Caesar enciphering

52

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy