Replaced String with StringBuilder so multiple String Objects don't get created (#2238)
This commit is contained in:
parent
0fd737e555
commit
e67212138c
@ -4,50 +4,51 @@ package ciphers;
|
|||||||
* A Java implementation of Vigenere Cipher.
|
* A Java implementation of Vigenere Cipher.
|
||||||
*
|
*
|
||||||
* @author straiffix
|
* @author straiffix
|
||||||
|
* @author beingmartinbmc
|
||||||
*/
|
*/
|
||||||
public class Vigenere {
|
public class Vigenere {
|
||||||
|
|
||||||
public static String encrypt(final String message, final String key) {
|
public static String encrypt(final String message, final String key) {
|
||||||
|
|
||||||
String result = "";
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
for (int i = 0, j = 0; i < message.length(); i++) {
|
for (int i = 0, j = 0; i < message.length(); i++) {
|
||||||
char c = message.charAt(i);
|
char c = message.charAt(i);
|
||||||
if (Character.isLetter(c)) {
|
if (Character.isLetter(c)) {
|
||||||
if (Character.isUpperCase(c)) {
|
if (Character.isUpperCase(c)) {
|
||||||
result += (char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A');
|
result.append((char) ((c + key.toUpperCase().charAt(j) - 2 * 'A') % 26 + 'A'));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result += (char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a');
|
result.append((char) ((c + key.toLowerCase().charAt(j) - 2 * 'a') % 26 + 'a'));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result += c;
|
result.append(c);
|
||||||
}
|
}
|
||||||
j = ++j % key.length();
|
j = ++j % key.length();
|
||||||
}
|
}
|
||||||
return result;
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String decrypt(final String message, final String key) {
|
public static String decrypt(final String message, final String key) {
|
||||||
String result = "";
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
for (int i = 0, j = 0; i < message.length(); i++) {
|
for (int i = 0, j = 0; i < message.length(); i++) {
|
||||||
|
|
||||||
char c = message.charAt(i);
|
char c = message.charAt(i);
|
||||||
if (Character.isLetter(c)) {
|
if (Character.isLetter(c)) {
|
||||||
if (Character.isUpperCase(c)) {
|
if (Character.isUpperCase(c)) {
|
||||||
result += ((char) ('Z' - (25 - (c - key.toUpperCase().charAt(j))) % 26));
|
result.append((char) ('Z' - (25 - (c - key.toUpperCase().charAt(j))) % 26));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
result += ((char) ('z' - (25 - (c - key.toLowerCase().charAt(j))) % 26));
|
result.append((char) ('z' - (25 - (c - key.toLowerCase().charAt(j))) % 26));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result += c;
|
result.append(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
j = ++j % key.length();
|
j = ++j % key.length();
|
||||||
}
|
}
|
||||||
return result;
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
Loading…
Reference in New Issue
Block a user