jsp新代码第225课

new225.java

package pack03;

import java.security.Key;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;


public class new225
{
	Key key1;
	
	public new225()
	{
		
	}
	
	public new225(String str)
	{
		setKey(str);
	}
	
	public Key getKey()
	{
		return key1;
	}
	
	public void setKey(Key key)
	{
		this.key1 = key;
	}
	
	public void setKey(String str)
	{
		try
		{
			KeyGenerator _geGenerator = KeyGenerator.getInstance("DES");
			_geGenerator.init(new SecureRandom(str.getBytes()));
			this.key1 = _geGenerator.generateKey();
			_geGenerator = null;
			
		} 
		catch (Exception e)
		{
			throw new RuntimeException("Error initalizing sqlmap class cause:" + e);
		}
	}
	
	public String encryptStr(String strMing)
	{
		byte[] byteMi = null;
		byte[] byteMing = null;
		String strMi = "";
		
		Encoder encoder = Base64.getEncoder();
		
		try
		{
			byteMing = strMing.getBytes("utf8");
			byteMi = this.encryptByte(byteMing);
			strMi = encoder.encodeToString(byteMi);
		} 
		catch (Exception e)
		{
			throw new RuntimeException("Error initializing sqlmap class cause" + e);
		}
		finally
		{
			encoder = null;
			byteMing = null;
			byteMi = null;
		}
		
		return strMi;
	}

	private byte[] encryptByte(byte[] byteMing)
	{
		byte[] byteFina = null;
		Cipher cipher;
		try
		{
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.ENCRYPT_MODE, key1);
			byteFina = cipher.doFinal(byteMing);
		} 
		catch (Exception e)
		{
			throw new RuntimeException("Error initializing sqlmap class cause" + e);
		}
		finally
		{
			cipher = null;
		}
		return byteFina;
	}
	
	public String decryptStr(String strMi)
	{
		Decoder decoder = Base64.getDecoder();
		
		byte[] byteMing = null;
		byte[] byteMi = null;
		String strMing = "";
		
		try
		{
			byteMi = decoder.decode(strMi);
			byteMing = this.decryptByte(byteMi);
			strMing = new String(byteMing,"utf8");
			
		} 
		catch (Exception e)
		{
			throw new RuntimeException("Error initializing sqlmap class cause:" + e);
		}
		finally
		{
			decoder = null;
			byteMing = null;
			byteMi = null;
		}
		
		return strMing;
	}

	private byte[] decryptByte(byte[] byteMi)
	{
		Cipher cipher;
		byte[] byteFina = null;
		try
		{
			cipher = Cipher.getInstance("DES");
			cipher.init(Cipher.DECRYPT_MODE, key1);
			byteFina = cipher.doFinal(byteMi);

		}
		catch (Exception e)
		{
			throw new RuntimeException("Error initializing sqlmap class cause:" + e);
		}
		finally
		{
			cipher = null;
			
		}
		return byteFina;
	}
	
	public void encryptFile(String file,String destFile) throws Exception
	{
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.ENCRYPT_MODE, this.key1);
		InputStream is = new FileInputStream(file);
		OutputStream out = new FileOutputStream(destFile);
		CipherInputStream cis = new CipherInputStream(is, cipher);
		
		byte[] buffer = new byte[1024];
		int r = 0;
		while ((r = cis.read(buffer)) > 0)
		{
			out.write(buffer,0,r);
		}
		cis.close();
		is.close();
		out.close();
	}
	
	public void decryptFile(String file,String destFile) throws Exception
	{
		Cipher cipher = Cipher.getInstance("DES");
		cipher.init(Cipher.DECRYPT_MODE, this.key1);
		InputStream is = new FileInputStream(file);
		OutputStream out = new FileOutputStream(destFile);
		CipherOutputStream cos = new CipherOutputStream(out, cipher);
		
		byte[] buffer = new byte[1024];
		int r = -1;
		while ((r = is.read(buffer)) >= 0)
		{
			cos.write(buffer,0,r);
		}
		cos.close();
		out.close();
		is.close();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虾米大王

有你的支持,我会更有动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值