分类: Java
2007-07-30 17:02:19
/** *MessageDigestExample.java *Copyright 2005-2-16 */ import java.security.MessageDigest; /** *单一的消息摘要算法,不使用密码.可以用来对明文消息(如:密码)隐藏保存 */ public class MessageDigestExample{ public static void main(String[] args) throws Exception{ if(args.length!=1){ System.err.println("Usage:java MessageDigestExample text"); System.exit(1); } byte[] plainText=args[0].getBytes("UTF8"); //使用getInstance("算法")来获得消息摘要,这里使用SHA-1的160位算法 MessageDigest messageDigest=MessageDigest.getInstance("SHA-1"); System.out.println("\n"+messageDigest.getProvider().getInfo()); //开始使用算法 messageDigest.update(plainText); System.out.println("\nDigest:"); //输出算法运算结果 System.out.println(new String(messageDigest.digest(),"UTF8")); } } |
/** *PrivateExmaple.java *Copyright 2005-2-16 */ import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import java.security.Key; /** *私鈅加密,保证消息机密性 */ public class PrivateExample{ public static void main(String[] args) throws Exception{ if(args.length!=1){ System.err.println("Usage:java PrivateExample System.exit(1); } byte[] plainText=args[0].getBytes("UTF8"); //通过KeyGenerator形成一个key System.out.println("\nStart generate AES key"); KeyGenerator keyGen=KeyGenerator.getInstance("AES"); keyGen.init(128); Key key=keyGen.generateKey(); System.out.println("Finish generating DES key"); //获得一个私鈅加密类Cipher,ECB是加密方式,PKCS5Padding是填充方法 Cipher cipher=Cipher.getInstance("AES/ECB/PKCS5Padding"); System.out.println("\n"+cipher.getProvider().getInfo()); //使用私鈅加密 System.out.println("\nStart encryption:"); cipher.init(Cipher.ENCRYPT_MODE,key); byte[] cipherText=cipher.doFinal(plainText); System.out.println("Finish encryption:"); System.out.println(new String(cipherText,"UTF8")); System.out.println("\nStart decryption:"); cipher.init(Cipher.DECRYPT_MODE,key); byte[] newPlainText=cipher.doFinal(cipherText); System.out.println("Finish decryption:"); System.out.println(new String(newPlainText,"UTF8")); } } |
/** *PublicExample.java *Copyright 2005-2-16 */ import java.security.Key; import javax.crypto.Cipher; import java.security.KeyPairGenerator; import java.security.KeyPair; /** *一个简单的公鈅加密例子,Cipher类使用KeyPairGenerator生成的公鈅和私鈅 */ public class PublicExample{ public static void main(String[] args) throws Exception{ if(args.length!=1){ System.err.println("Usage:java PublicExample System.exit(1); } byte[] plainText=args[0].getBytes("UTF8"); //构成一个RSA密钥 System.out.println("\nStart generating RSA key"); KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair key=keyGen.generateKeyPair(); System.out.println("Finish generating RSA key"); //获得一个RSA的Cipher类,使用公鈅加密 Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding"); System.out.println("\n"+cipher.getProvider().getInfo()); System.out.println("\nStart encryption"); cipher.init(Cipher.ENCRYPT_MODE,key.getPublic()); byte[] cipherText=cipher.doFinal(plainText); System.out.println("Finish encryption:"); System.out.println(new String(cipherText,"UTF8")); //使用私鈅解密 System.out.println("\nStart decryption"); cipher.init(Cipher.DECRYPT_MODE,key.getPrivate()); byte[] newPlainText=cipher.doFinal(cipherText); System.out.println("Finish decryption:"); System.out.println(new String(newPlainText,"UTF8")); } } |
/** *DigitalSignature2Example.java *Copyright 2005-2-16 */ import java.security.Signature; import java.security.KeyPairGenerator; import java.security.KeyPair; import java.security.SignatureException; /** *数字签名,使用RSA私钥对对消息摘要签名,然后使用公鈅验证 测试 */ public class DigitalSignature2Example{ public static void main(String[] args) throws Exception{ if(args.length!=1){ System.err.println("Usage:java DigitalSignature2Example System.exit(1); } byte[] plainText=args[0].getBytes("UTF8"); //形成RSA公钥对 System.out.println("\nStart generating RSA key"); KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair key=keyGen.generateKeyPair(); System.out.println("Finish generating RSA key"); //使用私鈅签名 Signature sig=Signature.getInstance("SHA1WithRSA"); sig.initSign(key.getPrivate()); sig.update(plainText); byte[] signature=sig.sign(); System.out.println(sig.getProvider().getInfo()); System.out.println("\nSignature:"); System.out.println(new String(signature,"UTF8")); //使用公鈅验证 System.out.println("\nStart signature verification"); sig.initVerify(key.getPublic()); sig.update(plainText); try{ if(sig.verify(signature)){ System.out.println("Signature verified"); }else System.out.println("Signature failed"); }catch(SignatureException e){ System.out.println("Signature failed"); } } } |
|
|||