Compare commits

...

3 Commits
1.0 ... master

  1. 9
      readme.md
  2. BIN
      release/password-1.0.jar
  3. BIN
      screenshots/demo.png
  4. 4
      src/main/java/Password.java
  5. 76
      src/main/java/Password9.java
  6. 51
      src/main/java/PasswordManager.java

9
readme.md

@ -1,6 +1,6 @@
# 密码生成器
进入release目录
下载[1.0版本](https://git.fanruan.com/fanruan/password/releases)
直接双击password-1.0.jar文件即可打开
@ -11,4 +11,11 @@ java -jar password-1.0.jar
```
打开
可以选择10.0和9.0版本(9.0之前的版本都和9.0版本一样),做加密解密处理。
**注意:**长度大于40,如果输入文本长度大于40,我们认为是解密码操作,如果长度小于40我们则认为是加密操作。
如图所示
![demo](screenshots/demo.png)

BIN
release/password-1.0.jar

Binary file not shown.

BIN
screenshots/demo.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 142 KiB

4
src/main/java/Password.java

@ -81,8 +81,8 @@ public class Password {
HashMap<String, String> map = new HashMap<>();
map.put(PUBLIC_KEY, findMatchedText(secretKeyText, PUBLIC_PATTERN));
map.put(PRIVATE_KEY, findMatchedText(secretKeyText, PRIVATE_PATTERN));
map.put(PUBLIC_KEY_MARK, findMatchedText(secretKeyText, PUBLIC_PATTERN));
map.put(PRIVATE_KEY_MARK, findMatchedText(secretKeyText, PRIVATE_PATTERN));
return map;
}

76
src/main/java/Password9.java

@ -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;
}
}

51
src/main/java/PasswordManager.java

@ -6,7 +6,6 @@ import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
@ -46,7 +45,7 @@ public class PasswordManager extends JFrame {
}
public PasswordManager() {
setTitle("密码加密解密帮助工具");
setTitle("9.0/10.0密码加密解密帮助工具");
final JPanel panel = (JPanel) getContentPane();
panel.setLayout(new BorderLayout());
final JTextArea inputTextArea = new JTextArea(5, 20);
@ -76,29 +75,42 @@ public class PasswordManager extends JFrame {
}
});
final JComboBox<String> versionComboBox = new JComboBox<>(new DefaultComboBoxModel<>(new String[]{"10.0", "9.0"}));
versionComboBox.setSelectedIndex(0);
final JTextArea outputTextArea = new JTextArea(5, 20);
transferButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filePath = inputFileTextField.getText();
File file = new File(filePath);
Password password = null;
if (file.exists()) {
try {
password = new Password(new String(inputStream2Bytes(new FileInputStream(file)), StandardCharsets.UTF_8));
} catch (Exception ex) {
ex.printStackTrace();
String version = String.valueOf(versionComboBox.getSelectedItem());
if ("10.0".equals(version)) {
String filePath = inputFileTextField.getText();
File file = new File(filePath);
Password password = null;
if (file.exists()) {
try {
password = new Password(new String(inputStream2Bytes(new FileInputStream(file)), StandardCharsets.UTF_8));
} catch (Exception ex) {
ex.printStackTrace();
password = new Password();
}
} else {
password = new Password();
}
} else {
password = new Password();
}
String inputText = inputTextArea.getText();
if (inputText != null && inputText.length() > 40) {
outputTextArea.setText(password.decrypt(inputText));
} else {
outputTextArea.setText(password.encrypt(inputText));
String inputText = inputTextArea.getText();
if (inputText != null && inputText.length() > 40) {
outputTextArea.setText(password.decrypt(inputText));
} else {
outputTextArea.setText(password.encrypt(inputText));
}
} else if ("9.0".equals(version)) {
String inputText = inputTextArea.getText();
if (inputText!= null && inputText.startsWith("___")) {
outputTextArea.setText(Password9.passwordDecode(inputText));
} else {
outputTextArea.setText(Password9.passwordEncode(inputText));
}
}
}
});
@ -107,11 +119,12 @@ public class PasswordManager extends JFrame {
double p = TableLayout.PREFERRED;
double f = TableLayout.FILL;
double[] rowSize = new double[]{p, p, f};
double[] rowSize = new double[]{p, p, p, f};
double[] columnSize = new double[]{p, f};
JComponent[][] comps = new JComponent[][]{
{new JLabel("输入文本:"), inputPane},
{new JLabel("版本信息:"), versionComboBox},
{new JLabel("秘钥文件:"), filePane},
{new JLabel("输出文本:"), outputTextArea}
};

Loading…
Cancel
Save