最近看到公司一个以前项目的MD5加密方式,让我对JAVA加密感觉到一些兴趣,但是公司的MD5加密是自己写的一个MD5工具类算法类进行加密。但窝了解到一些其他的加密方式。
一、DES加密
DDES(Data Encryption Standar 数据加密标准算法)是一种常见的对称密钥加密算法,加密者使用密钥对原文加密,解密者必须使用相同的密钥才能对密文进行解密。
重点在KeyGenerator、Cipher、和Key类的使用
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; public class Test2 { /** * 生成密钥 */ private void secretKey(){ String fileName = "d:/encry/密钥.dat"; try { KeyGenerator keyGen = KeyGenerator.getInstance("DES"); keyGen.init(56); Key key = keyGen.generateKey(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName)); out.writeObject(key); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 加密 */ private void encrypt(){ String fileName = "d:/encry/密钥.dat"; try { //读取密钥 ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName)); Key key = (Key) in.readObject(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); //加密 byte[] cipherText = cipher.doFinal("aaaaasda".getBytes("UTF-8")); //保存加密文件 FileOutputStream fileOutputStream = new FileOutputStream("d:/encry/encrypt.txt",false); fileOutputStream.write(cipherText); fileOutputStream.close(); }catch (Exception e) { e.printStackTrace(); } } /** * 解密 */ private void decrypt(){ try { //读取加密文件 FileInputStream fi = new FileInputStream("d:/encryS/encrypt.txt"); byte[] cipherText = new byte[fi.available()]; fi.read(cipherText); fi.close(); //读取密钥 ObjectInputStream in = new ObjectInputStream(new FileInputStream("d:/encry/密钥.dat")); Key key = (Key) in.readObject(); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] text = cipher.doFinal(cipherText); System.out.println(new String(cipherText,"UTF-8")); } catch (Exception e) { e.printStackTrace(); } } }
二、RSA加密(不对称密钥加密,加密和解密的密钥是不一样的,常见的方法是用户生成一对密钥,将公钥发送给别人,别人使用公钥加密,此时密文只能被用户的另一个私钥解密,有效解决了密钥传递可能产生的密钥泄露问题)
注意:KeyPairGenerator、Cipher、KeyPair类的使用
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.Cipher; public class Rsa { /** * 生成密文 */ private void secretKey(){ KeyPairGenerator keyPairGenerator; try { keyPairGenerator = KeyPairGenerator.getInstance("RSA"); //采用1024位 keyPairGenerator.initialize(1024); KeyPair key = keyPairGenerator.generateKeyPair(); PublicKey publicKey = key.getPublic(); PrivateKey privateKey = key.getPrivate(); ObjectOutputStream ot = new ObjectOutputStream(new FileOutputStream("d:test/私钥.dat")); ot.writeObject(privateKey); ot.close(); ot = new ObjectOutputStream(new FileOutputStream("d:test/公钥.dat")); ot.writeObject(privateKey); ot.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 加密 */ private void crypt(){ try { ObjectInputStream os = new ObjectInputStream(new FileInputStream("d:/test/公钥.dat")); PublicKey publicKey = (PublicKey) os.readObject(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] cipherTxt = cipher.doFinal("test".getBytes("UTF-8")); FileOutputStream fileOutputStream = new FileOutputStream("d:/test/密文.txt",false); fileOutputStream.write(cipherTxt); fileOutputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 解密 */ private void deCrypt(){ try { FileInputStream fileInputStream = new FileInputStream("d:/test/密文.txt"); byte[]cipherText = new byte[fileInputStream.available()]; fileInputStream.read(cipherText); fileInputStream.close(); ObjectInputStream fi = new ObjectInputStream(new FileInputStream("d:test/私钥.dat")); PrivateKey privateKey = (PrivateKey) fi.readObject(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println(new String(plainText,"UTF-8")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
三、信息验证(MD5),MD5算法是一种常见的消息摘要算法,它能够将原始信息文本生成一个消息摘要值,这个消息摘要值也称为哈希值,只要文本不同,就会得到一个不同的消息摘要,而且几乎不能从消息摘要反向推出原始信息文本。
主要注意:MessageDigest类的使用
四、文件签名(主要是密钥不对称加密算法的另一种应用,主要用于验证文件类容是否被篡改,原理是对原始文件使用私钥加密得到签名文件,验证者使用公钥对签名文件解密,对比原文件)
主要注意:generateKeys、sign和validateSign方法及signature方法