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

  • Home
  • SEARCH
  • 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 8184385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:29:03+00:00 2026-06-07T01:29:03+00:00

Can you help me to find a simple tutorial of how sign a string

  • 0

Can you help me to find a simple tutorial of how sign a string using ECDSA algorithm in java. But without using any third-party libraries like bouncycastle. Just JDK 7. I found it difficult to search a simple example, I’m new to cryptography.


import java.io.*;
import java.security.*;

public class GenSig {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        /*
         * Generate a DSA signature
         */

        try {

            /*
             * Generate a key pair
             */

            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");

            keyGen.initialize(1024, random);

            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey priv = pair.getPrivate();
            PublicKey pub = pair.getPublic();

            /*
             * Create a Signature object and initialize it with the private key
             */

            Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");

            dsa.initSign(priv);

            String str = "This is string to sign";
            byte[] strByte = str.getBytes();
            dsa.update(strByte);

            /*
             * Now that all the data to be signed has been read in, generate a
             * signature for it
             */

            byte[] realSig = dsa.sign();
            System.out.println("Signature: " + new String(realSig));


        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }
    }
}

How to modify it for ECDSA?

  • 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-07T01:29:04+00:00Added an answer on June 7, 2026 at 1:29 am

    Here is small example based on your example.

    NOTE: this is the original code for this answer, please see the next code snippet for an updated version.

    import java.math.BigInteger;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.SecureRandom;
    import java.security.Signature;
    
    public class ECDSAExample {
    
        public static void main(String[] args) throws Exception {
            /*
             * Generate an ECDSA signature
             */
    
            /*
             * Generate a key pair
             */
    
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    
            keyGen.initialize(256, random);
    
            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey priv = pair.getPrivate();
            PublicKey pub = pair.getPublic();
    
            /*
             * Create a Signature object and initialize it with the private key
             */
    
            Signature dsa = Signature.getInstance("SHA1withECDSA");
    
            dsa.initSign(priv);
    
            String str = "This is string to sign";
            byte[] strByte = str.getBytes("UTF-8");
            dsa.update(strByte);
    
            /*
             * Now that all the data to be signed has been read in, generate a
             * signature for it
             */
    
            byte[] realSig = dsa.sign();
            System.out.println("Signature: " + new BigInteger(1, realSig).toString(16));
    
        }
    }
    

    UPDATE: Here is slightly improved example removing deprecated algorithms. It also explicitly requests the NIST P-256 curve using the SECG notation "secp256r1" as specified in RFC 8422.

    import java.math.BigInteger;
    import java.security.*;
    import java.security.spec.ECGenParameterSpec;
    
    public class ECDSAExample {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
            /*
             * Generate an ECDSA signature
             */
    
            /*
             * Generate a key pair
             */
    
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
    
            keyGen.initialize(new ECGenParameterSpec("secp256r1"), new SecureRandom());
    
            KeyPair pair = keyGen.generateKeyPair();
            PrivateKey priv = pair.getPrivate();
            PublicKey pub = pair.getPublic();
    
            /*
             * Create a Signature object and initialize it with the private key
             */
    
            Signature ecdsa = Signature.getInstance("SHA256withECDSA");
    
            ecdsa.initSign(priv);
    
            String str = "This is string to sign";
            byte[] strByte = str.getBytes("UTF-8");
            ecdsa.update(strByte);
    
            /*
             * Now that all the data to be signed has been read in, generate a
             * signature for it
             */
    
            byte[] realSig = ecdsa.sign();
            System.out.println("Signature: " + new BigInteger(1, realSig).toString(16));
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to do a simple GUI application in VC++ but can't find any
Perhaps you can help me find this in the docs. I'm using pound-quote to
I'm building my first autoconf managed package. However I can't find any simple examples
Can someone help me find the error when I tried to display the list
Can anyone help me find an up-to-date, working ATL project which has a main
Can someone help me to find a solution on how to calculate a cubic
Can someone help me to find web-page or resource where can I see what
Can you help me to find trouble with my code? It has to delete
Hoping that someone can help clear up this very annoying situation I find myself
Where do I find a good CSS editor that can help me use all

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.