Java Input byte array has wrong 4-byte ending unit
Asked Answered
L

2

7

I am trying to convert the following string into a Public Key using X509 and KeyFactory. The Code that I have written so far is:

public static void main(String args[]) throws Exception {
    String key = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAidRWp1iB0s2LKnAyAxUKCMmsKK9bMTdRUItZVRcV4lB0RXGla0wRTNeR6r5oqNo6poHUJ+QGPjAHDCztMjAZdtuMSQ+Lohn+TjDMIEi2sUNeXhZuXchwEE+3QTgPpIOGhjJtv4wmTjXD5UaZbYWuydNpgvFEDsF4jf02xM8t8a7nOgQIriPi83fa4XHXcoCcG"
            + "EHDbpbtYUhVq12rJEBXUoVM1zi9LcDhEsgilpzRPlkT6zC+89SkgYHWTRtO2shLpJcnThkR1nyLqHU2Zgn1hSrNsy+T97bNL1Umhcs7e94WJ7WWO6PoSst4cknPIZhhRbeBHoJ9rdV+XLBoew7buDQSht2Jn"
            + "zAm6A6Pvi+XhLVRlIEMLOsG6Y92Lwhuc21oSKeqklv9yDfMznJm0aeCbm3TWZehAfPD9EKJ4LgvSVbT"
            + "tXSiOVvPS8JtzIedISqioSvPPP5v4qqdbqobGBv2uE0sdwYhXh+dTIFSO4WG+dQHMZpdZu38lFBec3y"
            + "EuZJuKpvtX5AvdYgCEwMioZxE3ph4X3SJEbcqfR1KuuGnYwg6nmSEwotDVg55pEtSsgu3j2KRgM8GA"
            + "7lkageikM4D6mq6vQ5fkedfzz8PuvQNe8BH3h2UZYmRjNvfKd8wt2bRKKFK7K4jCYT5riYo+5aEWS"
            + "SrWvL+ECAwEAAQ==";
    getKey(key);
      }
public static PublicKey getKey(String key){
    try{
        byte[] byteKey = Base64.getDecoder().decode(key.getBytes());    
        X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
        KeyFactory kf = KeyFactory.getInstance("RSA");

        return kf.generatePublic(X509publicKey);
    }
    catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

The error that I received, is the following:

java.lang.IllegalArgumentException: Input byte array has wrong 4-byte ending unit
at java.base/java.util.Base64$Decoder.decode0(Base64.java:781)
at java.base/java.util.Base64$Decoder.decode(Base64.java:567)
at WannaCry.getKey(WannaCry.java:46)
at WannaCry.main(WannaCry.java:27)

I'm not sure if I'm getting the wrong key, as this is from my assignment. This is the key I have to use for the Key.

MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAidRWp1iB0s2LKnAyAxUKCMmsKK9bMTdRUItZ
VRcV4lB0RXGla0wRTNeR6r5oqNo6poHUJ+QGPjAHDCzt/MjAZdtuMSQ+Lohn+TjDMIEi2sUNeXhZuXch
w/EE+3QTgPpIOGhjJtv4wmTjXD5UaZbYWuydNpgvFEDsF4jf02xM8t8a7nOgQIriPi83f/a4XHXcoCcG
EHDbpbtYUhVq12rJEBXUoVM1zi9LcDhEsgil/pzRPlkT6zC+89SkgYHWTRtO2shLpJcnThkR1nyLqHU2
Zgn1hSrNsy+T97bNL1Umhcs7/e94WJ7WWO6PoSs/t4cknPIZhhRbeBHoJ9rdV+XLBoew7buDQSht2Jn/
zAm6A6Pvi+XhLVRlIEMLOsG6Y92Lwhuc21oS/Keqklv9yDfMznJm0aeCbm3TWZehAfPD9EKJ4LgvSVbT
tXSiOVvPS8JtzIedISqioSvPPP5v4qqdbqobGBv2uE0sdwYhXh+dTIFSO4WG+dQHMZpdZu38l/FBec3y
EuZJuK/pvtX5AvdYgCEwMioZxE3ph4X3S/JEbcqfR1KuuGnYwg6nmSEwotDVg55pEtSsgu3j2KRgM8GA
7lkageikM4D6m/q6vQ5fkedfzz8PuvQn/Ne8BH3h2UZYmRjNvfKd8wt2bRKKFK7K4jCYT5riYo+5aEWS
SrWvL+ECAwEAAQ==

I have of course removed all of the line breakers ("/") but I am getting this error, or sometimes I am getting

Invalid Byte Array Character D

Latrishalatry answered 10/2, 2021 at 14:18 Comment(6)
Copy past the string again properly. it is workin fine for me with the string you mentioned belowAgro
Try removing == at the endBaksheesh
@PandeyAmit, we shouldn't remove those characters. he made some mistake while copying string in his code. that's allAgro
I compared the original string to the string in your code .you should only remove "\n" not all the "\" s. hope it helpsAgro
I'll try checking the string, and yeah if I remove == at the end I get a separate errorLatrishalatry
Thank you I removed the /n and it worked. CheersLatrishalatry
E
2

Remove \n from signature/key works for me, hope it helps you too.

Eurhythmics answered 22/1 at 6:7 Comment(0)
H
-2

Java 8 BUG, the fix is https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8008925 Enjoy!

Holtz answered 16/3, 2022 at 11:36 Comment(1)
you posted a bugreport that literally says the bug still isn't fixed... According to what I read, in most cases the issue could be solved by removing the padding from the StringDreda

© 2022 - 2024 — McMap. All rights reserved.