diff --git a/src/main/java/io/jsonwebtoken/impl/Base64UrlCodec.java b/src/main/java/io/jsonwebtoken/impl/Base64UrlCodec.java index 73420452..ea87f514 100644 --- a/src/main/java/io/jsonwebtoken/impl/Base64UrlCodec.java +++ b/src/main/java/io/jsonwebtoken/impl/Base64UrlCodec.java @@ -82,7 +82,14 @@ public class Base64UrlCodec extends AbstractTextCodec { char[] result = chars; //assume argument in case no padding is necessary - int paddingCount = chars.length % 4; + int paddingCount = 0; + + //fix for https://github.com/jwtk/jjwt/issues/31 + int remainder = chars.length % 4; + if (remainder == 2 || remainder == 3) { + paddingCount = 4 - remainder; + } + if (paddingCount > 0) { result = new char[chars.length + paddingCount]; System.arraycopy(chars, 0, result, 0, chars.length);