How to resolve the algorithm Caesar cipher step by step in the Java programming language
How to resolve the algorithm Caesar cipher step by step in the Java programming language
Table of Contents
Problem Statement
Implement a Caesar cipher, both encoding and decoding. The key is an integer from 1 to 25. This cipher rotates (either towards left or right) the letters of the alphabet (A to Z). The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A). So key 2 encrypts "HI" to "JK", but key 20 encrypts "HI" to "BC". This simple "mono-alphabetic substitution cipher" provides almost no security, because an attacker who has the encoded message can either use frequency analysis to guess the key, or just try all 25 keys. Caesar cipher is identical to Vigenère cipher with a key of length 1. Also, Rot-13 is identical to Caesar cipher with key 13.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Caesar cipher step by step in the Java programming language
Overview:
The Java program defines a utility class called Cipher
for performing character substitution encoding and decoding. It demonstrates how to encode and decode a given string using a shift cipher technique.
Class Definition:
- The
Cipher
class is declared as public with amain
method to execute the program.
Encoding Method (encode
):
- The
encode
method takes two parameters:enc
: The input string to be encoded.offset
: The character offset used for encoding.
- It computes the adjusted offset by taking the modulus of
offset
by 26 and adding 26, ensuring it stays within the range of 0 to 25. - It creates a
StringBuilder
calledencoded
to construct the encoded string. - The method iterates through each character (
i
) in the input string. - For each character, it checks if it's a letter. If it is, it further checks if it's uppercase or lowercase.
- If the character is a letter, it calculates the shifted character by adding the offset to its position in the alphabet ('A' for uppercase or 'a' for lowercase). The result is then taken modulo 26 to wrap around the alphabet.
- If the character is not a letter, it is appended unchanged.
- After processing all characters, the
encoded
string is returned.
Decoding Method (decode
):
- The
decode
method is a straightforward wrapper around theencode
method. - It takes the encoded string (
enc
) and a decoding offset and callsencode
with the same string but a reversed offset (26 minus the decoding offset). - The result of
encode
is the decoded string.
Main Method:
- In the
main
method:- It defines an input string (
str
) containing a sentence. - It encodes the string using a shift of 12 characters and prints the encoded result.
- It then decodes the encoded string using the same shift of 12 characters and prints the decoded result.
- It defines an input string (
How it Works:
- The
encode
method implements the Caesar Cipher, which is a shift cipher that replaces each character in a given text with another character a fixed number of positions down the alphabet. - The
decode
method reverses the substitution by using the same shift value but in the opposite direction, effectively returning the original plaintext. - The program demonstrates the shift cipher encryption and decryption process, showing how the input string is encoded and then decoded back to its original form.
Source code in the java programming language
public class Cipher {
public static void main(String[] args) {
String str = "The quick brown fox Jumped over the lazy Dog";
System.out.println( Cipher.encode( str, 12 ));
System.out.println( Cipher.decode( Cipher.encode( str, 12), 12 ));
}
public static String decode(String enc, int offset) {
return encode(enc, 26-offset);
}
public static String encode(String enc, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : enc.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + offset) % 26 ));
} else {
encoded.append((char) ('a' + (i - 'a' + offset) % 26 ));
}
} else {
encoded.append(i);
}
}
return encoded.toString();
}
}
You may also check:How to resolve the algorithm Knapsack problem/Continuous step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the Scala programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the C programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Pascal programming language
You may also check:How to resolve the algorithm Semordnilap step by step in the TUSCRIPT programming language