分类: 网络与安全
2011-05-04 16:33: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")); } } |