You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.2 KiB
76 lines
2.2 KiB
/** |
|
* @author richie |
|
* @version 10.0 |
|
* Created by richie on 2019-07-12 |
|
*/ |
|
public class Password9 { |
|
|
|
private static final int HEX = 16; |
|
|
|
private static final int ENCODE_LEN = 4; |
|
|
|
private static final int[] PASSWORD_MASK_ARRAY = {19, 78, 10, 15, 100, 213, 43, 23}; |
|
|
|
/** |
|
* 给字符串加密. |
|
* |
|
* @param text 旧文本 |
|
* @return 加密结果 |
|
*/ |
|
public static String passwordEncode(String text) { |
|
StringBuilder passwordTextBuf = new StringBuilder(); |
|
passwordTextBuf.append("___"); |
|
if (text == null) { |
|
return passwordTextBuf.toString(); |
|
} |
|
|
|
int pmIndex = 0; |
|
for (int i = 0; i < text.length(); i++) { |
|
if (pmIndex == PASSWORD_MASK_ARRAY.length) { |
|
pmIndex = 0; |
|
} |
|
|
|
int intValue = ((int) text.charAt(i)) ^ PASSWORD_MASK_ARRAY[pmIndex]; |
|
String passwordText = Integer.toHexString(intValue); |
|
int pLength = passwordText.length(); |
|
for (int j = 0; j < ENCODE_LEN - pLength; j++) { |
|
passwordText = "0" + passwordText; |
|
} |
|
|
|
passwordTextBuf.append(passwordText); |
|
pmIndex++; |
|
} |
|
|
|
return passwordTextBuf.toString(); |
|
} |
|
|
|
/** |
|
* 给字符串解密 |
|
* |
|
* @param passwordText 待解密的字符串 |
|
* @return 解密后的字符串 |
|
*/ |
|
public static String passwordDecode(String passwordText) { |
|
if (passwordText != null && passwordText.startsWith("___")) { |
|
passwordText = passwordText.substring(3); |
|
|
|
StringBuilder textBuf = new StringBuilder(); |
|
|
|
int pmIndex = 0; |
|
for (int i = 0; i <= (passwordText.length() - ENCODE_LEN); i += ENCODE_LEN) { |
|
if (pmIndex == PASSWORD_MASK_ARRAY.length) { |
|
pmIndex = 0; |
|
} |
|
|
|
String hexText = passwordText.substring(i, i + ENCODE_LEN); |
|
int hexInt = Integer.parseInt(hexText, HEX) ^ PASSWORD_MASK_ARRAY[pmIndex]; |
|
|
|
textBuf.append((char) hexInt); |
|
pmIndex++; |
|
} |
|
|
|
passwordText = textBuf.toString(); |
|
} |
|
return passwordText; |
|
} |
|
}
|
|
|