-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
赵栩彬
committed
Nov 21, 2019
1 parent
8fab47a
commit 65df7c6
Showing
6 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
## 安装 | ||
1. 解压文件包 | ||
2. 将文件包根目录配置到环境变量 | ||
## 快速上手 | ||
### 添加密钥 | ||
1. 命令: yank add | ||
2. 会提示输入:标题、用户名、密码、备注 | ||
- 首次使用该密钥可能会要求设置初始PIN码(用作后续查找密钥) | ||
### 查找密钥 | ||
1. 命令: yank show | ||
- 会提示输入PIN码 | ||
- 以模糊匹配方式按标题查找,会将所有匹配结果完整展示出来 | ||
### 安全 | ||
1. 此命令小工具未接入互联网,大家可根据自身使用情况修改源码以接入互联网保存密钥信息 | ||
2. PIN码及文本密钥加密后持久化在安装目录,加密方式:防君子不能防小人。大家可根据要求替换安全策略 | ||
### 源码打包 | ||
1. 源码打包成jar文件 | ||
2. 并在同级放入脚本(Windows:bat文件,Linux:sh文件) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>site.yan</groupId> | ||
<artifactId>yank</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>8</source> | ||
<target>8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package site.yan.key; | ||
|
||
/** | ||
* Create in 2019/11/21 14:16 by Zhao Xubin. | ||
*/ | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
/** | ||
* %cd%为当前目录,而%~dp0为脚本自身目录 | ||
*/ | ||
public class Main { | ||
|
||
public static String BASE_PATH; | ||
public static String SECRET_FILE; | ||
public static String SECRET_PIN; | ||
public static String CMD; | ||
|
||
/** | ||
* add | ||
* show | ||
*/ | ||
public static void main(String[] args) { | ||
if (args.length < 2) { | ||
System.out.println("命令不全"); | ||
System.exit(0); | ||
} | ||
BASE_PATH = args[0]; | ||
SECRET_FILE = BASE_PATH + "\\.key"; | ||
SECRET_PIN = BASE_PATH + "\\.pin"; | ||
checkHasPin(); | ||
CMD = args[1].toUpperCase(); | ||
Scanner scanner = new Scanner(System.in); | ||
if (CMD.equals("ADD")) { | ||
System.out.println("输入标题:"); | ||
String title = scanner.next(); | ||
System.out.println("输入用户名:"); | ||
String username = scanner.next(); | ||
System.out.println("输入密钥:"); | ||
String password = scanner.next(); | ||
System.out.println("备注:"); | ||
String memo = scanner.next(); | ||
String info = title + "\n" + username + "\n" + password + "\n" + memo + "\n"; | ||
if (addToFile(title, info)) | ||
System.out.println("保存成功"); | ||
else | ||
System.out.println("保存异常"); | ||
} else if (CMD.equals("SHOW")) { | ||
System.out.println("请输入PIN码(4位数字)"); | ||
String pin = scanner.next(); | ||
if (!pin.matches("\\d{4}")) { | ||
System.out.println("PIN码格式错误"); | ||
System.exit(0); | ||
} | ||
if (!verify(pin)) { | ||
System.out.println("PIN码错误"); | ||
System.exit(0); | ||
} | ||
System.out.println("输入该密钥模糊标题"); | ||
String title = scanner.next(); | ||
parse(title); | ||
|
||
} else { | ||
System.out.println("命令输入错误"); | ||
} | ||
|
||
} | ||
|
||
public static boolean addToFile(String title, String info) { | ||
try { | ||
File file = new File(SECRET_FILE); | ||
if (!file.exists()) file.createNewFile(); | ||
FileWriter fileWriter = new FileWriter(file, true); | ||
PrintWriter printWriter = new PrintWriter(fileWriter); | ||
printWriter.println(Secrey.secret(title) + " " + Secrey.secret(info)); | ||
fileWriter.close(); | ||
return true; | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* PIN码验证 | ||
* | ||
* @return | ||
*/ | ||
public static boolean verify(String pin) { | ||
File file = new File(SECRET_PIN); | ||
try { | ||
FileReader fileReader = new FileReader(file); | ||
BufferedReader reader = new BufferedReader(fileReader); | ||
String re = reader.readLine(); | ||
return (Secrey.secret(pin).equals(re)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return false; | ||
} | ||
|
||
public static String parse(String title) { | ||
|
||
Map<String, String> map = new HashMap<>(); | ||
File file = new File(SECRET_FILE); | ||
try { | ||
if(!file.exists())file.createNewFile(); | ||
FileReader fileReader = new FileReader(file); | ||
BufferedReader bufferedReader = new BufferedReader(fileReader); | ||
String temp; | ||
while (Objects.nonNull(temp = bufferedReader.readLine())) { | ||
String[] line = temp.split(" "); | ||
map.put(Secrey.secret(line[0]), Secrey.secret(line[1])); | ||
} | ||
Set<String> set = map.keySet(); | ||
Iterator iterator = set.iterator(); | ||
boolean has=false; | ||
while (iterator.hasNext()) { | ||
String head = (String) iterator.next(); | ||
if (contains(head, title)) | ||
{ | ||
has=true; | ||
String[] info=map.get(head).split("\\n"); | ||
System.out.println("--------------------"); | ||
System.out.println(" title:"+info[0]); | ||
System.out.println("username:"+info[1]); | ||
System.out.println("password:"+info[2]); | ||
System.out.println(" memo:"+info[3]); | ||
System.out.println("--------------------"); | ||
} | ||
} | ||
if(!has)System.out.println("未找到相关密钥"); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return ""; | ||
} | ||
|
||
public static void checkHasPin() { | ||
File file = new File(SECRET_PIN); | ||
if (!file.exists()) { | ||
System.out.println("欢迎使用由言言技术中心研发yank密钥管理工具\n" + | ||
"首次使用请输入初始4位PIN码:"); | ||
String pin = new Scanner(System.in).next(); | ||
if (!pin.matches("\\d{4}")) { | ||
System.out.println("PIN码格式错误"); | ||
System.exit(0); | ||
} | ||
try { | ||
file.createNewFile(); | ||
FileWriter fileWriter = new FileWriter(file); | ||
fileWriter.write(Secrey.secret(pin)); | ||
fileWriter.close(); | ||
System.out.println("PIN码设置成功"); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private static boolean contains(String text, String regex) { | ||
if (text.startsWith(regex) || text.endsWith(regex)) return true; | ||
if (text.split(regex).length > 1) return true; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package site.yan.key; | ||
|
||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.KeyGenerator; | ||
import javax.crypto.SecretKey; | ||
import javax.crypto.SecretKeyFactory; | ||
import javax.crypto.spec.DESKeySpec; | ||
import java.security.Key; | ||
import java.util.Scanner; | ||
|
||
/** | ||
* Create in 2019/11/21 15:52 by Zhao Xubin. | ||
*/ | ||
public class Secrey { | ||
public static String secret(String text) { | ||
|
||
//讲获取的字符串转成字符数组 | ||
char[] c = text.toCharArray(); | ||
//使用for循环给字符数组加密 | ||
for(int i=0;i<c.length;i++){ | ||
c[i] = (char)(c[i]^20000); | ||
} | ||
return new String(c); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
@set input1=%1% | ||
@set input2=%2% | ||
@set input3=%3% | ||
@set input4=%4% | ||
@set input5=%5% | ||
@set input6=%6% | ||
@set input7=%7% | ||
@set input8=%8% | ||
@set input9=%9% | ||
@java -jar "%~dp0key.jar" "%~dp0\" %input1% %input2% %input3% %input4% %input5% %input6% %input7% %input8% %input9% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
PWD=`pwd` | ||
input1=$1 | ||
input2=$2 | ||
input3=$3 | ||
input4=$4 | ||
input5=$5 | ||
input6=$6 | ||
input7=$7 | ||
input8=$8 | ||
input9=$9 | ||
java -jar $PWD/fyan.jar $PWD $input1 $input2 $input3 $input4 $input5 $input6 $input7 $input8 $input9 |