本文主要谈一下密码学中的加密和数字签名,以及其在java中如何进行使用。对密码学有兴趣的伙伴,推荐看bruce schneier的著作:applied crypotography。在jdk1.5的发行版本中安全性方面有了很大的改进,也提供了对rsa算法的直接支持,现在我们从实例入手解决问题(本文仅是作为简单介绍):
一、密码学上常用的概念
1)消息摘要:
这是一种与消息认证码结合使用以确保消息完整性的技术。主要使用单向散列函数算法,可用于检验消息的完整性,和通过散列密码直接以文本形式保存等,目前广泛使用的算法有md4、md5、sha-1,jdk1.5对上面都提供了支持,在java中进行消息摘要很简单, java.security.messagedigest提供了一个简易的操作方法:
| /** *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 <text>"); 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 <text>"); 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")); } } |
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器