创建一个类,使用AES进行加密、解密
package com.jiami.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtil {
/**
* 加密密码 ,也可以自己定义
*/
public static String PSW="9912634458";
/**
* 加密
* @param pwd
* @param file
* @throws Exception
*/
public static void EncryptFile(String pwd, File file) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytIn = new byte[(int) file.length()];
bis.read(bytIn);
bis.close();
// AES加密
SecretKey skey=GetKey(pwd);
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
// 写文件
byte[] bytOut = cipher.doFinal(bytIn);
File file1 = new File(file.getPath()+".Encrypted");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
bos.write(bytOut);
bos.close();
file.delete();
}
/**
* 生成秘钥
* @param pwd
* @return
* @throws Exception
*/
private static SecretKey GetKey(String pwd) throws Exception{
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom random=SecureRandom.getInstance("SHA1PRNG");
random.setSeed(pwd.getBytes());
kgen.init(128, random);
SecretKey skey = kgen.generateKey();
return skey;
}
/**
* 解密
* @param pwd
* @param file
* @throws Exception
*/
public static void DecryptFile(String pwd, File file) throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
byte[] bytIn = new byte[(int) file.length()];
bis.read(bytIn);
bis.close();
// AES加密
SecretKey skey=GetKey(pwd);
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
// 写文件
byte[] bytOut = cipher.doFinal(bytIn);
File file1 = new File(file.getPath().substring(0,file.getPath().lastIndexOf('.')));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
bos.write(bytOut);
bos.close();
}
public static boolean IsEncrypt(){
String isEncrypt="true";
if(isEncrypt==null||isEncrypt.equals("")){
return false;
}else{
if(isEncrypt.toLowerCase().equals("true")){
return true;
}
else{
return false;
}
}
}
/**
* 加密
* @param pwd
* @param str
* @throws Exception
*/
public static String EncryptStr(String pwd, String str){
// AES加密
try {
SecretKey skey=GetKey(pwd);
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] bytOut = cipher.doFinal(str.getBytes("UTF-8"));
final Base64.Encoder encoder = Base64.getEncoder();
bytOut=encoder.encode(bytOut);
return new String(bytOut);
}catch(Exception e) {
System.out.println("=================加密=========================");
return str;
}
}
/**
* 解密
* @param pwd
* @param str
* @throws Exception
*/
public static String DecryptStr(String pwd, String str){
// AES解密
try {
SecretKey skey=GetKey(pwd);
byte[] raw = skey.getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
final Base64.Decoder decoder = Base64.getDecoder();
byte[] bytOut=decoder.decode(str.getBytes());
bytOut = cipher.doFinal(bytOut);
return new String(bytOut,"UTF-8");
}catch(Exception e) {
System.out.println("=================解密==========================");
return str;
}
}
//测试
public static void main(String[] args) throws Exception{
//加密密码也可以自己定义,下面随便定义,用作测试
String pwd = "zzz12345678";
String str="发射点发射点发生45678";
String a=EncryptStr(pwd,str);
String b=DecryptStr(pwd,a);
System.out.println("加密后:"+a);
System.out.println("解密后:"+b);
System.out.println("加密前:"+str);
}
}
使用方法
//列如:对"12345678"进行加密
AESUtil.EncryptStr(AESUtil.pwd, "12345678");