Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting public key null while converting string to public key

Tags:

java

android

public static PublicKey strToPublicKey(String s) {

    PublicKey pbKey = null;
    try {

        BufferedReader br = new BufferedReader(new StringReader(s));
        PEMReader pr = new PEMReader(br);
        Object obj = pr.readObject();

        if (obj instanceof PublicKey) {
            pbKey = (PublicKey) pr.readObject();
        } else if (obj instanceof KeyPair) {
            KeyPair kp = (KeyPair) pr.readObject();
            pbKey = kp.getPublic();
        }
        pr.close();

    } catch (Exception e) {
        Log.d("CIPHER", e.getMessage());
    }
    return pbKey;
}

this line returns null value

pbKey = (PublicKey) pr.readObject();
    Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC");

when I try to convert server key to rsa public key type it reurns null value at this line

pbKey = (PublicKey) pr.readObject();
like image 536
Vipin Avatar asked Oct 19 '25 10:10

Vipin


1 Answers

Your code doesn't make sense. You've already read the key. The instanceof test proves it. You should not be reading another object: you should be casting the object you already read.

like image 74
user207421 Avatar answered Oct 22 '25 00:10

user207421



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!