第一段代码:生成公开/私有密钥对并在命令行中指定文件把密钥对写入该文件 import javasecurity*; import javaio*; public class KeyPairGen { public static void main(String[] args) { if(argslength!=) { Systemoutprintln(Usage: java KeyPairGen KeyFile); Systemexit(); } KeyPairGen obj=new KeyPairGen(); try{ objgen(args[]); }catch(NoSuchAlgorithmException ex) { Systemoutprintln(NoSuchAlgorithmException); } catch(FileNotFoundException ex) { Systemoutprintln(FileNotFoundException); } catch(IOException ex) { Systemoutprintln(IOException); } } public void gen(String source) throws NoSuchAlgorithmException FileNotFoundExceptionIOException { KeyPairGenerator kpGen=KeyPairGeneratorgetInstance(DSA); kpGeninitialize(); KeyPair kPair=kpGengenKeyPair(); FileOutputStream fos=new FileOutputStream(source); ObjectOutputStream oos=new ObjectOutputStream(fos); ooswriteObject(kPair); fosclose(); oosclose(); } } 第二段代码命令行中指定存放密钥的文件用于签名的字符串(这里使用字符串只是为了简单其实在真正实际使用中应该换成用MD或SHA算法计算某一文件流的消息摘要值)和签名所存放的文件功能是计算出签名并把该签名存放在文件中 import javasecurity*; import javaio*; public class SignGen { public static void main(String[] args) { if(argslength!=) { Systemoutprintln(Usage: java SignGen KeyFile String SigFile); Systemexit(); } SignGen obj=new SignGen(); try{ objgenSignature(args[]args[]args[]); }catch(NoSuchAlgorithmException ex) { Systemoutprintln(NoSuchAlgorithmException); } catch(InvalidKeyException ex) { Systemoutprintln(InvalidKeyException); } catch(SignatureException ex) { Systemoutprintln(SignatureException); } catch(ClassNotFoundException ex) { Systemoutprintln(ClassNotFoundException); } catch(FileNotFoundException ex) { Systemoutprintln(FileNotFoundException); } catch(IOException ex) { Systemoutprintln(IOException); } } public void genSignature(String keyFileString strString sigFile) throws NoSuchAlgorithmExceptionInvalidKeyExceptionSignatureException ClassNotFoundExceptionFileNotFoundExceptionIOException { FileInputStream fis=new FileInputStream(keyFile); ObjectInputStream ois=new ObjectInputStream(fis); KeyPair kp=(KeyPair)oisreadObject(); PublicKey pubKey=kpgetPublic(); PrivateKey priKey=kpgetPrivate(); fisclose(); oisclose(); Signature sig=SignaturegetInstance(SHAWithDSA); siginitSign(priKey); sigupdate(strgetBytes()); byte[] b=sigsign(); FileOutputStream fos=new FileOutputStream(sigFile); ObjectOutputStream oos=new ObjectOutputStream(fos); ooswriteObject(b); fosclose(); oosclose(); } } 第三段代码当然是用于验证签名了命令行中指定三个参数密钥文件更新验证的字符串和签名文件 import javasecurity*; import javaio*; public class SignVerify { public static void main(String[] args) { if(argslength!=) { Systemoutprintln(Usage: java SignVerify KeyFile String SigFile); Systemexit(); } SignVerify obj=new SignVerify(); try{ objverify(args[]args[]args[]); }catch(NoSuchAlgorithmException ex) { Systemoutprintln(NoSuchAlgorithmException); } catch(InvalidKeyException ex) { Systemoutprintln(InvalidKeyException); } catch(SignatureException ex) { Systemoutprintln(SignatureException); } catch(ClassNotFoundException ex) { Systemoutprintln(ClassNotFoundException); } catch(FileNotFoundException ex) { Systemoutprintln(FileNotFoundException); } catch(IOException ex) { Systemoutprintln(IOException); } } public void verify(String keyFileString strString sigFile) throws NoSuchAlgorithmExceptionInvalidKeyExceptionSignatureException ClassNotFoundExceptionFileNotFoundExceptionIOException { FileInputStream fis=new FileInputStream(keyFile); ObjectInputStream ois=new ObjectInputStream(fis); KeyPair kp=(KeyPair)oisreadObject(); PublicKey pubKey=kpgetPublic(); PrivateKey priKey=kpgetPrivate(); fisclose(); oisclose(); FileInputStream fis=new FileInputStream(sigFile); ObjectInputStream ois=new ObjectInputStream(fis); byte[] b=(byte[])oisreadObject(); fisclose(); oisclose(); Signature sig=SignaturegetInstance(SHAWithDSA); siginitVerify(pubKey); sigupdate(strgetBytes()); if(sigverify(b)) { Systemoutprintln(Verify OK!); } else { Systemoutprintln(Verify Error!); } } } 在验证过程中密钥对字符串和签名一个都不能错否则无法通过验证 |