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
==
at the end – Baksheesh