Skip to content

Commit

Permalink
初始提交
Browse files Browse the repository at this point in the history
  • Loading branch information
赵栩彬 committed Nov 22, 2019
1 parent 1bc0d51 commit 4bcc929
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/main/java/site/yan/key/Cmd.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package site.yan.key;

import site.yan.key.securtiy.Secrey;

/**
* Create in 2019/11/22 9:31 by Zhao Xubin.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/site/yan/key/cmds/Add.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import site.yan.key.Utils;

import static site.yan.key.Secrey.addToFile;
import static site.yan.key.securtiy.Secrey.*;

/**
* Create in 2019/11/22 10:14 by Zhao Xubin.
*/
public class Add implements Run {
@Override
public void execute() {
secretVerify();
Utils.tell("输入标题:");
String title = Utils.input();
Utils.tell("输入用户名:");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/site/yan/key/cmds/Show.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package site.yan.key.cmds;

import site.yan.key.Secrey;
import site.yan.key.securtiy.Secrey;
import site.yan.key.Utils;

import java.io.BufferedReader;
Expand All @@ -11,7 +11,7 @@

import static site.yan.key.Cmd.SECRET_FILE;
import static site.yan.key.Cmd.arg;
import static site.yan.key.Secrey.secretVerify;
import static site.yan.key.securtiy.Secrey.secretVerify;
import static site.yan.key.Utils.contains;

/**
Expand All @@ -30,7 +30,7 @@ public void execute() {

private static void parse(String regex) {

if ("*".equals(regex)) regex = ".";
if (".".equals(regex)) regex = "";

Map<String, String> map = new HashMap<>();
File file = new File(SECRET_FILE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package site.yan.key;
package site.yan.key.securtiy;
import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
Expand Down Expand Up @@ -28,12 +28,16 @@ public class AesCore {
//public static final String AES_TYPE = "AES/CBC/ZeroPadding";

//密钥
private static final String AES_KEY="147896325632147896321452"; //AES固定格式为128/192/256 bits.即:16/24/32bytes。DES固定格式为128bits,即8bytes。
private static String AES_KEY="81dc9bdb52d04dc20036dbd8"; //AES固定格式为128/192/256 bits.即:16/24/32bytes。DES固定格式为128bits,即8bytes。

//字符补全
private static final String[] consult = new String[]{"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G"};


public static void setAesKey(String aesKey) {
AES_KEY = aesKey;
}

/**
* 加密
*
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/site/yan/key/securtiy/MD5Core.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package site.yan.key.securtiy;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* Create in 2019/11/22 11:25 by Zhao Xubin.
*/
public class MD5Core {
public static String getMD5(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");//获取MD5实例
md.update(plainText.getBytes());//此处传入要加密的byte类型值
byte[] digest = md.digest();//此处得到的是md5加密后的byte类型值

/*
下边的运算就是自己添加的一些二次小加密,记住这个千万不能弄错乱,
否则在解密的时候,你会发现值不对的(举例:在注册的时候加密方式是一种,
在我们登录的时候是不是还需要加密它的密码然后和数据库的进行比对,但是
最后我们发现,明明密码对啊,就是打不到预期效果,这时候你就要想一下,你是否
有改动前后的加密方式)
*/
int i;
StringBuilder sb = new StringBuilder();
for (int offset = 0; offset < digest.length; offset++) {
i = digest[offset];
if (i < 0)
i += 256;
if (i < 16)
sb.append(0);
sb.append(Integer.toHexString(i));//通过Integer.toHexString方法把值变为16进制
}
return sb.toString().substring(0, 24);//从下标0开始,length目的是截取多少长度的值
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) {
System.out.println(getMD5("1234"));
}
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package site.yan.key;
package site.yan.key.securtiy;


import site.yan.key.Utils;

import java.io.*;
import java.util.Scanner;

import static site.yan.key.Cmd.SECRET_FILE;
import static site.yan.key.Cmd.SECRET_PIN;
import static site.yan.key.securtiy.MD5Core.getMD5;

/**
* Create in 2019/11/21 15:52 by Zhao Xubin.
Expand All @@ -25,7 +28,7 @@ public static void checkHasPin() {
try {
file.createNewFile();
FileWriter fileWriter = new FileWriter(file);
fileWriter.write(Secrey.secret(pin,true));
fileWriter.write(getMD5(pin));
fileWriter.close();
System.out.println("PIN码设置成功 :)");
} catch (IOException e) {
Expand All @@ -49,8 +52,10 @@ private static boolean verifyPin(String pin) {
try {
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String re = reader.readLine();
return (Secrey.secret(pin,true).equals(re));
String read = reader.readLine();
String md5 = getMD5(pin);
AesCore.setAesKey(md5);
return (md5.equals(read));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -63,7 +68,7 @@ public static boolean addToFile(String title, String info) {
if (!file.exists()) file.createNewFile();
FileWriter fileWriter = new FileWriter(file, true);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.println(Secrey.secret(title,true) + " " + Secrey.secret(info,true));
printWriter.println(Secrey.secret(title, true) + " " + Secrey.secret(info, true));
fileWriter.close();
return true;
} catch (IOException e) {
Expand All @@ -72,7 +77,7 @@ public static boolean addToFile(String title, String info) {
}
}

public static String secret(String text ,boolean encode) {
return encode? AesCore.encrypt(text): AesCore.decrypt(text);
public static String secret(String text, boolean encode) {
return encode ? AesCore.encrypt(text) : AesCore.decrypt(text);
}
}

0 comments on commit 4bcc929

Please sign in to comment.