mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-12 14:42:03 +08:00
Fix columnarTranspositionCipher and typos in Test (#5649)
This commit is contained in:
@ -27,13 +27,13 @@ public final class ColumnarTranspositionCipher {
|
|||||||
* @return a String with the word encrypted by the Columnar Transposition
|
* @return a String with the word encrypted by the Columnar Transposition
|
||||||
* Cipher Rule
|
* Cipher Rule
|
||||||
*/
|
*/
|
||||||
public static String encrpyter(String word, String keyword) {
|
public static String encrypt(final String word, final String keyword) {
|
||||||
ColumnarTranspositionCipher.keyword = keyword;
|
ColumnarTranspositionCipher.keyword = keyword;
|
||||||
abecedariumBuilder(500);
|
abecedariumBuilder();
|
||||||
table = tableBuilder(word);
|
table = tableBuilder(word);
|
||||||
Object[][] sortedTable = sortTable(table);
|
Object[][] sortedTable = sortTable(table);
|
||||||
StringBuilder wordEncrypted = new StringBuilder();
|
StringBuilder wordEncrypted = new StringBuilder();
|
||||||
for (int i = 0; i < sortedTable[i].length; i++) {
|
for (int i = 0; i < sortedTable[0].length; i++) {
|
||||||
for (int j = 1; j < sortedTable.length; j++) {
|
for (int j = 1; j < sortedTable.length; j++) {
|
||||||
wordEncrypted.append(sortedTable[j][i]);
|
wordEncrypted.append(sortedTable[j][i]);
|
||||||
}
|
}
|
||||||
@ -51,11 +51,12 @@ public final class ColumnarTranspositionCipher {
|
|||||||
* @return a String with the word encrypted by the Columnar Transposition
|
* @return a String with the word encrypted by the Columnar Transposition
|
||||||
* Cipher Rule
|
* Cipher Rule
|
||||||
*/
|
*/
|
||||||
public static String encrpyter(String word, String keyword, String abecedarium) {
|
public static String encrypt(String word, String keyword, String abecedarium) {
|
||||||
ColumnarTranspositionCipher.keyword = keyword;
|
ColumnarTranspositionCipher.keyword = keyword;
|
||||||
ColumnarTranspositionCipher.abecedarium = Objects.requireNonNullElse(abecedarium, ABECEDARIUM);
|
ColumnarTranspositionCipher.abecedarium = Objects.requireNonNullElse(abecedarium, ABECEDARIUM);
|
||||||
table = tableBuilder(word);
|
table = tableBuilder(word);
|
||||||
Object[][] sortedTable = sortTable(table);
|
Object[][] sortedTable = sortTable(table);
|
||||||
|
|
||||||
StringBuilder wordEncrypted = new StringBuilder();
|
StringBuilder wordEncrypted = new StringBuilder();
|
||||||
for (int i = 0; i < sortedTable[0].length; i++) {
|
for (int i = 0; i < sortedTable[0].length; i++) {
|
||||||
for (int j = 1; j < sortedTable.length; j++) {
|
for (int j = 1; j < sortedTable.length; j++) {
|
||||||
@ -72,7 +73,7 @@ public final class ColumnarTranspositionCipher {
|
|||||||
* @return a String decrypted with the word encrypted by the Columnar
|
* @return a String decrypted with the word encrypted by the Columnar
|
||||||
* Transposition Cipher Rule
|
* Transposition Cipher Rule
|
||||||
*/
|
*/
|
||||||
public static String decrypter() {
|
public static String decrypt() {
|
||||||
StringBuilder wordDecrypted = new StringBuilder();
|
StringBuilder wordDecrypted = new StringBuilder();
|
||||||
for (int i = 1; i < table.length; i++) {
|
for (int i = 1; i < table.length; i++) {
|
||||||
for (Object item : table[i]) {
|
for (Object item : table[i]) {
|
||||||
@ -91,14 +92,14 @@ public final class ColumnarTranspositionCipher {
|
|||||||
*/
|
*/
|
||||||
private static Object[][] tableBuilder(String word) {
|
private static Object[][] tableBuilder(String word) {
|
||||||
Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
|
Object[][] table = new Object[numberOfRows(word) + 1][keyword.length()];
|
||||||
char[] wordInChards = word.toCharArray();
|
char[] wordInChars = word.toCharArray();
|
||||||
// Fils in the respective numbers
|
// Fills in the respective numbers for the column
|
||||||
table[0] = findElements();
|
table[0] = findElements();
|
||||||
int charElement = 0;
|
int charElement = 0;
|
||||||
for (int i = 1; i < table.length; i++) {
|
for (int i = 1; i < table.length; i++) {
|
||||||
for (int j = 0; j < table[i].length; j++) {
|
for (int j = 0; j < table[i].length; j++) {
|
||||||
if (charElement < wordInChards.length) {
|
if (charElement < wordInChars.length) {
|
||||||
table[i][j] = wordInChards[charElement];
|
table[i][j] = wordInChars[charElement];
|
||||||
charElement++;
|
charElement++;
|
||||||
} else {
|
} else {
|
||||||
table[i][j] = ENCRYPTION_FIELD_CHAR;
|
table[i][j] = ENCRYPTION_FIELD_CHAR;
|
||||||
@ -116,7 +117,7 @@ public final class ColumnarTranspositionCipher {
|
|||||||
* order to respect the Columnar Transposition Cipher Rule.
|
* order to respect the Columnar Transposition Cipher Rule.
|
||||||
*/
|
*/
|
||||||
private static int numberOfRows(String word) {
|
private static int numberOfRows(String word) {
|
||||||
if (word.length() / keyword.length() > word.length() / keyword.length()) {
|
if (word.length() % keyword.length() != 0) {
|
||||||
return (word.length() / keyword.length()) + 1;
|
return (word.length() / keyword.length()) + 1;
|
||||||
} else {
|
} else {
|
||||||
return word.length() / keyword.length();
|
return word.length() / keyword.length();
|
||||||
@ -173,13 +174,11 @@ public final class ColumnarTranspositionCipher {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an abecedarium with a specified ascii inded
|
* Creates an abecedarium with all available ascii values.
|
||||||
*
|
|
||||||
* @param value Number of characters being used based on the ASCII Table
|
|
||||||
*/
|
*/
|
||||||
private static void abecedariumBuilder(int value) {
|
private static void abecedariumBuilder() {
|
||||||
StringBuilder t = new StringBuilder();
|
StringBuilder t = new StringBuilder();
|
||||||
for (int i = 0; i < value; i++) {
|
for (int i = 0; i < 256; i++) {
|
||||||
t.append((char) i);
|
t.append((char) i);
|
||||||
}
|
}
|
||||||
abecedarium = t.toString();
|
abecedarium = t.toString();
|
||||||
|
@ -20,7 +20,7 @@ public class ColumnarTranspositionCipherTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEncryption() {
|
public void testEncryption() {
|
||||||
String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
|
String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
|
||||||
assertNotNull(encryptedText, "The encrypted text should not be null.");
|
assertNotNull(encryptedText, "The encrypted text should not be null.");
|
||||||
assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty.");
|
assertFalse(encryptedText.isEmpty(), "The encrypted text should not be empty.");
|
||||||
// Check if the encrypted text is different from the plaintext
|
// Check if the encrypted text is different from the plaintext
|
||||||
@ -29,19 +29,19 @@ public class ColumnarTranspositionCipherTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecryption() {
|
public void testDecryption() {
|
||||||
String encryptedText = ColumnarTranspositionCipher.encrpyter(plaintext, keyword);
|
String encryptedText = ColumnarTranspositionCipher.encrypt(plaintext, keyword);
|
||||||
String decryptedText = ColumnarTranspositionCipher.decrypter();
|
String decryptedText = ColumnarTranspositionCipher.decrypt();
|
||||||
|
|
||||||
assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces.");
|
assertEquals(plaintext.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original plaintext, ignoring spaces.");
|
||||||
assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
|
assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(plaintext, keyword), "The encrypted text should be the same when encrypted again.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLongPlainText() {
|
public void testLongPlainText() {
|
||||||
String longText = "This is a significantly longer piece of text to test the encryption and decryption capabilities of the Columnar Transposition Cipher. It should handle long strings gracefully.";
|
String longText = "This is a significantly longer piece of text to test the encryption and decryption capabilities of the Columnar Transposition Cipher. It should handle long strings gracefully.";
|
||||||
String encryptedText = ColumnarTranspositionCipher.encrpyter(longText, keyword);
|
String encryptedText = ColumnarTranspositionCipher.encrypt(longText, keyword);
|
||||||
String decryptedText = ColumnarTranspositionCipher.decrypter();
|
String decryptedText = ColumnarTranspositionCipher.decrypt();
|
||||||
assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces.");
|
assertEquals(longText.replaceAll(" ", ""), decryptedText.replaceAll(" ", ""), "The decrypted text should match the original long plaintext, ignoring spaces.");
|
||||||
assertEquals(encryptedText, ColumnarTranspositionCipher.encrpyter(longText, keyword), "The encrypted text should be the same when encrypted again.");
|
assertEquals(encryptedText, ColumnarTranspositionCipher.encrypt(longText, keyword), "The encrypted text should be the same when encrypted again.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user