银行卡卡号加解密

 完整的银行卡卡号加解密代码如下:

package com.test.abc.utils;

import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @Author 564413906@qq.com
 * @Date: 2023/07/18 19:20
 * @description BankToolUtils
 */
public class BankToolUtils {

    static String key = "test.abc";

    static String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";

    /*
     *@desc 加密
     *
     * @param $data 待加密数据
     * @param $key
     *
     * @return $token
     * */
    public static String encry(String data) {
        return encry(data, key);
    }

    public static String encry(String data, String key) {
        int nh = (int) (Math.random() * 64);
        String ch = chars.substring(nh, nh + 1);
        String mdKey = md5(key + ch);
        mdKey = mdKey.substring(nh % 8, (nh % 8) + (nh % 8) + 7);
        String txt = base64Encode(data);
        String tmp = "";
        int j = 0;
        int k = 0;
        for (int i = 0; i < txt.length(); i++) {
            k = k == mdKey.length() ? 0 : k;
            j = nh + chars.indexOf(txt.substring(i, i + 1)) + ord(mdKey.charAt(k++)) % 64;
            tmp += chars.substring(j, j + 1);
        }
        try {
            return URLEncoder.encode(ch + tmp, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /*
     *@desc 加密
     *
     * @param $data 待加密数据
     * @param $key
     *
     * @return $token
     * */
    public static String decry(String data) {
        return decry(data, key);
    }

    public static String decry(String data, String key) {
        try {
            String txt = URLDecoder.decode(data, "UTF-8");
            String ch = txt.substring(0, 1);
            int nh = chars.indexOf(ch);
            String mdKey = md5(key + ch);
            mdKey = mdKey.substring(nh % 8, (nh % 8) + (nh % 8) + 7);
            txt = txt.substring(1);
            String tmp = "";
            int j = 0;
            int k = 0;
            for (int i = 0; i < txt.length(); i++) {
                k = k == mdKey.length() ? 0 : k;
                j = chars.indexOf(txt.substring(i, i + 1)) - nh - ord(mdKey.charAt(k++));
                while (j < 0) {
                    j += 64;
                }
                tmp += chars.substring(j, j + 1);
            }
            return base64Decode(tmp);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将字节转换为介于0到255之间的值
     *
     * @param c
     * @return
     */
    public static int ord(char c) {
        return c < 0x80 ? c : c & 0xff;
    }

    /**
     * md5加密
     *
     * @param value 要加密的字符串
     * @return
     */
    public static String md5(String value) {
        return DigestUtils.md5DigestAsHex(value.getBytes());
    }

    /**
     * base64 解密
     *
     * @param value 解密字符串
     * @return
     */
    public static String base64Decode(String value) {
        Base64.Decoder decoder = Base64.getMimeDecoder();
        return new String(decoder.decode(value.getBytes()), StandardCharsets.ISO_8859_1);
    }

    /**
     * base64加密
     *
     * @param value 加密字符串
     * @return
     */
    public static String base64Encode(String value) {
        Base64.Encoder encoder = Base64.getMimeEncoder();
        byte[] bytes = encoder.encode(value.getBytes(StandardCharsets.ISO_8859_1));
        return new String(bytes);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值