一个简单功能,主要是备存一下代码
这功能的想法,有些文档,基于安全考虑,不能明文写上密码,所以密码要么单独存储,要么就存加密内容,但加密内容每次都要选择和去找工具解密,比较麻烦。索性,就做成url的方式。
相关链接:
上图看看:
相关代码:
密码本
类型:
输入:
密钥:
编码:
过滤空格
填充:
跳转URL地址:
输出:
package com.zx.app.work.app.enums.tool;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 编码类型
*/
@AllArgsConstructor
@Getter
public enum EncodeActionEnum {
MD5("md5", "", "", ""),
BASE64("base64", "", "", ""),
AES("aes", "", "DES@", ""),
ENC("enc", "", "ENC(", ")"),
UNKNOWN("unknown", "未知", "", ""),
;
private final String code;
private final String desc;
private final String fillPreKey;
private final String fillPostKey;
public static EncodeActionEnum getItem(String pCode, EncodeActionEnum defaultValue) {
if (pCode == null || pCode.isEmpty()) {
return defaultValue;
}
for (EncodeActionEnum item : EncodeActionEnum.values()) {
if (pCode.equals(item.getCode())) {
return item;
}
}
return defaultValue;
}
/**
* 获得枚举的数据源Key-Value
*
* @return
*/
public static Map getList() {
Map result = new LinkedHashMap();
for (EncodeActionEnum item : EncodeActionEnum.values()) {
result.put(item.getCode(), item.getDesc());
}
return result;
}
protected static String toJson() {
StringBuilder sb = new StringBuilder();
for (EncodeActionEnum em : EncodeActionEnum.values()) {
if(EncodeActionEnum.UNKNOWN.equals(em)){
continue;
}
sb.append("");
}
return sb.toString();
}
public static void main(String[] args) {
String str = toJson();
System.out.println(str);
}
}
package com.zx.app.work.app.enums.tool;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 填充操作
*/
@AllArgsConstructor
@Getter
public enum RemoveFillKeyEnum {
YES("yes", ""),
NO("no", ""),
INPUT("input", ""),
;
private final String code;
/**
* 描述
*/
private final String desc;
public static RemoveFillKeyEnum getItem(String pCode, RemoveFillKeyEnum defaultValue) {
if (pCode == null || pCode.isEmpty()) {
return defaultValue;
}
for (RemoveFillKeyEnum item : RemoveFillKeyEnum.values()) {
if (pCode.equals(item.getCode())) {
return item;
}
}
return defaultValue;
}
}
package com.zx.app.work.app.svc.tool;
import com.zx.app.ba.tool.demo.EncUniUtil;
import com.zx.app.ba.tool.enums.BooleanEnum;
import com.zx.app.ba.tool.util.*;
import com.zx.app.work.app.bs.tool.LoadJenkinsConfigBs;
import com.zx.app.work.app.enums.tool.EncodeActionEnum;
import com.zx.app.work.app.enums.tool.RemoveFillKeyEnum;
import com.zx.app.work.app.item.cp.tool.LoadJenkinsConfigCp;
import com.zx.app.work.app.item.cp.tool.ToolCommonEncodeCp;
import com.zx.app.work.app.item.cr.tool.LoadJenkinsConfigCr;
import com.zx.app.work.common.constant.ConstantName;
/**
* @author SH.YE
*/
public class AppToolCommonSvc {
protected String getFillPreKey(ToolCommonEncodeCp cp){
EncodeActionEnum encodeActionEnumV = EncodeActionEnum.getItem(cp.getAction(), EncodeActionEnum.UNKNOWN);
RemoveFillKeyEnum keyEnum = RemoveFillKeyEnum.getItem(cp.getRemoveFillKey(),RemoveFillKeyEnum.NO);
switch (keyEnum){
case YES:
return encodeActionEnumV.getFillPreKey();
case INPUT:
return StrUniUtil.strNullToEmpty(cp.getFillPreKey());
case NO:
default:
return "";
}
}
protected String getFillPostKey(ToolCommonEncodeCp cp){
EncodeActionEnum encodeActionEnumV = EncodeActionEnum.getItem(cp.getAction(), EncodeActionEnum.UNKNOWN);
RemoveFillKeyEnum keyEnum = RemoveFillKeyEnum.getItem(cp.getRemoveFillKey(),RemoveFillKeyEnum.NO);
switch (keyEnum){
case YES:
return encodeActionEnumV.getFillPostKey();
case INPUT:
return StrUniUtil.strNullToEmpty(cp.getFillPostKey());
case NO:
default:
return "";
}
}
public String encode(ToolCommonEncodeCp cp) {
EncodeActionEnum encodeActionEnumV = EncodeActionEnum.getItem(cp.getAction(), EncodeActionEnum.UNKNOWN);
boolean trimFlag = BooleanEnum.dataIsTrue(cp.getTrimFlag());
String input = StrUniUtil.strNullToTrimEmpty(cp.getInput(),trimFlag);
boolean decodeFlag = BooleanEnum.dataIsTrue(cp.getDecodeFlag());
String fillPreKey = getFillPreKey(cp);
String fillPostKey = getFillPostKey(cp);
if(decodeFlag) {
input = StrUniUtil.strRemoveStartKey(input, fillPreKey);
input = StrUniUtil.strRemoveEndKey(input, fillPostKey);
}
String result = null;
switch (encodeActionEnumV) {
case MD5:
result = Md5Util.getMD5ByEncodingName(input, cp.getCharsetName());
break;
case BASE64:
if (decodeFlag) {
result = Base64Util.decryptBase64(input);
} else {
result = Base64Util.encryptBase64(input, cp.getCharsetName());
result = StrUniUtil.strNotEmptyFillKey(result,fillPreKey,fillPostKey);
}
break;
case AES:
if (decodeFlag) {
result = AesUtil.decode(input, cp.getSecretKey());
} else {
result = AesUtil.encode(input, cp.getSecretKey(), cp.getCharsetName());
result = StrUniUtil.strNotEmptyFillKey(result,fillPreKey,fillPostKey);
}
break;
case ENC:
if (decodeFlag) {
result = EncUniUtil.decode(input, cp.getSecretKey());
} else {
result = EncUniUtil.encode(input, cp.getSecretKey());
result = StrUniUtil.strNotEmptyFillKey(result,fillPreKey,fillPostKey);
}
break;
case UNKNOWN:
default:
return cp.getAction() + "指令未知";
}
return result;
}
}
package com.zx.app.ba.tool.demo;
import org.jasypt.util.text.BasicTextEncryptor;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class EncUniUtil {
public static String encode(String secret, String strPwd) {
BasicTextEncryptor worker = new BasicTextEncryptor();
// application.properties,
// jasypt.encryptor.password 加密盐值
worker.setPassword(strPwd);
return worker.encrypt(secret);
}
public static String decode(String secret) {
return decode(secret, UNI_KEY_PWD);
}
public static String decode(String secret, String strPwd) {
BasicTextEncryptor worker = new BasicTextEncryptor();
// application.properties,
// jasypt.encryptor.password 加密盐值
worker.setPassword(strPwd);
return worker.decrypt(secret);
}
}
package com.zx.app.ba.tool.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
public class AesUtil {
private static final String AES = "AES";
private static final String SHA1PRNG = "SHA1PRNG";
private static final String UTF8 = "UTF-8";
/**
* 将二进制转换成16进制
*/
public static String parseByte2HexStr(byte[] buf) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; ++i) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr == null) {
return null;
}
int hexStrLen = hexStr.length();
if (hexStrLen < 1) {
return null;
}
int iLen = hexStrLen / 2;
byte[] result = new byte[iLen];
for (int i = 0; i < iLen; ++i) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static String encode(String secret, String strPwd, String charsetName) {
byte[] encryptResult = encrypt(secret, strPwd, charsetName);
String encryptResultStr = parseByte2HexStr(encryptResult);
return encryptResultStr;
}
public static String decode(String secret, String strPwd) {
//解密
byte[] decryptFrom = parseHexStr2Byte(secret);
byte[] decryptResult = decrypt(decryptFrom, strPwd);
return new String(decryptResult);
}
/**
* 加密
*
* @param content 需要加密的内容
* @param password 加密密码
* @param charsetName 编码
*/
private static byte[] encrypt(String content, String password, String charsetName) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance(AES);
SecureRandom secureRandom = SecureRandom.getInstance(SHA1PRNG);
secureRandom.setSeed(password.getBytes());
keyGen.init(128, secureRandom);
SecretKey secretKey = keyGen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);
if (charsetName == null || charsetName.length() == 0) {
charsetName = UTF8;
}
byte[] byteContent = content.getBytes(charsetName);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
return result;
} catch (Exception e) {
return null;
}
}
/**
* 解密
*/
private static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance(AES);
//防止linux下 随机生成key
SecureRandom secureRandom = SecureRandom.getInstance(SHA1PRNG);
secureRandom.setSeed(password.getBytes());
keyGen.init(128, secureRandom);
SecretKey secretKey = keyGen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
return null;
}
}
}
package com.zx.app.ba.tool.util;
import java.security.MessageDigest;
/**
* MD5相关的方法
*/
public class Md5Util {
/**
* byte[] 数组转成Md5的字符串
*
* @param b
* @return
*/
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; ++i) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
/**
* byte 转成字符串
*
* @param b
* @return
*/
private static String byteToHexString(byte b) {
int n = b;
if (n < 0) {
n += 256;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
/**
* 通过编码类型生成MD5
*
* @param origin | 原字符串 | 如:Java编程
* @param charsetName |编码类型 | 如:gb2312
* @return
*/
public static String getMD5ByEncodingName(String origin, String charsetName) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetName == null || charsetName.isEmpty()) {
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
} else {
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetName)));
}
} catch (Exception err) {
err.printStackTrace();
}
return resultString;
}
/**
*
*/
private static final String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
"e", "f"};
/**
* 获得中文编码类型(gb2312)的MD5
*
* @param s 要MD5的字符串|如:Java编程
* @return
*/
public static String getGb2312Md5(String s) {
return getMD5ByEncodingName(s, "gb2312");
}
/**
* 获得中文编码类型(gb2312)的MD5
*
* @param s 要MD5的字符串|如:Java编程
* @return
*/
public static String getUtf8Md5(String s) {
return getMD5ByEncodingName(s, "UTF-8");
}
}
package com.zx.app.ba.tool.util;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
/**
* @author SH.YE
*/
public class Base64Util {
/**
* Base64编码
*
* @param str
* @return
*/
public static String getBase64En(String str) {
if (str == null) {
return "";
}
return Base64.getEncoder().encodeToString(str.getBytes());
}
/**
* Base64解码
*
* @param str
* @return
*/
public static String getBase64De(String str) {
if (str == null) {
return "";
}
byte[] decodeBytes = Base64.getDecoder().decode(str);
return new String(decodeBytes);
}
/**
* 给字符串加密
*
* @param text
* @return
*/
public static String encryptBase64(String text) {
if (text == null || text.isEmpty()) {
return "";
}
byte[] textByte = new byte[0];
try {
textByte = text.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String encodedText = Base64.getEncoder().encodeToString(textByte);
return encodedText;
}
/**
* 给字符串加密
*
* @param text
* @return
*/
public static String encryptBase64(String text, String charsetName) {
if (text == null || text.isEmpty()) {
return "";
}
byte[] textByte = new byte[0];
try {
if (charsetName == null || charsetName.isEmpty()) {
charsetName = "utf-8";
}
textByte = text.getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String encodedText = Base64.getEncoder().encodeToString(textByte);
return encodedText;
}
/**
* 将加密后的字符串进行解密
*
* @param encodedText
* @return
*/
public static String decryptBase64(String encodedText) {
if (encodedText == null || encodedText.isEmpty()) {
return "";
}
String text = null;
try {
text = new String(Base64.getDecoder().decode(encodedText), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return text;
}
public static String encodeToString(byte[] src) {
return Base64.getEncoder().encodeToString(src);
}
}
public static String strNullToTrimEmpty(String data, boolean trimFlag) {
if (data == null) {
return "";
}
if (trimFlag) {
return data.trim();
}
return data;
}
public static String strRemoveStartKey(String data, String key) {
if (data == null || data.isEmpty()) {
return "";
}
if (key == null || key.isEmpty()) {
return data;
}
if (!data.startsWith(key)) {
return data;
}
if (data.equals(key)) {
return "";
}
return data.substring(key.length());
}
public static String strRemoveEndKey(String data, String key) {
if (data == null || data.isEmpty()) {
return "";
}
if (key == null || key.isEmpty()) {
return data;
}
if (!data.endsWith(key)) {
return data;
}
if (data.equals(key)) {
return "";
}
return data.substring(0, data.length() - key.length());
}
public static String strNotEmptyFillKey(String data, String preKey, String postKey) {
if (data == null || data.isEmpty()) {
return "";
}
return strNullToEmpty(preKey) + strNullToEmpty(data) + strNullToEmpty(postKey);
}