java 加解密类,简略的java加密解密类

简单的java加密解密类

import java.security.Key;

import java.security.NoSuchAlgorithmException;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

public class CryptUtil {

private static final CryptUtil instance = new CryptUtil();

private CryptUtil() {

}

public static CryptUtil getInstance() {

return instance;

}

private Key initKeyForAES(String key) throws NoSuchAlgorithmException {

if (null == key || key.length() == 0) {

throw new NullPointerException("key not is null");

}

SecretKeySpec key2 = null;

try {

KeyGenerator kgen = KeyGenerator.getInstance("AES");

kgen.init(128, new SecureRandom(key.getBytes()));

SecretKey secretKey = kgen.generateKey();

byte[] enCodeFormat = secretKey.getEncoded();

key2 = new SecretKeySpec(enCodeFormat, "AES");

} catch (NoSuchAlgorithmException ex) {

throw new NoSuchAlgorithmException();

}

return key2;

}

/**

* AES加密算法,不受密钥长度限制

* @param content

* @param key

* @return

*/

public String encryptAES(String content, String key){

try{

SecretKeySpec secretKey = (SecretKeySpec) initKeyForAES(key);

Cipher cipher = Cipher.getInstance("AES");// 创建密码器

byte[] byteContent = content.getBytes("utf-8");

cipher.init(Cipher.ENCRYPT_MODE, secretKey);// 初始化

byte[] result = cipher.doFinal(byteContent);

return asHex(result); // 加密

}

catch (Exception e){

e.printStackTrace();

}

return null;

}

/**

* aes解密算法,不受密钥长度限制

* @param content

* @param key

* @return

*/

public String decryptAES(String content, String key){

try{

SecretKeySpec secretKey = (SecretKeySpec) initKeyForAES(key);

Cipher cipher = Cipher.getInstance("AES");// 创建密码器

cipher.init(Cipher.DECRYPT_MODE, secretKey);// 初始化

byte[] result = cipher.doFinal(asBytes(content));

return new String(result); // 加密

}

catch (Exception e){

e.printStackTrace();

}

return null;

}

/**

* 将2进制数值转换为16进制字符串

* @param buf

* @return

*/

public String asHex(byte buf[]){

StringBuffer strbuf = new StringBuffer(buf.length * 2);

int i;

for (i = 0; i < buf.length; i++){

if (((int) buf[i] & 0xff) < 0x10)

strbuf.append("0");

strbuf.append(Long.toString((int) buf[i] & 0xff, 16));

}

return strbuf.toString();

}

/**

* 将16进制转换

* @param hexStr

* @return

*/

public byte[] asBytes(String hexStr) {

if (hexStr.length() < 1)

return null;

byte[] result = new byte[hexStr.length() / 2];

for (int i = 0; i < hexStr.length() / 2; i++){

int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);

int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),16);

result[i] = (byte) (high * 16 + low);

}

return result;

}

public static void main(String[] args) {

CryptUtil crypt = CryptUtil.getInstance();

String content = "asdfsdfdsfds|33333443";

System.out.println(crypt.encryptAES(content, "aaa22"));

String dcontent = crypt.encryptAES(content, "aaa22");

System.out.println(crypt.decryptAES(dcontent, "aaa22"));

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值