/*
* Copyright (C), 2017-2018, Night’s Watch
* I pledge my life and honor to the Night’s Watch,
* for this night and all the nights to come.
* FileName: AesUtils.java
* Author: join
* Date: 2018年11月29日 上午8:13:17
* Description: //模块目的、功能描述
* History: //修改记录
* <author> <time> <version> <desc>
* 修改人姓名 修改时间 版本号 描述
*/
package com.keda.util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* 〈一句话功能简述〉<br>
* 〈功能详细描述〉
*
* @author join
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
@SuppressWarnings("restriction")
public class AesUtils {
public static final String key="1234567812345678";
public static final String iv="1234567812345678";
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/CBC/NoPadding";//默认的加密算法
/**
*
* 功能描述: <br>
* 〈加密〉
*
* @param data 数据
* @param key 密钥
* @param iv 偏移量
* @return
* @throws Exception
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String encrypt(String data,String key,String iv) throws Exception {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
*
* 功能描述: <br>
* 〈加密:不使用偏移量〉
*
* @param content 数据
* @param password 密钥
* @return
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String encrypt(String content, String password) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
SecretKeySpec keyspec = new SecretKeySpec(password.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, keyspec);// 初始化为加密模式的密码器
byte[] result = cipher.doFinal(byteContent);// 加密
return new BASE64Encoder().encode(result);
}
/**
*
* 功能描述: <br>
* 〈解密〉
*
* @param data 数据
* @param key 密钥
* @param iv 偏移量
* @return
* @throws Exception
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String desEncrypt(String data,String key,String iv) throws Exception {
try {
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "UTF-8");
return originalString;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
*
* 功能描述: <br>
* 〈解密:不使用偏移量〉
*
* @param content
* @param password
* @return
* @throws NoSuchPaddingException
* @throws Exception
* @see [相关类/方法](可选)
* @since [产品/模块版本](可选)
*/
public static String decrypt(String content, String password) throws Exception {
// 实例化
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
// 使用密钥初始化,设置为解密模式
SecretKeySpec keyspec = new SecretKeySpec(password.getBytes(), "AES");
cipher.init(Cipher.DECRYPT_MODE, keyspec);
// 执行操作
byte[] result = cipher.doFinal(new BASE64Decoder().decodeBuffer(content));
return new String(result, "utf-8");
}
}