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 8310781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:35:07+00:00 2026-06-08T19:35:07+00:00

I have an external service which call me back after some defined event, and

  • 0

I have an external service which call me back after some defined event, and sign his request with its private key.

I have stored the public key which look like :

-----BEGIN PUBLIC KEY-----
........................................
-----END PUBLIC KEY-----

So my work is to check if request’s content has not been alterned by verifying signature.

Here is my algorithm :

// 1 - reading public key :
Scanner scanner = new Scanner( new File( keyPath ) );


//            encodedPublicKey.toString( );
StringBuilder sb = new StringBuilder( );
while ( scanner.hasNextLine( ) )
{
    sb.append( scanner.nextLine( ) );
    sb.append( '\n' );
}

byte[] encodedPublicKey = sb.toString( ).getBytes( "utf-8" );

// 2 - loading public key in a relevant object :
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec( publicKeyBytes );

KeyFactory keyFactory = KeyFactory.getInstance( "DSA" );

PublicKey publicKey = keyFactory.generatePublic( publicKeySpec );

// 3 - verifying content with signature and content :
Signature sig = Signature.getInstance( "SHA1withDSA" );
sig.initVerify( publicKey );
sig.update( message.getBytes( ) );
ret = sig.verify( sign.getBytes( ) );

But for now my algorithm is stoped at “PublicKey publicKey = keyFactory.generatePublic( publicKeySpec )” step by this message :

java.security.spec.InvalidKeySpecException: Inappropriate key specification: invalid key format

So how can I load my key in a way that is accepted by java api ?

  • 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-08T19:35:10+00:00Added an answer on June 8, 2026 at 7:35 pm

    Actually I’ve found the solution.

    The problem was to load in the public key file in the right way.

    I’va added bouncycastle library to my dependencies :

    <dependency>
      <groupId>org.bouncycastle</groupId>
      <artifactId>bcprov-jdk15on</artifactId>
      <version>1.47</version>
    </dependency>
    

    It provides PemReader which allows to read and load non certificated public keys.

    Here is my utility class :

    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.security.InvalidKeyException;
    import java.security.KeyFactory;
    import java.security.NoSuchAlgorithmException;
    import java.security.PublicKey;
    import java.security.Signature;
    import java.security.SignatureException;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.X509EncodedKeySpec;
    
    import org.bouncycastle.util.io.pem.PemReader;
    import org.castor.util.Base64Decoder;
    
    import fr.paris.lutece.portal.service.util.AppLogService;
    
    
    /**
     * Classe d'aide à l'interfacage avec le service paybox.
     *
     * Toutes les informations parameterables sont sous la forme paybox.*
     */
    public final class PayboxUtil
    {
    
        /** The Constant CHARSET. */
        private static final String CHARSET = "utf-8";
    
        /** The Constant ENCRYPTION_ALGORITHM. */
        private static final String ENCRYPTION_ALGORITHM = "RSA";
    
        /** The Constant HASH_ENCRIPTION_ALGORITHM. */
        private static final String HASH_ENCRYPTION_ALGORITHM = "SHA1withRSA";
    
        /**
         * constructeur privé pour classe statique.
         */
        private PayboxUtil(  )
        {
        }
    
        /**
         * Controle si une signature est bien celle du message à l'aide de la clé
         * publique de l'emmeteur?.
         *
         * @param message le message
         * @param sign la signature
         * @param keyPath le chemin vers la clé publique.
         * @return true si la signature est bien celle du message avec la clé privé
         *         attendue.
         */
        public static boolean checkSign( String message, String sign, String keyPath )
        {
            boolean ret = false;
    
            try
            {
                ret = PayboxUtil.verify( message, sign, PayboxUtil.getKey( keyPath ) );
            }
            catch ( final FileNotFoundException e )
            {
                AppLogService.error( e );
            }
            catch ( final IOException e )
            {
                AppLogService.error( e );
            }
            catch ( final NoSuchAlgorithmException e )
            {
                AppLogService.error( e );
            }
            catch ( final InvalidKeySpecException e )
            {
                AppLogService.error( e );
            }
            catch ( final InvalidKeyException e )
            {
                AppLogService.error( e );
            }
            catch ( final SignatureException e )
            {
                AppLogService.error( e );
            }
    
            return ret;
        }
    
    
        /**
         * Récupère la clé publique à partir du chemin passé en paramètre.
         *
         * @param keyPath le chemin vers la clé.
         * @return la clé publique
         * @throws NoSuchAlgorithmException the no such algorithm exception
         * @throws IOException Signals that an I/O exception has occurred.
         * @throws InvalidKeySpecException the invalid key spec exception
         */
        private static PublicKey getKey( String keyPath )
            throws NoSuchAlgorithmException, IOException, InvalidKeySpecException
        {
            final KeyFactory keyFactory = KeyFactory.getInstance( PayboxUtil.ENCRYPTION_ALGORITHM );
            final PemReader reader = new PemReader( new FileReader( keyPath ) );
            final byte[] pubKey = reader.readPemObject(  ).getContent(  );
            final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec( pubKey );
    
            return keyFactory.generatePublic( publicKeySpec );
        }
    
        /**
         * effectue la vérification du message en fonction de la signature et de la
         * clé.
         *
         * @param message le message
         * @param sign la signature
         * @param publicKey la clé publique.
         * @return true, if successful
         * @throws NoSuchAlgorithmException the no such algorithm exception
         * @throws InvalidKeyException the invalid key exception
         * @throws SignatureException the signature exception
         * @throws UnsupportedEncodingException the unsupported encoding exception
         */
        private static boolean verify( String message, String sign, PublicKey publicKey )
            throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException
        {
            final Signature sig = Signature.getInstance( PayboxUtil.HASH_ENCRYPTION_ALGORITHM );
            sig.initVerify( publicKey );
            sig.update( message.getBytes( PayboxUtil.CHARSET ) );
    
            final byte[] bytes = Base64Decoder.decode( URLDecoder.decode( sign, PayboxUtil.CHARSET ) );
    
            return sig.verify( bytes );
        }
    }
    

    You just have to pass signed content, signature and key path to checkSign method and it does all the work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which consults some external stateful service and returns a
I have an assembly which wraps the functionality of an external live web service.
I have a few camel routes that call an external web service, i need
I have a REST web service using spring MVC which is external-facing. I would
I have a C# Windows service which manages some stuff for my server application.
In my application, I have a Service which is responsible for looking after a
We have an external service that continuously sends us data. For the sake of
I have a background thread that handles communication with an external service. Each time
I have a PHP script that grabs data from an external service and saves
I have a problem with deserializing JSON data from an external REST service. Depending

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.