ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Make signature for string (javacompute)

Post new topic  Reply to topic
 Make signature for string (javacompute) « View previous topic :: View next topic » 
Author Message
Vincent Vega
PostPosted: Thu Sep 27, 2012 3:41 am    Post subject: Make signature for string (javacompute) Reply with quote

Novice

Joined: 27 Sep 2012
Posts: 18
Location: Kiev, Ukraine

Hello.
I try to make digital sign for some string. I use java compute node for this.
My code (this is simple code only for test):

Code:

…
   String data = "Test message";
   String storename = "/home/esbadmin/keystore/WBRK-Keystore.jks";
   char[] storepass = "123456".toCharArray();
   String alias = "wbrk";
   KeyStore ks = KeyStore.getInstance("JKS");
   ks.load(new FileInputStream(storename), storepass);
   Key key = ks.getKey(alias, storepass);
   if (!(key instanceof PrivateKey)) {
      throw new IllegalArgumentException("key isn't private");
   }
   PrivateKey privKey = (PrivateKey) key;
   System.out.println("Private Key: " + Utilities.bytesToHex((privKey.getEncoded())));
   Signature signature = Signature.getInstance("SHA1withRSA");
   signature.initSign(privKey);
   byte[] bytes = data.getBytes("UTF-8");
   signature.update(bytes, 3, bytes.length - 3);
   byte[] sign = signature.sign();
   String s = Utilities.bytesToHex(sign);
System.out.println("Signature: "+ s);
…


Output this code:

Code:

Private Key: 30820…D86ED
Signature: 6132C…358B2


But signature is wrong. The system which received this signature can’t verify it.
For test I wrote the same code in Eclipse

Code:

...
        String data = "Test Message";
        String storename = "d:\\Vincent\\keystore\\WBRK-Keystore.jks";
   char[] storepass = "123456".toCharArray();
   String alias = "wbrk";
   KeyStore ks = KeyStore.getInstance("JKS");
   ks.load(new FileInputStream(storename), storepass);
   Key key = ks.getKey(alias, storepass);
   if (!(key instanceof PrivateKey)) {
      throw new IllegalArgumentException("key isn't private");
   }
   PrivateKey privKey = (PrivateKey) key;
   System.out.println("Private Key: " + Utilities.bytesToHex((privKey.getEncoded())));
   Signature signature = Signature.getInstance("SHA1withRSA");
   signature.initSign(privKey);
   byte[] bytes = data.getBytes("UTF-8");
   signature.update(bytes, 3, bytes.length - 3);
   byte[] sign = signature.sign();
   String s = Utilities.bytesToHex(sign);
   System.out.println("Signature: "+ s);


Output this code:

Code:

Private Key: 30820…D86ED
Signature: 0BDBF...D1A5C


This signature is correct.

JKS file is the same in broker and in Eclipse.

Why is signature different? Where is mistake?

Message Broker: 7.0.0.1 on AIX
Eclipse jre: 1.7.0_07 on Windows 7 (x64)
Back to top
View user's profile Send private message Visit poster's website
lancelotlinc
PostPosted: Thu Sep 27, 2012 5:16 am    Post subject: Reply with quote

Jedi Knight

Joined: 22 Mar 2010
Posts: 4941
Location: Bloomington, IL USA

It's not just the hash that gets encrypted, it's the ID of the hash algorithm concatenated with the digest, which is why it is different. Also, the underlying crypto library may use random padding for any encryption operation.

You may find better success working out the solution apart from WMB. WMB has no influence on how the code operates. Start fresh with a brand new Java project and make the code perform the successful way. Once you have done this, migrate the code into your WMB project.

By the way, you need to update your toolkit and runtime to at least 7.0.0.4.
_________________
http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » Make signature for string (javacompute)
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.