File tree Expand file tree Collapse file tree
main/java/com/thealgorithms/conversions
test/java/com/thealgorithms/conversions Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -119,8 +119,15 @@ public static byte[] decode(String input) {
119119
120120 // Validate padding: '=' can only appear at the end (last 1 or 2 chars)
121121 int firstPadding = input .indexOf ('=' );
122- if (firstPadding != -1 && firstPadding < input .length () - 2 ) {
123- throw new IllegalArgumentException ("Padding '=' can only appear at the end (last 1 or 2 characters)" );
122+ if (firstPadding != -1 ) {
123+ if (firstPadding < input .length () - 2 ) {
124+ throw new IllegalArgumentException ("Padding '=' can only appear at the end (last 1 or 2 characters)" );
125+ }
126+ for (int i = firstPadding ; i < input .length (); i ++) {
127+ if (input .charAt (i ) != '=' ) {
128+ throw new IllegalArgumentException ("A padding '=' must not be followed by a non-padding character" );
129+ }
130+ }
124131 }
125132
126133 List <Byte > result = new ArrayList <>();
Original file line number Diff line number Diff line change @@ -127,6 +127,9 @@ void testInvalidPaddingPosition() {
127127 assertThrows (IllegalArgumentException .class , () -> Base64 .decode ("Q=QQ" ));
128128 assertThrows (IllegalArgumentException .class , () -> Base64 .decode ("Q=Q=" ));
129129 assertThrows (IllegalArgumentException .class , () -> Base64 .decode ("=QQQ" ));
130+ assertThrows (IllegalArgumentException .class , () -> Base64 .decode ("QQ=Q" ));
131+ assertThrows (IllegalArgumentException .class , () -> Base64 .decode ("AB=C" ));
132+ assertThrows (IllegalArgumentException .class , () -> Base64 .decode ("AB=A" ));
130133 }
131134
132135 @ Test
You can’t perform that action at this time.
0 commit comments