Convert PHP RSA PublicKey into Android PublicKey

前端 未结 2 1195
失恋的感觉
失恋的感觉 2020-12-16 07:19

I am working on a client server based application.

Where I get PublicKey in this format

\"enter

2条回答
  •  余生分开走
    2020-12-16 07:59

    First you need to generate the public key from the pem format you provided, here is my method for doing this:

    /**
     * 
     * @param PEMString  -A file/string in .pem format with a generated RSA key (with "des3", using "openssl genrsa".)
     * @param isFilePath - If it's a file path or a string
     * @return java.security.PublicKey
     * @throws IOException -No key found
     * @throws NoSuchAlgorithmException 
     * @throws InvalidKeySpecException 
     * 
     * @author hsigmond
     */
    
    private static PublicKey getPublicKeyFromPemFormat(String PEMString,
            boolean isFilePath) throws IOException, NoSuchAlgorithmException,
            InvalidKeySpecException {
    
        BufferedReader pemReader = null;
        if (isFilePath) {
            pemReader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(PEMString)));
        } else {
            pemReader = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(PEMString.getBytes("UTF-8"))));
        }
        StringBuffer content = new StringBuffer();
        String line = null;
        while ((line = pemReader.readLine()) != null) {
            if (line.indexOf("-----BEGIN PUBLIC KEY-----") != -1) {
                while ((line = pemReader.readLine()) != null) {
                    if (line.indexOf("-----END PUBLIC KEY") != -1) {
                        break;
                    }
                    content.append(line.trim());
                }
                break;
            }
        }
        if (line == null) {
            throw new IOException("PUBLIC KEY" + " not found");
        }
    Log.i("PUBLIC KEY: ", "PEM content = : " + content.toString());
    
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(content.toString(), Base64.DEFAULT)));
    
    }
    

    And here is how I use it to read (decode) the content signed with the public key provided.

    /**
     * 
     * @param PEMString  -A file/string in .pem format with a generated RSA key (with "des3", using "openssl genrsa".)
     * @param content
     * @return String value of content Decoded
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws IOException
     * @throws NoSuchProviderException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * 
     * @author hsigmond
     */
    
    
        public static String getContentWithPublicKeyFromPemFormat(String PEMString,
            String content,boolean isFilePath) throws NoSuchAlgorithmException,
            InvalidKeySpecException, IOException, NoSuchProviderException,
            NoSuchPaddingException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
    
        PublicKey publicKey = getPublicKeyFromPemFormat(PEMString,isFilePath);
        if (publicKey != null)
            Log.i("PUBLIC KEY: ", "FORMAT : " + publicKey.getFormat()
                    + " \ntoString : " + publicKey.toString());
    
        byte[] contentBytes = Base64.decode(content, Base64.DEFAULT);
        byte[] decoded = null;
    
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");//BC=BouncyCastle Provider
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        decoded = cipher.doFinal(contentBytes);
        return new String(decoded, "UTF-8");
    }
    

提交回复
热议问题