Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:58:57+00:00 2026-06-10T02:58:57+00:00

I am working on a client server based application. Where I get PublicKey in

  • 0

I am working on a client server based application.

Where I get PublicKey in this format

enter image description here

as I saved it into String.

Now I want to use this key in my Android(Java code), how can I use this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:58:59+00:00Added an answer on June 10, 2026 at 2:58 am

    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");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in the WAP(Wireless Application Protocol) based application. I want to use
I'm currently working on a client/server based Java-program for a customer. I googled a
I am working on a client/server application and communication between the two is encrypted.
I'm working on a client-server Android application and trying to figure out how to
I am working on a client server system, and am running into issues where
I'm working on a client-server application (.NET 4, WCF) that must support backwards compatibility.
I am working on a .net rich client server application, the client call the
I have a WCF service as part of a client/server application I am working
I'm working an OCA application based on a SQL server db and a SQL
I am working on a client-server application that uses boost::serialization library for it's serialization

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.