Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
richie | 5ad819ffbd | 5 years ago |
richie | 9b4ff1e86d | 5 years ago |
richie | 1f0d665abc | 5 years ago |
6 changed files with 118 additions and 22 deletions
Binary file not shown.
After Width: | Height: | Size: 142 KiB |
@ -0,0 +1,76 @@
|
||||
/** |
||||
* @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; |
||||
} |
||||
} |
Loading…
Reference in new issue