互联网虽然用起来方便,但是很容易信息泄露,被些别有用心的人攻击,为了保护网络的安全,双向加密算法例如DES,AES,信息摘要MD5,SHA应运而生。
java在jdk的包中也支持相关的加解密算法,底层通过spi扩展机制,加载不同的provider。
其中有几个比较关键的类
- KeyGenerator :提供对称密钥生成器的功能,支持各种算法
- SecretKey:责保存对称密钥
- Cipher:负责完成加密或解密工作
package com.cipher;/*
@author
@time 23:02
*/
import org.junit.Test;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class CipherTest {
@Test
public void DESTest() throws Exception {
//对称加密生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
//keyGenerator.
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("DES");
String str="hello DES";
//加密
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] encryptStr = cipher.doFinal(str.getBytes());
String hexStr = EncryptUtil.parseByte2HexStr(encryptStr);
System.out.println(hexStr);
//解密
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] decryptorByte = cipher.doFinal(parseHexStr2Byte(hexStr));
System.out.println(new String(decryptorByte));
}
@Test
public void AESTest() throws Exception {
//对称加密生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
//keyGenerator.
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("AES");
String str="hello AES";
//加密
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] encryptStr = cipher.doFinal(str.getBytes());
String hexStr = parseByte2HexStr(encryptStr);
System.out.println(hexStr);
//解密
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] decryptorByte = cipher.doFinal(EncryptUtil.parseHexStr2Byte2(hexStr));
System.out.println(new String(decryptorByte));
}
//指定key加解密
@Test
public void AESByIndicateKeyTest() throws Exception {
//对称加密生成器
String key = "AESKEY";
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(new SecureRandom(key.getBytes("utf-8")));
//keyGenerator.
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance("AES");
String str="hello AES";
//加密
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] encryptStr = cipher.doFinal(str.getBytes());
String hexStr = parseByte2HexStr(encryptStr);
System.out.println(hexStr);
//使用特定的key
KeyGenerator keyGenerator2 = KeyGenerator.getInstance("AES");
keyGenerator2.init(new SecureRandom(key.getBytes("utf-8")));
//keyGenerator.
SecretKey secretKey2 = keyGenerator2.generateKey();
//解密
cipher.init(Cipher.DECRYPT_MODE,secretKey2);
byte[] decryptorByte = cipher.doFinal(EncryptUtil.parseHexStr2Byte2(hexStr));
System.out.println(new String(decryptorByte));
}
@Test
public void md5Test() throws NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
String str = "hello md5";
byte[] result = messageDigest.digest(str.getBytes());
System.out.println(new String(Base64.getEncoder().encode(result)));
}
/**将二进制转换成16进制 */
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**将16进制转换为二进制*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int temp = Integer.parseInt(hexStr.substring(i*2, i*2+2), 16);
result[i] = (byte) (temp);
}
return result;
}
}
官方文档:
https://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#KeyGenerator