linux c d5加密和解密,后台对Json数据加密、解密

package com.abc.er.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.*;

import javax.crypto.spec.SecretKeySpec;

import java.security.SecureRandom;public classAesUtil {public static voidmain(String[] args) throws Exception {//aes + base --> aes + base

String content = "{\"mac\":\"123\",\"idfa\":\"123\",\"clientIp\":\"456\",insertTime\":\"test\"}";

System.out.println("加密内容:" +content);

String key= "123abc";

System.out.println("加密密钥和解密密钥:" +key);

String encrypt=aesEncrypt(content, key);

System.out.println("加密后:" +encrypt);

String decrypt=aesDecrypt(encrypt, key);

System.out.println("解密后:" +decrypt);

}/**

* 编码

* @param bstr

* @return String*/

public static String Base64encode(byte[] bstr) {returnBase64.encodeBase64String(bstr);

}/**

* 解码

* @param str

* @return string*/

public static byte[] Base64decode(String str) {returnBase64.decodeBase64(str);

}/** AES加密

* @param content 待加密的内容

* @param encryptKey 加密密钥

* @return 加密后的byte[]

* @throws Exception*/

public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {

KeyGenerator kgen= KeyGenerator.getInstance("AES");/*防止linux下 随机生成key*/SecureRandom random= SecureRandom.getInstance("SHA1PRNG");

random.setSeed(encryptKey.getBytes());

kgen.init(128, random);

Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));return cipher.doFinal(content.getBytes("UTF-8"));

}/**

* AES加密为base 64 code

* @param content 待加密的内容

* @param encryptKey 加密密钥

* @return 加密后的base 64 code

* @throws Exception*/

public staticString aesEncrypt(String content, String encryptKey) throws Exception {returnBase64encode(aesEncryptToBytes(content, encryptKey));

}/**

* AES解密

* @param encryptBytes 待解密的byte[]

* @param decryptKey 解密密钥

* @return 解密后的String

* @throws Exception*/

public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) {byte[] decryptBytes = new byte[0];try{

KeyGenerator kgen= KeyGenerator.getInstance("AES");/*防止linux下 随机生成key*/SecureRandom random= SecureRandom.getInstance("SHA1PRNG");

random.setSeed(decryptKey.getBytes());

kgen.init(128, random);

Cipher cipher= Cipher.getInstance("AES/ECB/PKCS5Padding");

cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));

decryptBytes=cipher.doFinal(encryptBytes);return new String(decryptBytes,"UTF-8");

}catch(Exception e) {

e.printStackTrace();

}returndecryptKey;

}/**

* 将base 64 code AES解密

* @param encryptStr 待解密的base 64 code

* @param decryptKey 解密密钥

* @return 解密后的string

* @throws Exception*/

public staticString aesDecrypt(String encryptStr, String decryptKey){returnaesDecryptByBytes(Base64decode(encryptStr), decryptKey);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值