对文件、字符进行加密、解密

创建一个类,使用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");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值