java AesUtil (五)

/*
 * 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");
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会跑的葫芦怪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值