File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Digital Signature (key) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Digital Signature (key)" Watch "Digital Signature (key)" New topic
Author

Digital Signature (key)

Paddy Totti
Greenhorn

Joined: Dec 12, 2001
Posts: 9
Hi,I have a problem when I doing cryptograpgy.
I am going to use keytool to generate keys.
My problem is the compiler complain that there is no getPrivateKey method in Keytool class. If so, how can I get the privatekey from a keytool object !!??
//code
import java.io.*;
import java.security.*;
public class SignatureTest {
public static void main(String[] args) throws Exception {
if (args.length != 6) {
System.out.println(
"Usage: SignatureTest -s|-v keystore storepass alias " +
"messagefile signaturefile");
return;
}
String options = args[0];
String keystorefile = args[1];
String storepass = args[2];
String alias = args[3];
String messagefile = args[4];
String signaturefile = args[5];
char[] a = new char[storepass.length()];
for(int i=0;i<storepass.length();i++)>
{
a = storepass.charAt(i);
}
Signature signature = Signature.getInstance("DSA");
KeyStore keystore = KeyStore.getInstance("jks");
keystore.load(new FileInputStream(keystorefile), a);
if (options.indexOf("s") != -1)
signature.initSign(keystore.getPrivateKey(alias, a));
else
signature.initVerify(keystore.getCertificate(alias).getPublicKey());
FileInputStream in = new FileInputStream(messagefile);
byte[] buffer = new byte[8192];
int length;
while ((length = in.read(buffer)) != -1)
signature.update(buffer, 0, length);
in.close();
if (options.indexOf("s") != -1) {
FileOutputStream out = new FileOutputStream(signaturefile);
byte[] raw = signature.sign();
out.write(raw);
out.close();
}
else {
FileInputStream sigIn = new FileInputStream(signaturefile);
byte[] raw = new byte[sigIn.available()];
sigIn.read(raw);
sigIn.close();
if (signature.verify(raw))
System.out.println("The signature is good.");
else
System.out.println("The signature is bad.");
}
}
}
/////////////////////////////////
Result when compile:
SignatureTest.java:32: Method getPrivateKey(java.lang.String, char[]) not found in class java.security.KeyStore.
signature.initSign(keystore.getPrivateKey(alias, a));
^
1 error
Exit code: 1
There were errors
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Paddy,
How about just using getKey method?
Regards,
Manfred.
Paddy Totti
Greenhorn

Joined: Dec 12, 2001
Posts: 9
Thanks for your reply, Manfred.
I have try use this statement:
///
signature.initSign((PrivateKey)keystore.getKey(alias, a));
///
and now there have not complier error.
However, when I run this program with this line in dos/mode:
java SignatureTest -s .keystore tsyfhedr Paddy custorder.xml signaturefile
It throws a exception:
Exception in thread "main" java.security.
UnrecoverableKeyException:Cannot recover key
at sun.security.provider.KeyProtector.recover(Unknown Source)
at sun.security.provider.JavaKeyStore.engineGetKey(Unknown Source)
at java.security.KeyStore.getKey(Unknown Source)
at Signature1.main(Signature1.java:32)
Of course, I have create all need and the password is right.
Anyone know why the keystore can't be used ???
 
jQuery in Action, 2nd edition
 
subject: Digital Signature (key)