|
RSS Feed - WebSphere MQ Support
|
RSS Feed - Message Broker Support
|
 |
|
Make signature for string (javacompute) |
« View previous topic :: View next topic » |
Author |
Message
|
Vincent Vega |
Posted: Thu Sep 27, 2012 3:41 am Post subject: Make signature for string (javacompute) |
|
|
 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 |
|
 |
lancelotlinc |
Posted: Thu Sep 27, 2012 5:16 am Post subject: |
|
|
 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 |
|
 |
|
|
 |
|
Page 1 of 1 |
|
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
|
|
|
|