java加密解密,使用AES对称加密解密工具类

下面展示这些 代码部分

import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.*;
import java.security.spec.*;
import javax.crypto.spec.*;

//算法
public class Aes
{
	static final String ALGORITHM = "AES";
	static Charset charset = Charset.forName("UTF-8");

	public static SecretKey generateKey() throws NoSuchAlgorithmException { // 生成密钥
		KeyGenerator secretGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom secureRandom = new SecureRandom();
		secretGenerator.init(secureRandom);
		SecretKey secretKey = secretGenerator.generateKey();
		return secretKey;
	}

	
	public static byte[] encrypt(byte[] content, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // 加密
		return aes(content,Cipher.ENCRYPT_MODE,secretKey);
	}

	public static byte[] decrypt(byte[] contentArray, SecretKey secretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { // 解密
		byte[] result =  aes(contentArray,Cipher.DECRYPT_MODE,secretKey);
		return result;
	}

	private static byte[] aes(byte[] contentArray, int mode, SecretKey secretKey)
	throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		Cipher cipher = Cipher.getInstance(ALGORITHM);
		cipher.init(mode, secretKey);
		byte[] result = cipher.doFinal(contentArray);
		return result;
	}

	//获取key
	public static SecretKey getKey(byte[] kes){
		SecretKey secretKey=null;
		try
		{
			KeyGenerator secretGenerator = KeyGenerator.getInstance("AES");
			byte[] sadkey=kes;//Aes.generateKey().getEncoded()
			SecureRandom secureRandom = new SecureRandom();
			secureRandom.setSeed(sadkey);
			secretGenerator.init(secureRandom);
			secretKey = secretGenerator.generateKey();
		}
		catch (Exception e)
		{}
		
		return secretKey;
	}
	
	//获取key byte类型
	public static byte[] getbyteKey(){
		
		byte[] keysss=null;
		
		//方法一
		try
		{
		KeyGenerator secretGenerator = KeyGenerator.getInstance(ALGORITHM);
		SecureRandom secureRandom = new SecureRandom();
		secretGenerator.init(secureRandom);
		SecretKey secretKey = secretGenerator.generateKey();
		keysss = secretKey.getEncoded();

		}
		catch (NoSuchAlgorithmException e)
		{}
		
		/*
		
		//方法二
		try
		{
			keysss = generateKey().getEncoded();

		}
		catch (NoSuchAlgorithmException e)
		{}
		
		*/
		
		
		return keysss;
	}
	
	
	
	/*
	
	
	 byte[] mi= getbyteKey();
	 SecretKey secretKey = getKey(mi);
	
	 String sad="待加jcjgvv国际服减肥法韩剧发货还差个独女结果迷惑不解hcc几个高Vhhc互个粉附近的烘干机发布会好的就睡觉大V好家常菜你打和他爸登记过TV好彻底v的开发是呀密";
	 byte[] sady=null;//加密了
	 byte[] sada=null;//解密后
	 sady=Aes.encrypt(sad.getBytes("utf-8"),secretKey);
	 sada=Aes.decrypt(sady,secretKey);
	 Toast.makeText(MainActivity.this,"加密后:_"+new String(sady)+"解密后:_"+new String(sada),Toast.LENGTH_SHORT).show();

	 
	
	
	
	*/
	
	
	
	
	
	
	}

上面一些就是笔记记录,内容一部分来自网络,进行修改优化后而来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值