Java crypto 之AES加密解密示例

代码如下:

package com.etoak.test;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class TestEncrypt {
    //private static final byte[] KEYS = new byte[]{100, 51, 78, 107, 97, 71, 120, 104, 100, 51, 78, 107, 97, 71, 120, 104, 100, 51, 78, 107, 97, 65, 61, 61};
    private static byte[] KEYS = null;
    private static String key = null;


    public static void main(String[] args) {
        // region 计算KEYS
        // AESConstants.AES_BLOCK_SIZE默认值为16
        // IV length: must be 16 bytes long
        Base64.Encoder encoder = Base64.getEncoder();
        Base64.Decoder decoder = Base64.getDecoder();
        String base = "wsdhlawsdhlawsdh";
        String s = encoder.encodeToString(base.getBytes());
        KEYS = s.getBytes();
        // endregion

        String uri = "待加密字符串";
        String encrypt = encrypt(uri);
        System.out.println("当前加密后URI:" + encrypt);
        System.out.println("当前解密后URI:" + decrypt(encrypt));
    }

    private static String encrypt(String planPasswd) {
        try {
            String key = getLegalKey();
            String iv = getLegalKey();
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int blockSize = cipher.getBlockSize();
            byte[] dataBytes = planPasswd.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                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(1, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            return encode(encrypted).trim();
        } catch (Exception var11) {
            return planPasswd;
        }
    }

    private static String decrypt(String encryptedPasswd) {
        String key = getLegalKey();
        String iv = getLegalKey();

        try {
            byte[] encrypted1 = decode(encryptedPasswd);
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());
            cipher.init(2, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString.trim();
        } catch (Exception var9) {
            return encryptedPasswd;
        }
    }

    private static String getLegalKey() {
        if (key == null) {
            Base64.Decoder decoder = Base64.getDecoder();

            try {
                key = new String(decoder.decode(new String(KEYS)), "UTF-8");
            } catch (UnsupportedEncodingException var2) {
                var2.printStackTrace();
            }
        }

        return key;
    }


    private static String encode(byte[] byteArray) {
        return new String(Base64.getEncoder().encode(byteArray));
    }

    private static byte[] decode(String base64EncodedString) {
        return Base64.getDecoder().decode(base64EncodedString);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wsdhla

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

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

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

打赏作者

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

抵扣说明:

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

余额充值