Skip to content

Commit 6ddb646

Browse files
Merge branch 'master' into fix-adfgvx-decrypt
2 parents b0e5252 + 4ff7eaf commit 6ddb646

14 files changed

Lines changed: 153 additions & 46 deletions

File tree

‎.github/workflows/codeql.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ jobs:
3030
distribution: 'temurin'
3131

3232
- name: Initialize CodeQL
33-
uses: github/codeql-action/init@v4.39.3
33+
uses: github/codeql-action/init@v4.39.4
3434
with:
3535
languages: 'java-kotlin'
3636

3737
- name: Build
3838
run: mvn --batch-mode --update-snapshots verify
3939

4040
- name: Perform CodeQL Analysis
41-
uses: github/codeql-action/analyze@v4.39.3
41+
uses: github/codeql-action/analyze@v4.39.4
4242
with:
4343
category: "/language:java-kotlin"
4444

@@ -55,12 +55,12 @@ jobs:
5555
uses: actions/checkout@v7
5656

5757
- name: Initialize CodeQL
58-
uses: github/codeql-action/init@v4.39.3
58+
uses: github/codeql-action/init@v4.39.4
5959
with:
6060
languages: 'actions'
6161

6262
- name: Perform CodeQL Analysis
63-
uses: github/codeql-action/analyze@v4.39.3
63+
uses: github/codeql-action/analyze@v4.39.4
6464
with:
6565
category: "/language:actions"
6666
...

‎src/main/java/com/thealgorithms/ciphers/HillCipher.java‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ public String decrypt(String message, int[][] inverseKeyMatrix) {
6868
return plainText.toString();
6969
}
7070

71-
// Validates that the determinant of the key matrix is not zero modulo 26
71+
// Validates that the key matrix is invertible modulo 26, which requires the
72+
// determinant to be coprime with 26. Checking only for a zero determinant is not
73+
// enough: a determinant of 2 or 13 (or any other non-unit) passes that check but
74+
// has no inverse, so encryption collapses distinct plaintexts onto the same
75+
// ciphertext and decryption becomes impossible.
7276
private void validateDeterminant(int[][] keyMatrix, int n) {
73-
int det = determinant(keyMatrix, n) % 26;
74-
if (det == 0) {
75-
throw new IllegalArgumentException("Invalid key matrix. Determinant is zero modulo 26.");
77+
int det = Math.floorMod(determinant(keyMatrix, n), 26);
78+
if (gcd(det, 26) != 1) {
79+
throw new IllegalArgumentException("Invalid key matrix. Determinant must be coprime with 26 to be invertible modulo 26.");
7680
}
7781
}
7882

83+
private int gcd(int a, int b) {
84+
return b == 0 ? a : gcd(b, a % b);
85+
}
86+
7987
// Computes the determinant of a matrix recursively
8088
private int determinant(int[][] matrix, int n) {
8189
int det = 0;

‎src/main/java/com/thealgorithms/ciphers/PlayfairCipher.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ public class PlayfairCipher {
66
private String key;
77

88
public PlayfairCipher(String key) {
9-
this.key = key;
9+
// Normalize the key the same way the plaintext is normalized in encrypt():
10+
// uppercase, J folded into I, and non-letters removed. Without this a
11+
// lowercase or mixed-case key, a key containing J, or a key with
12+
// punctuation silently produces a wrong matrix.
13+
this.key = key.toUpperCase().replace("J", "I").replaceAll("[^A-Z]", "");
1014
generateMatrix();
1115
}
1216

‎src/main/java/com/thealgorithms/conversions/OctalToBinary.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private OctalToBinary() {
3737
*/
3838
public static long convertOctalToBinary(int octalNumber) {
3939
long binaryNumber = 0;
40-
int digitPosition = 1;
40+
long digitPosition = 1;
4141

4242
while (octalNumber != 0) {
4343
int octalDigit = octalNumber % 10;
Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
11
package com.thealgorithms.maths;
22

3-
/*
4-
* Java program for Pronic Number
5-
* Pronic Number: A number n is a pronic number if
6-
* it is equal to product of two consecutive numbers m and m+1.
3+
/**
4+
* Pronic Number: A number n is a pronic number (or oblong number) if
5+
* it is equal to the product of two consecutive integers m and m + 1 (for m >= 0).
76
* Wikipedia: https://en.wikipedia.org/wiki/Pronic_number
87
*
9-
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
10-
*
11-
* */
12-
8+
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
9+
*/
1310
public final class PronicNumber {
1411
private PronicNumber() {
1512
}
1613

1714
/**
18-
* This method checks if the given number is pronic number or non-pronic number
15+
* Checks if the given number is a pronic number iteratively.
1916
*
20-
* @param inputNumber Integer value which is to be checked if is a pronic number or not
21-
* @return true if input number is a pronic number, false otherwise
17+
* @param inputNumber integer value to be checked
18+
* @return true if inputNumber is a pronic number, false otherwise
2219
*/
23-
static boolean isPronic(int inputNumber) {
24-
if (inputNumber == 0) {
25-
return true;
20+
public static boolean isPronic(int inputNumber) {
21+
if (inputNumber < 0) {
22+
return false;
2623
}
27-
// Iterating from 0 to input_number
28-
for (int i = 0; i <= inputNumber; i++) {
29-
// Checking if product of i and (i+1) is equals input_number
30-
if (i * (i + 1) == inputNumber && i != inputNumber) {
31-
// return true if product of i and (i+1) is equals input_number
24+
for (int i = 0; (long) i * (i + 1) <= inputNumber; i++) {
25+
if ((long) i * (i + 1) == inputNumber) {
3226
return true;
3327
}
3428
}
35-
36-
// return false if product of i and (i+1) for all values from 0 to input_number is not
37-
// equals input_number
3829
return false;
3930
}
4031

4132
/**
42-
* This method checks if the given number is pronic number or non-pronic number using square root of number for finding divisors
33+
* Checks if the given number is a pronic number in O(1) time using square root.
4334
*
44-
* @param number Integer value which is to be checked if is a pronic number or not
45-
* @return true if input number is a pronic number, false otherwise
35+
* @param number integer value to be checked
36+
* @return true if number is a pronic number, false otherwise
4637
*/
4738
public static boolean isPronicNumber(int number) {
48-
int squareRoot = (int) Math.sqrt(number); // finding just smaller divisor of the number than its square root.
49-
return squareRoot * (squareRoot + 1) == number;
39+
if (number < 0) {
40+
return false;
41+
}
42+
int squareRoot = (int) Math.sqrt(number);
43+
return (long) squareRoot * (squareRoot + 1) == number;
5044
}
5145
}

‎src/main/java/com/thealgorithms/maths/PythagoreanTriple.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public static boolean isPythagTriple(int a, int b, int c) {
3434
int[] sides = {a, b, c};
3535
java.util.Arrays.sort(sides);
3636

37-
int x = sides[0];
38-
int y = sides[1];
39-
int hypotenuse = sides[2];
37+
long x = sides[0];
38+
long y = sides[1];
39+
long hypotenuse = sides[2];
4040

4141
return x * x + y * y == hypotenuse * hypotenuse;
4242
}

‎src/main/java/com/thealgorithms/misc/MedianOfRunningArrayInteger.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public final class MedianOfRunningArrayInteger extends MedianOfRunningArray<Integer> {
44
@Override
55
public Integer calculateAverage(final Integer a, final Integer b) {
6-
return (a + b) / 2;
6+
// Widen to long so that summing values near Integer.MAX_VALUE does not overflow.
7+
final long sum = (long) a + b;
8+
return (int) (sum / 2);
79
}
810
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.thealgorithms.misc;
22

3+
import java.math.BigInteger;
4+
35
public final class MedianOfRunningArrayLong extends MedianOfRunningArray<Long> {
46
@Override
57
public Long calculateAverage(final Long a, final Long b) {
6-
return (a + b) / 2L;
8+
// a + b overflows a long near its bounds and there is no wider primitive to widen to.
9+
return BigInteger.valueOf(a).add(BigInteger.valueOf(b)).divide(BigInteger.TWO).longValueExact();
710
}
811
}

‎src/test/java/com/thealgorithms/ciphers/HillCipherTest.java‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.ciphers;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

@@ -33,4 +34,25 @@ void hillCipherDecryptTest() {
3334
// then
3435
assertEquals("ACT", plainText);
3536
}
37+
38+
@Test
39+
void rejectsNonInvertibleKeyWithEvenDeterminant() {
40+
// det = 2, gcd(2, 26) = 2: passes the old zero-check but has no inverse mod 26
41+
int[][] keyMatrix = {{1, 0}, {0, 2}};
42+
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("AA", keyMatrix));
43+
}
44+
45+
@Test
46+
void rejectsNonInvertibleKeyWithDeterminantThirteen() {
47+
// det = 13, gcd(13, 26) = 13: also not invertible mod 26
48+
int[][] keyMatrix = {{13, 0}, {0, 1}};
49+
assertThrows(IllegalArgumentException.class, () -> hillCipher.encrypt("BB", keyMatrix));
50+
}
51+
52+
@Test
53+
void acceptsKeyWithNegativeCoprimeDeterminant() {
54+
// det = -1, coprime with 26: a valid key that a naive signed modulo would misclassify
55+
int[][] keyMatrix = {{1, 2}, {1, 1}};
56+
assertEquals("XP", hillCipher.encrypt("HI", keyMatrix));
57+
}
3658
}

‎src/test/java/com/thealgorithms/ciphers/PlayfairTest.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,30 @@ public void testEncryptionAndDecryption() {
3434

3535
assertEquals(plaintext, decryptedText);
3636
}
37+
38+
@Test
39+
public void testLowercaseKeyBehavesLikeUppercase() {
40+
PlayfairCipher lowercase = new PlayfairCipher("monarchy");
41+
PlayfairCipher uppercase = new PlayfairCipher("MONARCHY");
42+
43+
assertEquals(uppercase.encrypt("HELLO"), lowercase.encrypt("HELLO"));
44+
assertEquals("CFSUPM", lowercase.encrypt("HELLO"));
45+
}
46+
47+
@Test
48+
public void testKeyContainingJStillCoversZ() {
49+
PlayfairCipher playfairCipher = new PlayfairCipher("JUMP");
50+
51+
String decryptedText = playfairCipher.decrypt(playfairCipher.encrypt("ZOO"));
52+
53+
assertEquals("ZOXO", decryptedText);
54+
}
55+
56+
@Test
57+
public void testKeyWithNonLettersIsStripped() {
58+
PlayfairCipher messy = new PlayfairCipher("Mon@rchy2026");
59+
PlayfairCipher clean = new PlayfairCipher("MONRCHY");
60+
61+
assertEquals(clean.encrypt("HELLO"), messy.encrypt("HELLO"));
62+
}
3763
}

0 commit comments

Comments
 (0)