File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Digital Signature (key)

Paddy Totti

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 !!??
public class SignatureTest {
public static void main(String[] args) throws Exception {
if (args.length != 6) {
"Usage: SignatureTest -s|-v keystore storepass alias " +
"messagefile signaturefile");
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));
FileInputStream in = new FileInputStream(messagefile);
byte[] buffer = new byte[8192];
int length;
while ((length = != -1)
signature.update(buffer, 0, length);
if (options.indexOf("s") != -1) {
FileOutputStream out = new FileOutputStream(signaturefile);
byte[] raw = signature.sign();
else {
FileInputStream sigIn = new FileInputStream(signaturefile);
byte[] raw = new byte[sigIn.available()];;
if (signature.verify(raw))
System.out.println("The signature is good.");
System.out.println("The signature is bad.");
Result when compile: Method getPrivateKey(java.lang.String, char[]) not found in class
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?
Paddy Totti

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"
UnrecoverableKeyException:Cannot recover key
at Source)
at Source)
at Source)
at Signature1.main(
Of course, I have create all need and the password is right.
Anyone know why the keystore can't be used ???
I agree. Here's the link:
subject: Digital Signature (key)
It's not a secret anymore!