sun.misc.BASE64Decoder是内部专用 API, 可能会在未来发行版中删除

文章讨论了sun.misc.BASE64Decoder作为内部专用API的潜在问题,指出其可能在未来的Java发行版中被移除。推荐使用java.util.Base64.Encoder和Decoder作为安全的替代品进行Base64编码和解码操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/**
 * Description 根据键值进行解密
 *
 * @return
 */
public static String decrypt(String data, String key) throws Exception {
    if (data == null)
        return null;
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] buf = decoder.decodeBuffer(data);
    byte[] bt = decrypt(buf, format(key));
    return new String(bt, "UTF-8");
}

/**
 * Description 根据键值进行解密
 *
 * @return
 */
public static byte[] decrypt2Byte(String data, String key) throws Exception {
    if (data == null)
        return null;
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] buf = decoder.decodeBuffer(data);
    return decrypt(buf, format(key));
}
/**
 * Description 根据键值进行加密
 *
 * @return
 */
public static String encrypt(String data, String key) throws Exception {
    byte[] bt = encrypt(data.getBytes("UTF-8"), format(key));
    String strs = new BASE64Encoder().encode(bt);
    return strs;
}

/**
 * Description 根据键值进行加密
 *
 * @return
 */
public static String encrypt(byte[] bytes, String key) throws Exception {
    byte[] bt = encrypt(bytes, format(key));
    String strs = new BASE64Encoder().encode(bt);
    return strs;
}

解决方案:

import java.util.Base64.Encoder;
import java.util.Base64.Decoder;

进行替换

/**
 * Description 根据键值进行加密
 *
 * @return
 */
public static String encrypt(String data, String key) throws Exception {
    byte[] bt = encrypt(data.getBytes("UTF-8"), format(key));
    Encoder encoder = Base64.getEncoder();
    return encoder.encodeToString(bt);
}

/**
 * Description 根据键值进行加密
 *
 * @return
 */
public static String encrypt(byte[] bytes, String key) throws Exception {
    byte[] bt = encrypt(bytes, format(key));
    Encoder encoder = Base64.getEncoder();
    return encoder.encodeToString(bt);
}

/**
 * Description 根据键值进行解密
 *
 * @return
 */
public static String decrypt(String data, String key) throws Exception {
    if (data == null)
        return null;
    Decoder decoder = Base64.getDecoder();
    byte[] buf = decoder.decode(data);
    byte[] bt = decrypt(buf, format(key));
    return new String(bt, "UTF-8");
}

/**
 * Description 根据键值进行解密
 *
 * @return
 */
public static byte[] decrypt2Byte(String data, String key) throws Exception {
    if (data == null)
        return null;
    /*BASE64Decoder decoder = new BASE64Decoder();
    byte[] buf = decoder.decodeBuffer(data);*/
    Decoder decoder = Base64.getDecoder();
    byte[] buf = decoder.decode(data);
    return decrypt(buf, format(key));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值