1.項目中引入jar包
javabase64-1.3.1.jar
2.如果是springboot項目,若在maven中不知道遠程路徑,可以按照如下方式操作;
2.1可以直接下載jar,
2.2在項目的src平級目錄下創建一個lib backage
2.3 在pom.xml
<dependency> <groupId>it.sauronsoftware.base64</groupId> <artifactId>base64</artifactId> <version>1.3.1</version> <scope>system</scope> <systemPath>${basedir}/lib/javabase64-1.3.1.jar</systemPath> </dependency>
這樣就可以引入了。
3.轉入正題,我們來講加密工具類
AESUtils
package com.foxconn.oem.common.utils.safe; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.*; import java.security.Key; import java.security.SecureRandom; /** * <p> * AES加密解密工具包 * </p> * * @author F3851884 XHB * @date 2012-5-18 * @version 1.0 */ public class AESUtils { public static final String ALGORITHM = "AES"; public static final int KEY_SIZE = 128; public static final int CACHE_SIZE = 1024; public static final String CODE = "utf-8"; /** * <p> * 生成随机密钥 * </p> * * @return * @throws Exception */ public static String getSecretKey() throws Exception { return getSecretKey(null); } /** * <p> * 生成密钥 * </p> * * @param seed 密钥种子 * @return * @throws Exception */ public static String getSecretKey(String seed) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM); SecureRandom secureRandom; if (seed != null && !"".equals(seed)) { //此种写法Windows上正常,Linux上异常 //secureRandom = new SecureRandom(seed.getBytes()); secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(seed.getBytes()); } else { secureRandom = new SecureRandom(); } keyGenerator.init(KEY_SIZE, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); return Base64Utils.encode(secretKey.getEncoded()); } /** * <p> * 加密 * </p> * * @param key * @return * @throws Exception */ public static String encrypt(String content, String key) throws Exception { if(content==null) content=""; byte[] byteContent = content.getBytes(CODE); Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); byte[] result =cipher.doFinal(byteContent); //通过Base64转码返回 return Base64Utils.encode(result); } /** * <p> * 文件加密 * </p> * * @param key * @param sourceFilePath * @param destFilePath * @throws Exception */ public static void encryptFile(String key, String sourceFilePath, String destFilePath) throws Exception { InputStream in = null; OutputStream out = null; CipherInputStream cin = null; try { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); in = new FileInputStream(sourceFile); out = new FileOutputStream(destFile); Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); cin = new CipherInputStream(in, cipher); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = cin.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } } }catch (Exception e){ e.printStackTrace(); }finally { if(out != null){ try { out.close(); }catch (Exception e){ } } if(cin != null){ try { cin.close(); }catch (Exception e){ } } if(in != null){ try { in.close(); }catch (Exception e){ } } } } /** * <p> * 解密 * </p> * * @param key * @return * @throws Exception */ public static String decrypt(String content, String key) throws Exception { Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); byte[] result = cipher.doFinal(Base64Utils.decode(content)); return new String(result, CODE); } /** * <p> * 文件解密 * </p> * * @param key * @param sourceFilePath * @param destFilePath * @throws Exception */ public static void decryptFile(String key, String sourceFilePath, String destFilePath) throws Exception { FileInputStream in = null; FileOutputStream out = null; CipherOutputStream cout = null; try { File sourceFile = new File(sourceFilePath); File destFile = new File(destFilePath); if (sourceFile.exists() && sourceFile.isFile()) { if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); in = new FileInputStream(sourceFile); out = new FileOutputStream(destFile); Key k = toKey(Base64Utils.decode(key)); byte[] raw = k.getEncoded(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKeySpec); cout = new CipherOutputStream(out, cipher); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { cout.write(cache, 0, nRead); cout.flush(); } } }catch (Exception e){ e.printStackTrace(); }finally { if(cout != null){ try { cout.close(); }catch (Exception e){ } } if(out != null){ try { out.close(); }catch (Exception e){ } } if(in != null){ try { in.close(); }catch (Exception e){ } } } } /** * <p> * 转换密钥 * </p> * * @param key * @return * @throws Exception */ public static Key toKey(byte[] key) throws Exception { SecretKey secretKey = new SecretKeySpec(key, ALGORITHM); return secretKey; } }
3.1
package com.foxconn.oem.common.utils.safe; import it.sauronsoftware.base64.Base64; import java.io.*; /** * <p> * BASE64编码解码工具包 * </p> * <p> * 依赖javabase64-1.3.1.jar * </p> * * @author F3851884 XHB * @date 2012-5-19 * @version 1.0 */ public class Base64Utils { /** * 文件读取缓冲区大小 */ private static final int CACHE_SIZE = 1024; /** * <p> * BASE64字符串解码为二进制数据 * </p> * * @param base64 * @return * @throws Exception */ public static byte[] decode(String base64) throws Exception { return Base64.decode(base64.getBytes()); } /** * <p> * 二进制数据编码为BASE64字符串 * </p> * * @param bytes * @return * @throws Exception */ public static String encode(byte[] bytes) throws Exception { return new String(Base64.encode(bytes)); } /** * <p> * 将文件编码为BASE64字符串 * </p> * <p> * 大文件慎用,可能会导致内存溢出 * </p> * * @param filePath 文件绝对路径 * @return * @throws Exception */ public static String encodeFile(String filePath) throws Exception { byte[] bytes = fileToByte(filePath); return encode(bytes); } /** * <p> * BASE64字符串转回文件 * </p> * * @param filePath 文件绝对路径 * @param base64 编码字符串 * @throws Exception */ public static void decodeToFile(String filePath, String base64) throws Exception { byte[] bytes = decode(base64); byteArrayToFile(bytes, filePath); } /** * <p> * 文件转换为二进制数组 * </p> * * @param filePath 文件路径 * @return * @throws Exception */ public static byte[] fileToByte(String filePath) throws Exception { byte[] data = new byte[0]; FileInputStream in = null; ByteArrayOutputStream out = null; try { File file = new File(filePath); if (file.exists()) { in = new FileInputStream(file); out = new ByteArrayOutputStream(2048); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } data = out.toByteArray(); } }catch (Exception e){ e.printStackTrace(); }finally { if(out != null){ try { out.close(); }catch (Exception e){ } } if(in != null){ try { in.close(); }catch (Exception e){ } } } return data; } /** * <p> * 二进制数据写文件 * </p> * * @param bytes 二进制数据 * @param filePath 文件生成目录 */ public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { InputStream in = null; OutputStream out = null; try { in = new ByteArrayInputStream(bytes); File destFile = new File(filePath); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); } destFile.createNewFile(); out = new FileOutputStream(destFile); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } }catch (Exception e){ e.printStackTrace(); }finally { if(out != null){ try { out.close(); }catch (Exception e){ } } if(in != null){ try { in.close(); }catch (Exception e){ } } } } }