From 4bcc9291989dbbb950f878ca77a1b9e06f60fab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E6=A0=A9=E5=BD=AC?= Date: Fri, 22 Nov 2019 11:47:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/site/yan/key/Cmd.java | 2 + src/main/java/site/yan/key/cmds/Add.java | 3 +- src/main/java/site/yan/key/cmds/Show.java | 6 +-- .../site/yan/key/{ => securtiy}/AesCore.java | 8 +++- .../java/site/yan/key/securtiy/MD5Core.java | 44 +++++++++++++++++++ .../site/yan/key/{ => securtiy}/Secrey.java | 19 +++++--- 6 files changed, 69 insertions(+), 13 deletions(-) rename src/main/java/site/yan/key/{ => securtiy}/AesCore.java (95%) create mode 100644 src/main/java/site/yan/key/securtiy/MD5Core.java rename src/main/java/site/yan/key/{ => securtiy}/Secrey.java (79%) diff --git a/src/main/java/site/yan/key/Cmd.java b/src/main/java/site/yan/key/Cmd.java index 83c150f..f72aa1b 100644 --- a/src/main/java/site/yan/key/Cmd.java +++ b/src/main/java/site/yan/key/Cmd.java @@ -1,5 +1,7 @@ package site.yan.key; +import site.yan.key.securtiy.Secrey; + /** * Create in 2019/11/22 9:31 by Zhao Xubin. */ diff --git a/src/main/java/site/yan/key/cmds/Add.java b/src/main/java/site/yan/key/cmds/Add.java index c86d29d..dbd874d 100644 --- a/src/main/java/site/yan/key/cmds/Add.java +++ b/src/main/java/site/yan/key/cmds/Add.java @@ -2,7 +2,7 @@ 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. @@ -10,6 +10,7 @@ public class Add implements Run { @Override public void execute() { + secretVerify(); Utils.tell("输入标题:"); String title = Utils.input(); Utils.tell("输入用户名:"); diff --git a/src/main/java/site/yan/key/cmds/Show.java b/src/main/java/site/yan/key/cmds/Show.java index 8179fae..f061f66 100644 --- a/src/main/java/site/yan/key/cmds/Show.java +++ b/src/main/java/site/yan/key/cmds/Show.java @@ -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; @@ -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; /** @@ -30,7 +30,7 @@ public void execute() { private static void parse(String regex) { - if ("*".equals(regex)) regex = "."; + if (".".equals(regex)) regex = ""; Map map = new HashMap<>(); File file = new File(SECRET_FILE); diff --git a/src/main/java/site/yan/key/AesCore.java b/src/main/java/site/yan/key/securtiy/AesCore.java similarity index 95% rename from src/main/java/site/yan/key/AesCore.java rename to src/main/java/site/yan/key/securtiy/AesCore.java index ddd6e61..91ced53 100644 --- a/src/main/java/site/yan/key/AesCore.java +++ b/src/main/java/site/yan/key/securtiy/AesCore.java @@ -1,4 +1,4 @@ -package site.yan.key; +package site.yan.key.securtiy; import java.io.UnsupportedEncodingException; import javax.crypto.Cipher; @@ -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; + } + /** * 加密 * diff --git a/src/main/java/site/yan/key/securtiy/MD5Core.java b/src/main/java/site/yan/key/securtiy/MD5Core.java new file mode 100644 index 0000000..57186c5 --- /dev/null +++ b/src/main/java/site/yan/key/securtiy/MD5Core.java @@ -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")); + } +} + diff --git a/src/main/java/site/yan/key/Secrey.java b/src/main/java/site/yan/key/securtiy/Secrey.java similarity index 79% rename from src/main/java/site/yan/key/Secrey.java rename to src/main/java/site/yan/key/securtiy/Secrey.java index a7e3ce4..cea133a 100644 --- a/src/main/java/site/yan/key/Secrey.java +++ b/src/main/java/site/yan/key/securtiy/Secrey.java @@ -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. @@ -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) { @@ -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(); } @@ -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) { @@ -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); } }