Search in sources :

Example 1 with HttpAesException

use of com.paascloud.exception.HttpAesException in project paascloud-master by paascloud.

the class HttpAesUtil method decrypt.

/**
 * 解密
 *
 * @param contentParam 需要加密的内容
 * @param keyParam     加密密码
 * @param md5Key       是否对key进行md5加密
 * @param ivParam      加密向量
 *
 * @return string
 */
public static String decrypt(String contentParam, String keyParam, boolean md5Key, String ivParam) {
    try {
        if (PubUtils.isNull(contentParam, keyParam, md5Key, ivParam)) {
            return "";
        }
        byte[] content = new BASE64Decoder().decodeBuffer(contentParam);
        byte[] key = keyParam.getBytes(CHAR_SET);
        byte[] iv = ivParam.getBytes(CHAR_SET);
        if (md5Key) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            key = md.digest(key);
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        // "算法/模式/补码方式"
        Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
        // 使用CBC模式, 需要一个向量iv, 可增加加密算法的强度
        IvParameterSpec ivps = new IvParameterSpec(iv);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivps);
        byte[] bytes = cipher.doFinal(content);
        return new String(bytes, CHAR_SET);
    } catch (Exception ex) {
        log.error("解密密码失败", ex);
        throw new HttpAesException("解密失败");
    }
}
Also used : HttpAesException(com.paascloud.exception.HttpAesException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest) BASE64Decoder(sun.misc.BASE64Decoder) HttpAesException(com.paascloud.exception.HttpAesException)

Example 2 with HttpAesException

use of com.paascloud.exception.HttpAesException in project paascloud-master by paascloud.

the class HttpAesUtil method encrypt.

/**
 * 加密
 *
 * @param contentParam 需要加密的内容
 * @param keyParam     加密密码
 * @param md5Key       是否对key进行md5加密
 * @param ivParam      加密向量
 *
 * @return 加密后的字节数据 string
 */
public static String encrypt(String contentParam, String keyParam, boolean md5Key, String ivParam) {
    try {
        byte[] content = contentParam.getBytes(CHAR_SET);
        byte[] key = keyParam.getBytes(CHAR_SET);
        byte[] iv = ivParam.getBytes(CHAR_SET);
        if (md5Key) {
            MessageDigest md = MessageDigest.getInstance("MD5");
            key = md.digest(key);
        }
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        // "算法/模式/补码方式"
        Cipher cipher = Cipher.getInstance("AES/CBC/ISO10126Padding");
        // 使用CBC模式, 需要一个向量iv, 可增加加密算法的强度
        IvParameterSpec ivps = new IvParameterSpec(iv);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivps);
        byte[] bytes = cipher.doFinal(content);
        return new BASE64Encoder().encode(bytes);
    } catch (Exception ex) {
        log.error("加密密码失败", ex);
        throw new HttpAesException("加密失败");
    }
}
Also used : HttpAesException(com.paascloud.exception.HttpAesException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) BASE64Encoder(sun.misc.BASE64Encoder) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MessageDigest(java.security.MessageDigest) HttpAesException(com.paascloud.exception.HttpAesException)

Aggregations

HttpAesException (com.paascloud.exception.HttpAesException)2 MessageDigest (java.security.MessageDigest)2 Cipher (javax.crypto.Cipher)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 SecretKeySpec (javax.crypto.spec.SecretKeySpec)2 BASE64Decoder (sun.misc.BASE64Decoder)1 BASE64Encoder (sun.misc.BASE64Encoder)1