AES

简介

xxx

处理

package aes;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created on 2019/3/12.
 *
 * @author 郑少鹏
 * @desc AES处理
 */
public class AesHandle {
    private static final String KEY_ALGORITHM = "AES";
    /**
     * 默加密算法
     */
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    /**
     * 加密
     *
     * @param origin   明文字节数组
     * @param password password须16字节(与iOS统一)
     * @return 字节数组
     */
    public static byte[] encrypt(byte[] origin, String password) {
        try {
            byte[] enCodeFormat = password.getBytes();
            // 转为AES专用密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
            // 创密码器
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            // 初始化为加密模式密码器
            cipher.init(Cipher.ENCRYPT_MODE, key);
            return cipher.doFinal(origin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param encrypt  密文字节数组
     * @param password password须16字节(与iOS统一)
     * @return 字节数组
     */
    public static byte[] decrypt(byte[] encrypt, String password) {
        try {
            byte[] enCodeFormat = password.getBytes();
            // 转为AES专用密钥
            SecretKeySpec key = new SecretKeySpec(enCodeFormat, KEY_ALGORITHM);
            // 创密码器
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            // 初始化为解密模式密码器
            cipher.init(Cipher.DECRYPT_MODE, key);
            return cipher.doFinal(encrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

snpmyn

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

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

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

打赏作者

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

抵扣说明:

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

余额充值