From e5b97d1e5dd325ab30f9b93e860a25b7ccebaa54 Mon Sep 17 00:00:00 2001
From: Jesse-Ma <24167796@qq.com>
Date: Mon, 27 Feb 2023 09:53:06 +0800
Subject: [PATCH] getNoteTxt
---
pom.xml | 4 +-
.../note/controller/NoteController.java | 7 ++++
.../flagnote/note/service/NoteService.java | 2 +
.../note/service/NoteServiceImpl.java | 37 +++++++++++++++-
.../com/flagnote/note/utils/BizKeyUtils.java | 8 +++-
.../com/flagnote/note/utils/SecretUtils.java | 42 +++++++++++++++++--
6 files changed, 92 insertions(+), 8 deletions(-)
diff --git a/pom.xml b/pom.xml
index 8a52da8..eaf9f8b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.boot
spring-boot-starter-parent
- 3.0.1
+ 3.0.2
com.flagnote
@@ -15,7 +15,7 @@
Demo project for Spring Boot
17
- 2022.0.0
+ 2022.0.1
diff --git a/src/main/java/com/flagnote/note/controller/NoteController.java b/src/main/java/com/flagnote/note/controller/NoteController.java
index a6cfdaf..46f160e 100644
--- a/src/main/java/com/flagnote/note/controller/NoteController.java
+++ b/src/main/java/com/flagnote/note/controller/NoteController.java
@@ -21,6 +21,7 @@ import com.flagnote.note.entity.NoteMeta;
import com.flagnote.note.service.NoteService;
import com.flagnote.note.utils.BizKeyUtils;
+import cn.hutool.core.util.ZipUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
@@ -124,5 +125,11 @@ public class NoteController {
noteService.deleteNote(key);
return key;
}
+
+ @RequestMapping(value = "/note/{key:[abcdefghijkmnopqrstuvwxyz23456789]{16}}.txt", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
+ public String getNoteText(@PathVariable("key") String key) {
+ return noteService.getNoteText(key);
+ }
+
}
diff --git a/src/main/java/com/flagnote/note/service/NoteService.java b/src/main/java/com/flagnote/note/service/NoteService.java
index 47d7609..1bf1cd7 100644
--- a/src/main/java/com/flagnote/note/service/NoteService.java
+++ b/src/main/java/com/flagnote/note/service/NoteService.java
@@ -10,4 +10,6 @@ public interface NoteService {
void deleteNote(String key);
+ String getNoteText(String key);
+
}
diff --git a/src/main/java/com/flagnote/note/service/NoteServiceImpl.java b/src/main/java/com/flagnote/note/service/NoteServiceImpl.java
index 5584f82..0219b47 100644
--- a/src/main/java/com/flagnote/note/service/NoteServiceImpl.java
+++ b/src/main/java/com/flagnote/note/service/NoteServiceImpl.java
@@ -1,5 +1,8 @@
package com.flagnote.note.service;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
@@ -10,8 +13,13 @@ import com.flagnote.note.repository.NoteMongoRepository;
import com.flagnote.note.utils.BizKeyUtils;
import com.flagnote.note.utils.SecretUtils;
+import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
+import cn.hutool.core.util.ByteUtil;
+import cn.hutool.core.util.CharUtil;
+import cn.hutool.core.util.CharsetUtil;
+import cn.hutool.core.util.ZipUtil;
@Service
public class NoteServiceImpl implements NoteService {
@@ -45,6 +53,15 @@ public class NoteServiceImpl implements NoteService {
return note;
}
+ @Override
+ public String getNoteText(String key) {
+ Note note = this.getNote(key);
+ if (null == note || ArrayUtil.isEmpty(note.getTextBytes())) {
+ return null;
+ }
+ return desNote(key, note);
+ }
+
@Override
public void saveNote(Note note) {
String mixKey = BizKeyUtils.mixKey(note.getKey());
@@ -66,11 +83,29 @@ public class NoteServiceImpl implements NoteService {
public void deleteNote(String key) {
String mixKey = BizKeyUtils.mixKey(key);
Note note = noteMongoRepository.findById(mixKey).orElse(null);
-
if (null != note && ArrayUtil.isNotEmpty(note.getTextBytes())) {
note.setState(0);
note.setTextBytes(null);
noteMongoRepository.save(note);
}
}
+
+ private String desNote(String key, Note note) {
+ if(null==note.getTextBytes()) {
+ return null;
+ }
+ String secretKey = BizKeyUtils.getSecretKey(key, "");
+
+ byte[] asebts = ZipUtil.unGzip(note.getTextBytes());
+ Charset cs = CharsetUtil.CHARSET_UTF_8;
+ ByteBuffer bb = ByteBuffer.allocate(asebts.length);
+ bb.put(asebts).flip();
+ CharBuffer cb = cs.decode(bb);
+ String result = SecretUtils.aesDecode(new String(cb.array()), secretKey);
+ if (result.startsWith("FLAGNOTE#")) {
+ return result.substring(9);
+ }
+ return "";
+ }
+
}
diff --git a/src/main/java/com/flagnote/note/utils/BizKeyUtils.java b/src/main/java/com/flagnote/note/utils/BizKeyUtils.java
index a6fb3b6..5ebad4f 100644
--- a/src/main/java/com/flagnote/note/utils/BizKeyUtils.java
+++ b/src/main/java/com/flagnote/note/utils/BizKeyUtils.java
@@ -4,6 +4,7 @@ import java.io.UnsupportedEncodingException;
import java.util.Date;
import org.springframework.util.DigestUtils;
+import org.springframework.util.StringUtils;
public class BizKeyUtils {
@@ -94,9 +95,14 @@ public class BizKeyUtils {
public static String getSecretKey(String key, String password) {
- return md5(key + md5(MIX_STRING + password));
+ if (!StringUtils.hasText(password)) {
+ password = key;
+ }
+ String aesString = SecretUtils.aesEncode(password, key);
+ return md5(aesString+"_F1agn0te");
}
+
public static void main(String[] args) {
int c = 0;
diff --git a/src/main/java/com/flagnote/note/utils/SecretUtils.java b/src/main/java/com/flagnote/note/utils/SecretUtils.java
index b97c32c..4919b3d 100644
--- a/src/main/java/com/flagnote/note/utils/SecretUtils.java
+++ b/src/main/java/com/flagnote/note/utils/SecretUtils.java
@@ -1,21 +1,55 @@
package com.flagnote.note.utils;
+import java.io.UnsupportedEncodingException;
+import java.security.InvalidKeyException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;
public class SecretUtils {
- private static AES aes = new AES(Mode.ECB, Padding.PKCS5Padding,
+ private static AES aesNote = new AES(Mode.ECB, Padding.PKCS5Padding,
new SecretKeySpec("b9t3pzbmybc66cba".getBytes(), "AES"));
-
+
+
public static byte[] encodeNote(byte[] text) {
- return aes.encrypt(text);
+ return aesNote.encrypt(text);
}
public static byte[] decodeNote(byte[] text) {
- return aes.decrypt(text);
+ return aesNote.decrypt(text);
}
+
+ public static String aesEncode(String word,String keyWord) {
+ AES aes = new AES(Mode.ECB, Padding.PKCS5Padding,
+ new SecretKeySpec(keyWord.getBytes(), "AES"));
+ return aes.encryptHex(word);
+ }
+
+ public static String aesDecode(String word,String keyWord) {
+ AES aes = new AES(Mode.ECB, Padding.PKCS5Padding,
+ new SecretKeySpec(keyWord.getBytes(), "AES"));
+ return new String(aes.decrypt(HexUtil.decodeHex(word)));
+ }
+
+ public static void main(String[] args) {
+ System.out.println(aesEncode("kaxfn73w2492eb2a","kaxfn73w2492eb2a"));;
+ AES aesNote = new AES(Mode.ECB, Padding.PKCS5Padding,
+ new SecretKeySpec("kaxfn73w2492eb2a".getBytes(), "AES"));
+ System.out.println(aesNote.encryptBase64("kaxfn73w2492eb2a"));
+ System.out.println(new String(aesNote.decrypt(Base64.decode("ycaXaJnQZz9sq/3ezdEB66s4rZv9POL4Q9HRIOWtQPo="))));
+ }
+
}