import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.util.Base64;
import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; import javax.crypto.spec.PBEParameterSpec;
import org.apache.commons.lang.StringUtils;
public class Mintest {
public static void main(String... args) throws Exception {
Mintest mintest=new Mintest();
String original = "Let’s Build a Giant Airship 青海长云暗雪山";
System.out.println("原文:"+original);
try {
String miWen=mintest.encryption(original);
System.out.println("密文:"+miWen);
String msg=mintest.unEncryption(miWen);
System.out.println("明文:"+msg);
}catch(Exception e) {
e.printStackTrace();
}
}
public String unEncryption(String miwen) throws NoSuchAlgorithmException, InvalidKeySpecException,
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
IllegalBlockSizeException, BadPaddingException {
Key key=getKey();
String realMiWen=StringUtils.substring(miwen,0,StringUtils.lastIndexOf(miwen, "?"));
System.out.println("realMiWen:"+realMiWen);
String salt=StringUtils.substring(miwen,StringUtils.lastIndexOf(miwen, "?")+1);
System.out.println("salt:"+salt);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(Base64.getDecoder().decode(salt), 100);
byte[] bytes = Base64.getDecoder().decode(realMiWen);
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
// 解密
cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpec);
bytes = cipher.doFinal(bytes);
String mingWen=new String(bytes);
System.out.println("解密:"+mingWen);
return mingWen;
}
public String encryption(String message) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException, InvalidKeySpecException {
Key key=getKey();
String salt=getSalt();
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(Base64.getDecoder().decode(salt), 100);
// 加密
Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpec);
byte[] bytes = cipher.doFinal(message.getBytes());
String miwen=Base64.getEncoder().encodeToString(bytes)+"?"+salt;
System.out.println("密文:" + miwen);
return miwen;
}
private String getSalt() {
// 初始化盐
SecureRandom random = new SecureRandom();
byte[] salt = random.generateSeed(8);
return Base64.getEncoder().encodeToString(salt);
}
private Key getKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
// 生产密钥
String password = "haha168";// 口令
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
Key key = factory.generateSecret(pbeKeySpec);// 密钥,下面加密解密都要用到
System.out.println("密钥:" +new String(key.getEncoded()));
return key;
}
}
|