patternjavaMinor
First try Caesar Cipher
Viewed 0 times
caesarfirsttrycipher
Problem
I've recently begun to teach myself to code. One simple project that I wanted to do was create a Caesar cipher that works well. Now that I feel I've accomplished that task, I'd like to know how I'm doing with overall code quality. Here's the code.
```
import java.util.Scanner;
public class Encryptor
{
private char[] upperCase = {'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'};
private char[] lowerCase = {'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'};
private char oneChar;
private String encryptedString = "";
private int counter;
public String encryptString(String input, int offset)
{
for (counter = 0; counter 26)
{
encryptedString += oneChar;
moveOn = true;
}
//remainder keeps me from going over array length
else if (oneChar == upperCase[i%26])
{
encryptedString += upperCase[(i+offset)%26];
moveOn = true;
}
else if (oneChar == lowerCase[i%26])
{
encryptedString += lowerCase[(i+offset)%26];
moveOn = true;
}
else
{
i++;
}
}
}
return encryptedString;
}
}
public class Main
{
public static void main (String[] args)
{
String inText = "";
String encryptedText = "";
int offset = 0;
String choice;
Scanner input = new Scanner(System.in);
System.out.println("Enter something to encrypt or decrypt");
inText = input.nextLine();
System.out.println("Would you like to encrypt your message, or decrypt your message? Please type in encrypt or decrypt
```
import java.util.Scanner;
public class Encryptor
{
private char[] upperCase = {'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'};
private char[] lowerCase = {'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'};
private char oneChar;
private String encryptedString = "";
private int counter;
public String encryptString(String input, int offset)
{
for (counter = 0; counter 26)
{
encryptedString += oneChar;
moveOn = true;
}
//remainder keeps me from going over array length
else if (oneChar == upperCase[i%26])
{
encryptedString += upperCase[(i+offset)%26];
moveOn = true;
}
else if (oneChar == lowerCase[i%26])
{
encryptedString += lowerCase[(i+offset)%26];
moveOn = true;
}
else
{
i++;
}
}
}
return encryptedString;
}
}
public class Main
{
public static void main (String[] args)
{
String inText = "";
String encryptedText = "";
int offset = 0;
String choice;
Scanner input = new Scanner(System.in);
System.out.println("Enter something to encrypt or decrypt");
inText = input.nextLine();
System.out.println("Would you like to encrypt your message, or decrypt your message? Please type in encrypt or decrypt
Solution
It would seem that you are restricting yourself to English alphabet, when there is no need for that. All that Caesar cipher does, is adding some constant (usually called key) to each character. Decoding then may be done by subtracting that very key from each encoded character. All in all, I had this in mind:
Also, using
will improve the performance of your cipher: when you do
a new
I run your program, and it does not cipher the white-space. This makes breaking your cipher easier as the attacker will see clearly the lengths of words. Instead, cipher any character in the input string.
public class Encryptor {
public static String encryptString(String input, int key) {
StringBuilder sb = new StringBuilder(input.length());
for (char c : input.toCharArray()) {
sb.append((char)(c + key));
}
return sb.toString();
}
public static String decryptString(String input, int key) {
return encryptString(input, -key);
}
}Also, using
StringBuilderwill improve the performance of your cipher: when you do
char c = '?';
myString += c;a new
String is created which runs in linear time as it needs to copy the character content. However, appending to a StringBuilder of a known size runs in constant time.I run your program, and it does not cipher the white-space. This makes breaking your cipher easier as the attacker will see clearly the lengths of words. Instead, cipher any character in the input string.
Code Snippets
public class Encryptor {
public static String encryptString(String input, int key) {
StringBuilder sb = new StringBuilder(input.length());
for (char c : input.toCharArray()) {
sb.append((char)(c + key));
}
return sb.toString();
}
public static String decryptString(String input, int key) {
return encryptString(input, -key);
}
}char c = '?';
myString += c;Context
StackExchange Code Review Q#123170, answer score: 2
Revisions (0)
No revisions yet.