PublicKey Unknown Encoding I have a public key:

MIGJAoGBAKv4OKlpYBAQEFAAOCADqTnisSvdLP+mTswZJdbtk1J+4+qAySJuENCe
+1rcnERjdNa2LTS2y//REwXWo8Jd3mU42GfZJrJhSLFOgYnX3efuMBVebWlqUNGt
sMedxuVaFBL3SoqU7e5RELIsuArCJJIgz86BQDX0x63VpXAgMBAAE=


which, if you use any online base64 decoder, will say it’s invalid Base64 encoding – which is why decoding fails. But I can get it to decode using the Apache Commons Base64.decodeBase64() function.

My current implementation in Java is:

String PUB_KEY = "MIGJAoGBAKv4OKlpYBAQEFAAOCADqTnisSvdLP+mTswZJdbtk1J+4+qAySJuENCe+1rcnERjdNa2LTS2y//REwXWo8Jd3mU42GfZJrJhSLFOgYnX3efuMBVebWlqUNGtsMedxuVaFBL3SoqU7e5RELIsuArCJJIgz86BQDX0x63VpXAgMBAAE=";

byte[] keyBytes = Base64.decodeBase64(PUB_KEY);
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
try {
    KeyFactory factory = KeyFactory.getInstance("RSA");
    PublicKey key = factory.generatePublic(spec);

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decrypted = cipher.doFinal(Base64.decodeBase64("Zm/qR/FrkzawabBZYk7WfQJNMVZoZrwWTvfQwIhPMzAuqEO+y+sb/x9+TZwTbqmu45/GV4yhKv0bbDL8F6rif7RJap7iQUFQBDEIAraY42IGZ8pB6A0Q0RSnJWW+tLTLJg5cTrgZQ8sLoO+U03T6DE1wy73FU5h6XhXxZERo0tQ="));
    String tada = new String(decrypted, StandardCharsets.UTF_8);
    tada.length();
} catch (Exception e) {
    e.getMessage();
}


There already exists a C++ implementation of this – which works – but the moment I get to factory.generatePublic(spec) I get an InvalidKeySpec error.

I can tell you that with this public key and with the encrypted text I should get this unencrypted text: 2ABB43E83F7EC33D0D33F64BA5782E42.

What is wrong with the public key where I am getting an error about it being the wrong key spec?
MIGJAoGBAKv4...................3VpXAgMBAAE=
30818902818100 ABF8... ...DD5A57 0203010001
        7                             5         


Overhead &amp; public exponent are 7 + 5 = 12 bytes. You have a 1024 bit modulus = 128 bytes.
So a correct encoding would be 12 + 128 = 140 bytes, requiring ceil(140 / 3) = 47 * 4 = 188 base 64 characters.

You however seem to have 182 characters, so there are 6 characters missing in action. These characters are part of the modulus (because they cannot be anywhere else, you clearly have the head and tail of the public key). That means you have to find 6 * 6 bits that can have basically any value, for a total of 36 bits or 2^36 = ~69 billion possible values. The 6 bit values may be at any location as well. That would multiply the number of options with over 8 billion if I'm not mistaken or 33 bits. Unfortunately 69 bits is out of range for brute force efforts. 

The best thing you can do is guess that you are missing very specific bytes.
Prime suspects are + and / signs, but it would be a long shot inserting 6 of these characters at every possible place, testing the modulus using whatever valid public key operation you are able to perform.

But in general, this is not an operation that is likely to succeed.