Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEM to PublicKey in Android

I've seen a number of similar questions, but nothing has quite worked for me. I am simply trying to convert an RSA public key that's in PEM format that I've retrieved from a server into a PublicKeyin Android. Can anyone point me in the right direction?

EDIT: I've successfully used the following code to convert the PEM into a PublicKey, but upon encoding a message, I get unexpected output...

 public PublicKey getFromString(String keystr) throws Exception
    {
        // Remove the first and last lines

        String pubKeyPEM = keystr.replace("-----BEGIN PUBLIC KEY-----\n", "");
        pubKeyPEM = pubKeyPEM.replace("-----END PUBLIC KEY-----", "");

        // Base64 decode the data

        byte [] encoded = Base64.decode(pubKeyPEM);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey pubkey = kf.generatePublic(keySpec);

        return pubkey;
    }

    public String RSAEncrypt(final String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException, IOException {

        if (pubKey!=null) {
            cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            encryptedBytes = cipher.doFinal(plain.getBytes());
            Log.d("BYTES", new String(encryptedBytes));
            return Hex.encodeHexString(encryptedBytes);
        }
        else
            return null;
    }

The output looks like this:

b6813f8791d67c0fa82890d005c8ff554b57143b752b34784ad271ec01bfaa9a6a31e7ae08444baef1585a6f78f3f848eecb1706bf7b2868fccefc9d728c30480f3aabc9ac5c3a9b4b3c74c2f7d6f0da235234953ea24b644112e04a2ec619f6bf95306ef30563c4608ec4b53ed7c15736d5f79c7fa1e35f2444beb366ae4c71

when I expect something closer to:

JfoSJGo1qELUbpzH8d4QXtafup+J2F9wLxHCop00BQ4YS0cRdRCKDfHpFPZQYjNeyQj00HwHbz+vj8haTPbpdqT94AHAl+VZ+TPAiUw1U5EXLLyy4tzbmfVI7CwvMm26lwB4REzYUZdedha1caxMEfxQ5duB+x4ol9eRZM/savg=

Is there some formatting or file type that I'm missing?

like image 488
cph2117 Avatar asked Nov 11 '14 20:11

cph2117


People also ask

What is the difference between public and private keys in PEM?

The public key is used to encrypt the message while only the owner of the private key can decrypt the message. In this tutorial, we’re going to see how to read public and private keys from a PEM file.

How to convert ssh-keygen public key to PEM format?

In case you are using a ssh-keygen public key to connect to your server/VPS, and want to use it to start a server setup using ClusterCS, you will need to convert to a PEM format. First, make sure you have a file that contains only your key, let’s say it’s called server.pub. Afterwards run the following command: ssh-keygen -f server.pub -e -m pem

How do I encrypt public and private keys for embedded devices?

Isolate the public keys and store them where you want. Store the private key in the secure store. Encrypt the client public key with the embedded dev public key so you can send it home.

What kind of data is encoded in a pem file?

PEM may also encode other kinds of data such as public/private keys and certificate requests. A PEM file also contains a header and a footer describing the type of encoded data: -----BEGIN PUBLIC KEY----- ...Base64 encoding of the DER encoded certificate...


1 Answers

To answer my own question...The first output is in hex and the second output is in base 64. Just change the return statement to return new String(Base64.encode(encryptedBytes)); and you'll be good!

like image 92
cph2117 Avatar answered Oct 10 '22 07:10

cph2117