From ab1589b194f59204e565a30fd2bce2db01addad6 Mon Sep 17 00:00:00 2001 From: godo Date: Sat, 7 Dec 2024 00:15:02 +0800 Subject: [PATCH] change pwd --- godo/files/pwd.go | 22 ++++++++++++++++------ godo/libs/filecode.go | 14 +++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/godo/files/pwd.go b/godo/files/pwd.go index 006d6b0..9470409 100644 --- a/godo/files/pwd.go +++ b/godo/files/pwd.go @@ -45,10 +45,13 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { if !isPwd { // 未加密文件,直接返回 - text = base64.StdEncoding.EncodeToString(fileData) - if len(text)%8 == 0 { - text += " " + if len(fileData) > 0 { + text = base64.StdEncoding.EncodeToString(fileData) } + + // if len(text) > 7 && len(text)%8 == 0 { + // text += " " + // } //log.Printf("fileData: %s", content) libs.SuccessMsg(w, text, "文件读取成功") return @@ -77,9 +80,16 @@ func HandleReadFile(w http.ResponseWriter, r *http.Request) { return } } - if len(decodeFile)%8 == 0 { - decodeFile = decodeFile + " " + // if len(decodeFile)%8 == 0 { + // decodeFile = decodeFile + " " + // } + if len(decodeFile) > 0 { + decodeFile = base64.StdEncoding.EncodeToString([]byte(decodeFile)) } + + // if len(decodeFile) > 7 && len(decodeFile)%8 == 0 { + // decodeFile += " " + // } libs.SuccessMsg(w, decodeFile, "加密文件读取成功") } func HandleWriteFile(w http.ResponseWriter, r *http.Request) { @@ -145,7 +155,7 @@ func HandleWriteFile(w http.ResponseWriter, r *http.Request) { // 直接写入新内容 file.Truncate(0) file.Seek(0, 0) - log.Printf("write file content%v", content) + //log.Printf("write file content%v", content) _, err = file.Write(content) if err != nil { libs.ErrorMsg(w, "Failed to write file content.") diff --git a/godo/libs/filecode.go b/godo/libs/filecode.go index 7340f06..3fa32e5 100644 --- a/godo/libs/filecode.go +++ b/godo/libs/filecode.go @@ -99,7 +99,7 @@ func EncodeFile(password string, longText string) (string, error) { } // 使用二级密码加密数据 - encryptedText, err := EncryptDataWithCBC(longText, secondaryPassword) + encryptedText, err := EncryptDataWithGCM(longText, secondaryPassword) if err != nil { return "", fmt.Errorf("加密数据失败:%v", err) } @@ -155,7 +155,7 @@ func DecodeFile(password string, encryptedData string) (string, error) { } // 使用二级密码解密数据 - decryptedText, err := DecryptDataWithCBC(encryptedText, secondaryPassword) + decryptedText, err := DecryptDataWithGCM(encryptedText, secondaryPassword) if err != nil { return "", fmt.Errorf("解密文本失败:%v", err) } @@ -327,7 +327,7 @@ func DecodeFileWithSecondaryPassword(password string, encryptedData string) (str } // 使用二级密码解密文本 - decryptedText, err := DecryptDataWithCBC(encryptedText, secondaryPassword) + decryptedText, err := DecryptDataWithGCM(encryptedText, secondaryPassword) if err != nil { return "", fmt.Errorf("解密文本失败:%v", err) } @@ -355,8 +355,8 @@ func EncryptSecondaryPassword(secondaryPassword string, publicKey *rsa.PublicKey return base64.URLEncoding.EncodeToString(ciphertext), nil } -// EncryptDataWithCBC 使用二级密码和CBC模式加密数据 -func EncryptDataWithCBC(data, secondaryPassword string) (string, error) { +// EncryptDataWithGCM 使用二级密码和GCM模式加密数据 +func EncryptDataWithGCM(data, secondaryPassword string) (string, error) { // 确保 secondaryPassword 长度为 32 字节 if len(secondaryPassword) < 32 { return "", fmt.Errorf("secondary password too short") @@ -382,8 +382,8 @@ func EncryptDataWithCBC(data, secondaryPassword string) (string, error) { return base64.URLEncoding.EncodeToString(ciphertext), nil } -// DecryptDataWithCBC 使用二级密码和CBC模式解密数据 -func DecryptDataWithCBC(encryptedData, secondaryPassword string) (string, error) { +// DecryptDataWithGCM 使用二级密码和GCM模式解密数据 +func DecryptDataWithGCM(encryptedData, secondaryPassword string) (string, error) { // 确保 secondaryPassword 长度为 32 字节 if len(secondaryPassword) < 32 { return "", fmt.Errorf("secondary password too short")