Caesar Cipher Java Code

Caesar cipher technique was founded by Julius caesar. Before looking at the caesar cipher program in java with output for encryption and decryption, first, we need to understand the terms plaintext and ciphertext.

The challenge The action of a Caesar cipher is to replace each plaintext letter (plaintext letters are from ‘a’ to ‘z’ or from ‘A’ to ‘Z’) with a different one a fixed number of places up or down the alphabet. This program performs a variation of the Caesar shift. The shift increases by 1 for Read More »First Variation on Caesar Cipher in Java. Note: The terms are explained in the order in which they are used in cryptography. Cryptography and ciphers techniques This is my code to encrypt-decrypt message using caesar cipher. Below I have shared the program to implement this algorithm in Java.: the letter after z is a, and the letter after Z is A).

Read Also:Vigenere Cipher Program in Java
What is plaintext and ciphertext?
plaintext is the input message given by user. In other words, message that needs to be encrypted.
ciphertext is the encrypted message. In other words, message after applying the caesar cipher technique.
What is Caesar cipher?
Caesar cipher is one of the simplest encryption technique. It is also known as the shift cipher, Caesar's cipher, Caesar shift or Caesar's code. Caesar cipher is a type of substitution cipher.
By using this cipher technique we can replace each letter in the plaintext with different one a fixed number of places up or down the alphabet.
For example :
With right shift of 3:
Caesar cipher in java source codeplaintext : ABCDEFGHIJKLMNOPQRSTUVWXYZ
ciphertext : DEFGHIJKLMNOPQRSTUVWXYZABC
With left shift of 3:
plaintext : ABCDEFGHIJKLMNOPQRSTUVWXYZ
ciphertext : XYZABCDEFGHIJKLMNOPQRSTUVW
With right shift of 2:
plaintext : Java Hungry Blog
ciphertext : Lcxc Jwpita Dnqi
As you can see for the above example 'Java Hungry Blog' each character in plain text is shifted by 2 as J become L , a become c , v become x and so on.
Note : You can use either left shift or right shift but not both in same text.

Caesar Cipher Java Program


If we want to see Caesar cipher in mathematical way, then formula to get encrypted letter will be :
e = (x + n) mod 26
where,
n is the number of positions we need to shift plaintext characters
x is the place value of original letter
e is the place value of encrypted letter
On the other hand, we will use the below formula to decrypt each letter.
e = (x - n) mod 26

Caesar Cipher Java Code Template


Caesar Cipher Java Code Pdf

Pseudo Code for Caesar Cipher
  1. Input : String plaintext
  2. Input : An integer between 0 and 25 representing the right shift of the character or,
  3. an integer between -25 and -1 representing the left shift of the characters.
  4. Traverse each character in the plaintext one at a time
  5. Transform the given character depending on encryption or decryption.
  6. print the ciphertext.

Simple Caesar Cipher Program in Java for Encryption

Output :
Input the plaintext message : Java Hungry Blog
Enter the value by which each character in the plaintext message gets shifted : 2
ciphertext : Lcxc Jwpita Dnqi
Simple Caesar Cipher Program in Java for Decryption
Output :
Input the ciphertext message : Lcxc Jwpita Dnqi

Caesar Cipher Java Code

Enter the shift value : 2
decrypt message : Java Hungry Blog
CipherThat's all for the post caesar cipher program in java with output. If you have any questions then please mention in the comments below.
Image Credits : Cepheus [Public domain], from Wikimedia Commons

Caesar Cipher Java Program Encryption And Decryption


Matt_Crypto [Public domain], via Wikimedia Commons

Hello Friends, in this tutorial we are going to learn Hackerrank Algorithm Caesar Cipher.

Challenge Name: Caesar Cipher
Problem:

Caesar Cipher Java Code

Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar’s cipher rotated every letter in a string by a fixed number, K, making it unreadable by his enemies. Given a string, S, and a number, K, encrypt S and print the resulting string.

Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.

Caesar Cipher Java Code Example

Input Format

The first line contains an integer, N, which is the length of the unencrypted string.
The second line contains the unencrypted string, S.
The third line contains the integer encryption key, K, which is the number of letters to rotate.

Constraints
1 ≤ N ≤ 100
0 ≤ K ≤ 100

S is a valid ASCII string and doesn’t contain any spaces.

Output Format

For each test case, print the encoded string.

Sample Input

Sample Output

Explanation
Each unencrypted letter is replaced with the letter occurring K spaces after it when listed alphabetically. Think of the alphabet as being both case-sensitive and circular; if K rotates past the end of the alphabet, it loops back to the beginning (i.e.: the letter after z is a, and the letter after Z is A).

Selected Examples:
m (ASCII 109) becomes o (ASCII 111).
i (ASCII 105) becomes k(ASCII 107).
– remains the same, as symbols are not encoded.
O (ASCII 79) becomes Q(ASCII 81).
z (ASCII 122) becomes b (ASCII 98); because z is the last letter of the alphabet, a (ASCII 97) is the next letter after it in lower-case rotation.

Solution Video:

Solution Code:

Challenge link:
https://www.hackerrank.com/challenges/caesar-cipher-1

Github Code link:
https://github.com/brighterapi/HackerRank-Solution/blob/master/HackerRank-Solution/HackerRankSolution/src/com/hackerranksolution/algorithms/Strings/CaesarCipher.java

Thank you 🙂