完整的银行卡卡号加解密代码如下:
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);
}
}
8428

被折叠的 条评论
为什么被折叠?



