#501 Caesar's Code is a basic encryption algorithm
Caesar's Code is a basic encryption algorithm - Computer Science
ChemistryExplain daily providing Q&A content “#501 Caesar's Code is a basic encryption algorithm" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs
Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.
ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.
For More Chegg Questions
Join Our Telegram Channel for Covers All Update by ChemistryExplain:- Click Now
Free Chegg Question
Caesar's Code is a basic encryption algorithm. It is a substitution cipher where each letter is replaced with another letter of a fixed position (the difference between the two letters is a fixed offset). For example, if the plaintext word is (Abc) and the offset is 3, then the ciphertext is (Def). Caesar's algorithm is a simple encryption technique. You are required to implement a modified version of the algorithm described as follows: - Convert capital letters into small letters and vice versa, - Use two offsets (n1 and n2) to encrypt the letters of plaintext alternatively. Those offsets should be strictly positive and greater than 0. (keep asking users to re-enter in case of invalid input). - keep digits and symbols unchanged. Implement a method to encrypt plaintext by using the modified Caesar's algorithm and returns a ciphered text. Write a main to ask the user to enter a plaintext and valid two Integers (ni and n2). Then print the ciphered text (don't print any other text). -- You can use the console or dialogs as an application's interface.
Solve in java language ----Example: Plaintext: Hello World n15, n2 - 2 Ciphertext: MGONT DOWN
Free Chegg AnswerFor More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain
Free Chegg Answer
Create new class CaesorsAlgo.java in eclipse or in netbeans and paste the code given below
CaesorsAlgo.java
import java.util.Scanner;
public class CaesorsAlgo {
public static StringBuffer encrypt(String text, int n1, int n2) {
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
int s = n1;//get first offset
//check character is Uppercase
if (Character.isUpperCase(text.
//convert uppercase to lowercase
String word = String.valueOf(text.charAt(i))
//we have to use offset alternatively
if (i % 2 == 0) {
s = n1;//if index is even then use n1
} else {
s = n2;//else use n2
}
char ch = (char) (((int) word.charAt(0)
+ s - 97) % 26 + 97);
result.append(ch);
} else {
if (i % 2 == 0) {
s = n1;
} else {
s = n2;
}
//lowercase to uppercase
String word = String.valueOf(text.charAt(i))
char ch = (char) (((int) word.charAt(0)
+ s - 65) % 26 + 65);
result.append(ch);
}
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = 0, n2 = 0;
//n1 and n2 should be positive and greater than 0
while (n1 <= 0 && n2 <= 0) {
System.out.print("Enter first offset (n1): ");
n1 = sc.nextInt();
System.out.print("Enter second offset (n2): ");
n2 = sc.nextInt();
}
System.out.print("Enter your plaintext: ");
sc.nextLine();
String str = sc.nextLine().trim();
System.out.println();
String data[] = str.split(" ");//split the message into words
System.out.println("Plaintext: "+str);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length; i++) {
sb.append(encrypt(data[i], 5, 2) + " ");
}
System.out.println("
}
}
Labels: Chegg, Free Chegg Answer, Q&A Computer Science
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home