How can I construct a java.security.PublicKey object from a base64 encoded string?

后端 未结 5 1865
生来不讨喜
生来不讨喜 2020-11-30 02:40

I have a bse64encoded string Public key from external source (Android Store) and I need to use it to verify signed content. How can I convert the string into an instance of

5条回答
  •  悲哀的现实
    2020-11-30 02:46

    Code for the above answer

    public static PublicKey getKey(String key){
        try{
            byte[] byteKey = Base64.decode(key.getBytes(), Base64.DEFAULT);
            X509EncodedKeySpec X509publicKey = new X509EncodedKeySpec(byteKey);
            KeyFactory kf = KeyFactory.getInstance("RSA");
    
            return kf.generatePublic(X509publicKey);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    
        return null;
    }
    

提交回复
热议问题