Recurssion is not required. Cryptography is the process of transforming messages
ID: 3751784 • Letter: R
Question
Recurssion is not required.
Cryptography is the process of transforming messages so that they cannot be read by unauthorized parties. There are many different methods for encrypting messages; some are much more secure than others. A "more secure" method is one which is harder to break. Cryptography is essential in our society; it is used to protect all sorts of private data that is stored anc transmitted online, such as credit card numbers. Modern cryptography methods are based on complex mathematical operations. In theory these cryptography methods can be cracked but in practice (so far) they cannot be cracked because it would take too long for any existing computer to perform the necessary computations A cipher is an algorithm that translates a message into an encoded form, so that it looks like gibberish to anyone who doesn't know how to decode the message. The cipher also provides the method to translate the message back to its original form. The original text is called plaintext; the encoded message is called ciphertext. Most ciphers use a key, which is a parameter that controls the encryption. To decrypt a message, it's necessary to have the key. Those who don't have the key cannot read the message In this lab we will work with a very simple encryption method called the Caesar cipher. In a Caesar cipher the key is an integer n. To encode a message, each letter is shifted n positions forward in the alphabet. The letters at the end of the alphabet wrap around to the beginning of the alphabet. For example, if the key is 4, the shift transforms: w- d x-> b Special characters are not modified. So with a key of 4, the plaintext "Hi Mom!" is encoded into the ciphertext "Lm Qsq!". To decrypt a message, every character is shifted the same number of characters in the opposite directionExplanation / Answer
Answer:
import java. util.Scanner;
public class CaesarCipher
{
public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
/* ENCRYPTION FUNCTION-cipher
The Caesar Cipher can be expressed in a more mathematical form as follows:
En(x)=(x+n)mod26
In plain terms, this means that the encryption of a letter x is equal to a shift of x + n, where n is the number of letters shifted. The result of the process is then taken under modulo division, essentially meaning that if a letter is shifted past the end of the alphabet, it wraps around to the beginning.
*/
public static String cipher (String plainText, int shiftKey)
{
plainText = plainText.toLowerCase();
String cipher Text = "";
for (int i = 0; i < plaintext. Length (); i++)
{
int char Position = ALPHABET.indexOf(plainText.charAt(i));
int keyVal = (shiftKey + charPosition) % 26;
char replaceVal = ALPHABET.charAt(keyVal);
cipherText += replaceVal;
}
return cipherText;
}
/*
DECRYPTION FUNCTION- breaker
Decryption of the encrypted text (called the ciphertext) would be carried out similarly, subtracting the shift amount.
Dn(x)=(x-n)mod 26
*/
public static String breaker(String cipherText, int shiftKey)
{
cipherText = cipherText.toLowerCase(); String plainText = "";
for (int i = 0; i < cipherText.length(); i++)
{
int charPosition = ALPHABET.indexOf(cipherText.charAt(i));
int keyVal = (charPosition - shiftKey) % 26;
if (keyVal < 0)
{
keyVal = ALPHABET.length() + keyVal;
}
char replaceVal = ALPHABET.charAt(keyVal);
plainText += replaceVal;
}
return plainText;
}
public static void main(String[] args)
{ Scanner sc = new Scanner (System.in);
System.out.println("Enter the String for Encryption: ");
String message = new String();
message = sc.next();
System.out.println(cipher(message, 3));
System.out.println(breaker (cipher (message, 3), 3));
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.