前端密码用AES加密,后端JAVA解密

这里写自定义目录标题

JS部分

  1. 需要引入的js文件–CryptoJS
    CryptoJS文件包可以自行网上下载;
    当前加密方式用到了aes.js这个文件,就引入这一个
    前端代码:
  	<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
  	<script type="text/javascript" src="js/CryptoJS/rollups/aes.js"></script>
    <script type="text/JavaScript">
	    function userlogin(){
	    	var userName = $("#username").val();
	    	//密钥 (需要前端和后端保持一致)十六位作为密钥
    		var key = "ABCDEFGHIJKL_key";
    		//密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量
    		var iv = "ABCDEFGHIJKLM_iv";
    		//获取加密后的密码
    		var password = aesMinEncrypt(key,iv,$("#password").val());
    		alert("加密后的密码为:"+password);
    		$.ajax({
                url: "userlogin",
                async:false,
                type: "POST",
                data: {'userName':userName,'password':password},
                success: function (result) {
                	$("html").html(result);
                }
    		});
	    }
    	function aesMinEncrypt(key, iv, word){
    	    var _word = CryptoJS.enc.Utf8.parse(word),
    	        _key = CryptoJS.enc.Utf8.parse(key),
    			_iv = CryptoJS.enc.Utf8.parse(iv);
    	    var encrypted = CryptoJS.AES.encrypt(_word, _key, {
    					iv: _iv,
    	                mode: CryptoJS.mode.CBC,
    	                padding: CryptoJS.pad.Pkcs7
    	        });
    	    return encrypted.toString();
    	}
    </script>

JAVA部分

  1. 解密单独写的方法类

后端代码:

public class AESUtils {
	
	//密钥 (需要前端和后端保持一致)十六位作为密钥
	private static final String KEY = "ABCDEFGHIJKL_key";

	//密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量
	private static final String IV = "ABCDEFGHIJKLM_iv";
	
	//算法
	private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
	
	/** 
	 * base 64 decode 
	 * @param base64Code 待解码的base 64 code 
	 * @return 解码后的byte[] 
	 * @throws Exception 
	 */
	public static byte[] base64Decode(String base64Code) throws Exception{
		return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
	}
	
	/** 
	 * AES解密 
	 * @param encryptBytes 待解密的byte[] 
	 * @return 解密后的String 
	 * @throws Exception 
	 */  
	public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {

		Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
		
		byte[] temp = IV.getBytes("UTF-8");
		IvParameterSpec iv = new IvParameterSpec(temp);
		
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"), iv);
		byte[] decryptBytes = cipher.doFinal(encryptBytes);
		
		System.out.print(new String(decryptBytes));
		return new String(decryptBytes);
	}

	/** 
	 * 将base 64 code AES解密 
	 * @param encryptStr 待解密的base 64 code
	 * @return 解密后的string
	 * @throws Exception
	 */ 
	public static String aesDecrypt(String encryptStr) throws Exception {  
		return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr));  
	}

	//测试一下
	public static void main(String[] args) throws Exception {
		String str = "Q uus tQvLdwtGSldhrtKQ==";
		str = str.replace(" ", "+");
		System.out.println(str);
		aesDecrypt(str);
	}
}

2.项目中引用该刚发方法中的解密部分:
在这里插入图片描述
3. 实测结果:
userName:admin ; password:admin12346进行测试
在这里插入图片描述
后台日志:
在这里插入图片描述
4.参考文档:
https://blog.csdn.net/yhj_911/article/details/87187053

发布了4 篇原创文章 · 获赞 0 · 访问量 199

猜你喜欢

转载自blog.csdn.net/u011339397/article/details/104967488