Re: One more question....

Gigi Ankeny (Gigi.Ankeny@Eng)
Tue, 18 Nov 1997 18:02:35 -0800 (PST)

Date: Tue, 18 Nov 1997 18:02:35 -0800 (PST)
From: Gigi Ankeny <Gigi.Ankeny@Eng>
Subject: Re: One more question....
To: avolio@unisys.com.br

Avolio,

The print out you have seen is the correct result. However, note that
the signature is actually 48 bytes, which simply given to System.out.printl=
n
will not show you the exact output, but some chopped off translation to som=
e
printable characters.

I have amended your code to include a hexDump function which will show you
the hex code of the signature bytes and some formatting, typo correction. =
=20

Hope that helps,

Gigi Ankeny

> Again thank you for your quick answer about javakey.
> I have a doubt about signatures: I'm running a program that makes a
> signature of a text file, verifies it against a public key and prints
> the result of check and the signature. But what ever file I use I get
> the same value for the signature. Is that correct? It happens even
> using keys from another signer.
>=20
> Heres the code:
>=20
> import java.io.*;
> import java.util.*;
> import java.security.*;
>=20
> public class SigFile
> {
> public static main(String argv[])
> {
> IdentityScope ss =3D IdentityScope.getSystemScope();
> Signer sig =3D (Signer) ss.getIdentity("avoli");
> PrivateKey priv =3D sig.getPrivateKey();
> PublicKey pub =3D sig.getPublicKey();
> try
> {
> Signature signature =3D Signature.getInstance("DSA");
> signature.initSign(priv);
> File file =3D new File("test.txt");
> FileInputStream fis =3D new FileInputStream(file);
> while (fis.available() !=3D 0)
> {
> signature.update((byte) fis.read());
> }
> byte[] realSignature =3D signature.sign();
> System.out.println(realSignature);
> signature.initVerify(pub);
> File f =3D new File("test.txt");
> FileInputStream fx =3D new FileInputStream(f);
> while (fx.available() !=3D 0)
> {
> signature.update((byte) fx.read());
> } =20
> if (signature.Verify(realSignature))
> {
> System.out.println("Signature checks out");
> }
> else
> {
> System.out.println("Signature does not check out");
> }
> }
> catch (Exception e)
> {
> System.err.println(e);
> }
> }
> } =20
>=20
> TIA
> --=20
> Cl=E1udio Avolio Rodrigues
> Systems Especialist
> IBM GS
> E-mail: avolio@unisys.com.br
> Home page: http://www.geocities.com/SunsetStrip/4994

import java.io.*;
import java.util.*;
import java.security.*;

public class Avolio {

public static void main(String argv[]) {

IdentityScope ss =3D IdentityScope.getSystemScope();
Signer sig =3D (Signer) ss.getIdentity("avolio");
PrivateKey priv =3D sig.getPrivateKey();
PublicKey pub =3D sig.getPublicKey();
try {
=09 Signature signature =3D Signature.getInstance("DSA");
=09 signature.initSign(priv);
=09 File file =3D new File("test.txt");
=09 FileInputStream fis =3D new FileInputStream(file);
=09 while (fis.available() !=3D 0) {
=09=09 signature.update((byte) fis.read());
=09 }
=09 byte[] realSignature =3D signature.sign();
=09 System.out.println(realSignature);
=09 System.out.println(hexDump(realSignature));
=09 signature.initVerify(pub);
=09 File f =3D new File("test.txt");
=09 FileInputStream fx =3D new FileInputStream(f);
=09 while (fx.available() !=3D 0) {
=09 signature.update((byte) fx.read());
=09 } =20
=09 if (signature.verify(realSignature)) {
=09 System.out.println("Signature checks out");
=09 }
=09 else {
=09 System.out.println("Signature does not check out");
=09 }
}
catch (Exception e) {
=09 System.err.println(e);
}
}=20
=20
private static final String digits =3D "0123456789abcdef";
=20
public static String hexDump(byte[] bytes) {
=09
StringBuffer buf =3D new StringBuffer (bytes.length * 2);
int i;
=09
buf.append (" "); // four spaces
for (i =3D 0; i < bytes.length; i++) {
buf.append (digits.charAt ((bytes[i] >> 4) & 0x0f));
buf.append (digits.charAt (bytes[i] & 0x0f));
if (((i + 1) % 32) =3D=3D 0) {
if ((i + 1) !=3D bytes.length)
buf.append ("\n "); // line after four words
} else if (((i + 1) % 4) =3D=3D 0)
buf.append (' '); // space between words
}
return buf.toString ();
}

=20
} =20