• JAVA AES加密算法实现代码


    1、代码

    package com.zhaochao.utill;
    
    import java.io.UnsupportedEncodingException;
    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.spec.SecretKeySpec;
    
    public class AESUitl {
    
    	public static byte[] encrypt(String content, String password) {
    		try {
    			KeyGenerator kgen = KeyGenerator.getInstance("AES");
    			kgen.init(128, new SecureRandom(password.getBytes()));
    			SecretKey secretKey = kgen.generateKey();
    			byte[] enCodeFormat = secretKey.getEncoded();
    			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
    			byte[] byteContent = content.getBytes("utf-8");
    			cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
    			byte[] result = cipher.doFinal(byteContent);
    			return result; // 加密
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		} catch (NoSuchPaddingException e) {
    			e.printStackTrace();
    		} catch (InvalidKeyException e) {
    			e.printStackTrace();
    		} catch (UnsupportedEncodingException e) {
    			e.printStackTrace();
    		} catch (IllegalBlockSizeException e) {
    			e.printStackTrace();
    		} catch (BadPaddingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static byte[] decrypt(byte[] content, String password) {
    		try {
    			KeyGenerator kgen = KeyGenerator.getInstance("AES");
    			kgen.init(128, new SecureRandom(password.getBytes()));
    			SecretKey secretKey = kgen.generateKey();
    			byte[] enCodeFormat = secretKey.getEncoded();
    			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
    			Cipher cipher = Cipher.getInstance("AES");// 创建密码器
    			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
    			byte[] result = cipher.doFinal(content);
    			return result; // 加密
    		} catch (NoSuchAlgorithmException e) {
    			e.printStackTrace();
    		} catch (NoSuchPaddingException e) {
    			e.printStackTrace();
    		} catch (InvalidKeyException e) {
    			e.printStackTrace();
    		} catch (IllegalBlockSizeException e) {
    			e.printStackTrace();
    		} catch (BadPaddingException e) {
    			e.printStackTrace();
    		}
    		return null;
    	}
    
    	public static void main(String[] rags) {
    		String content = "test2222";
    		String password = "12345678";
    		// 加密
    		System.out.println("加密前:" + content);
    		byte[] encryptResult = encrypt(content, password);
    		System.out.println("加密后:"+new String(encryptResult));
    		// 解密
    		byte[] decryptResult = decrypt(encryptResult, password);
    		System.out.println("解密后:" + new String(decryptResult));
    	}
    
    }
    

    2、输出结果

    加密前:test2222
    加密后:��»�4�s-�?�D�H�
    解密后:test2222
    


  • 相关阅读:
    【scrapy windows环境下安装遇到的问题】
    关于python中try,except,else,finaly的使用说明:
    【转】简单介绍Python中的try和finally和with方法
    [python]请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution:
    利用闭包返回一个计数器函数,每次调用它返回递增整数:
    把“数字的字符串”转换成“整数”时遇到的小麻烦
    图像识别sift+bow+svm
    自己重装系统原来如此简单
    OLTPBenchmark教程以及workload自动生成
    windows系统快速安装pytorch的详细教程
  • 原文地址:https://www.cnblogs.com/whzhaochao/p/5023416.html
Copyright © 2020-2023  润新知