Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: invalid encoding for signature

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default invalid encoding for signature

    Hi every body!

    I am writing a java code which creates some signature using SHA1 and I want to store the signature in db. I used MYSQL and I put the signature in a column by VARCHAR type. when I fetch the signature to verify it, the 'Verification=signature.verify(Sign);" make an error which is :
    invalid encoding for signature

    Variable type of Sign is byte[]. but I fetch String type from DB. then I converted it to byte[] by :
    Sign=rs.getString("sign").getBytes();

    I think this conversion spoils the signature.What shall I do? How can I make the encoding proper?

    this is the code:

     //create signature
     
            Signature signature = Signature.getInstance("DSA");
            signature.initSign(privkey);
            signature.update(datatoSign);
            byte[] Sign=signature.sign();
     
           mystatment.executeUpdate("UPDATE mytable SET sign = '"+Sign+"' ");
     
    //Now I a will fetch the tuple AND verfiy the signature by public key
     
    SQL="SELECT * FROM notebooks "
    rs=(stat.executeQuery(SQL));
    Sign=rs.getString("sign").getBytes();
     
     
    boolean Verification;
    signature.initVerify(pubkey);
    signature.update(datatoSign);
    Verification=signature.verify(Sign); //--------------this the error generating line


    I am looking to here from you


  2. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: invalid encoding for signature

    You're making it too difficult. Use something like Commons Codec, to base64 encode your string before you save it and base64 decode when you read it out of the database. Converting from byte[] to String between your database and your code can introduce conversion errors based on different character sets - which is what you suspect.

    Creating a base64 encoded String looks something like:

    byte[] theArray = ...;
    String hash = new String( Base64.encodeBase64( theArray ) );
    Need Java help? Check out the HotJoe Java Help forums!

  3. The Following User Says Thank You to stdunbar For This Useful Post:

    Silvery (July 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: invalid encoding for signature

    Yes, you are right. So thanks for your useful comment.

    I changed the code in this form:

     //create signature
     
            Signature signature = Signature.getInstance("DSA");
            signature.initSign(privkey);
            signature.update(datatoSign);
            byte[] Sign=signature.sign();
    //encode the Sign to Base64 
    String MySign=new String (Base64.encode(Sign));
     
           mystatment.executeUpdate("UPDATE mytable SET sign = '"+MySign+"' ");
     
    //Now I a will fetch the tuple AND verfiy the signature by public key
     
    SQL="SELECT * FROM notebooks "
    rs=(stat.executeQuery(SQL));
     
    //decode the fetched signature from DB 
    TheSign=Base64.decode(rs.getString("sign"));
     
    boolean Verification;
    signature.initVerify(pubkey);
    signature.update(datatoSign);
    Verification=signature.verify(TheSign);

Similar Threads

  1. How to capture signature using Canvas and save in png format?
    By aelynne in forum File I/O & Other I/O Streams
    Replies: 28
    Last Post: June 3rd, 2012, 10:43 AM
  2. Why does not forum allow signature?
    By hns1984 in forum The Cafe
    Replies: 0
    Last Post: October 20th, 2011, 11:04 AM
  3. Call with a different signature
    By brajesh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 15th, 2010, 02:05 PM
  4. El Gamal signature -Handle big integer
    By miotvsingtel in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 9th, 2010, 12:05 PM
  5. Problem with Japanese Encoding
    By sendhilpk in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 12th, 2010, 02:57 AM

Tags for this Thread