Skip to content
This repository was archived by the owner on Mar 14, 2024. It is now read-only.

Commit 8d78433

Browse files
little update 📝
1 parent 058d977 commit 8d78433

File tree

1 file changed

+75
-33
lines changed

1 file changed

+75
-33
lines changed

PDF_Decrypt.go

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"flag"
56
"fmt"
67
"io/fs"
@@ -17,12 +18,13 @@ import (
1718

1819
var (
1920
buildTime, commitId, versionData, author string
20-
inputFolder, outputFolder, passWord string
21+
inputFolder, outputFolder, passWords string
2122
help, version bool
2223
)
2324

2425
var (
25-
fileList []string
26+
fileList []string
27+
errorList []string
2628
)
2729

2830
func Flag() {
@@ -32,7 +34,7 @@ func Flag() {
3234
flag.BoolVar(&version, "version", false, "PDF_Decrypt version")
3335
flag.StringVar(&inputFolder, "in", "PDF", "in explorer")
3436
flag.StringVar(&outputFolder, "out", "out", "out explorer")
35-
flag.StringVar(&passWord, "pass", "123456", "password ('abc' | 'abc\\def\\ghi')")
37+
flag.StringVar(&passWords, "pass", "123456", "password ('abc' | 'abc\\def\\ghi')")
3638
}
3739

3840
func passWordLists(passWord string) []string {
@@ -51,17 +53,23 @@ func adsPath(folder string) string {
5153
return adspath
5254
}
5355

56+
func relPath(basePath, targPath string) (relPath string) {
57+
var err error
58+
if relPath, err = filepath.Rel(adsPath(basePath), targPath); err != nil {
59+
_, _ = color.New(color.FgYellow).Println(err)
60+
os.Exit(1)
61+
}
62+
return relPath
63+
}
64+
5465
func inputFolders(filePath string) []string {
5566
if err := filepath.Walk(adsPath(filePath), func(path string, info os.FileInfo, err error) error {
5667
if filepath.Ext(path) == ".pdf" {
5768
fileList = append(fileList, path)
5869
}
5970
if info.IsDir() {
6071
var outFileName, outFilePath string
61-
if outFileName, err = filepath.Rel(adsPath(inputFolder), path); err != nil {
62-
_, _ = color.New(color.FgYellow).Println(err)
63-
os.Exit(1)
64-
}
72+
outFileName = relPath(adsPath(inputFolder), path)
6573
outFilePath = filepath.Join(outputFolder, outFileName)
6674
if _, err := os.Stat(outFilePath); err != nil {
6775
if err := os.Mkdir(outFilePath, 0755); err != nil {
@@ -102,6 +110,9 @@ func outPutFolderClean(filePath string) {
102110
os.Exit(1)
103111
}
104112
if len(files) == 0 {
113+
if filePath == outputFolder {
114+
return
115+
}
105116
if err := os.Remove(filePath); err != nil {
106117
_, _ = color.New(color.FgYellow).Println(err)
107118
os.Exit(1)
@@ -119,16 +130,15 @@ func outPutFolderClean(filePath string) {
119130

120131
func Decrypt(fileList []string) {
121132
for _, v := range fileList {
122-
for _, p := range passWordLists(passWord) {
133+
for _, p := range passWordLists(passWords) {
123134
if DecryptFile(v, p) {
124135
break
125136
}
126137
}
127138
}
128-
129139
}
130140

131-
func DecryptFile(filePath, p string) bool {
141+
func DecryptFile(filePath, pass string) bool {
132142
//conf.UserPW = i
133143
filePath = adsPath(filePath)
134144
if !pdfcpu.MemberOf(pdfcpu.ConfigPath, []string{"default", "disable"}) {
@@ -138,30 +148,61 @@ func DecryptFile(filePath, p string) bool {
138148
}
139149
}
140150
conf := pdfcpu.NewDefaultConfiguration()
141-
conf.OwnerPW = p
151+
conf.OwnerPW = pass
142152
_, _ = color.New(color.FgCyan).Printf(" Decrypting: ")
143153
_, _ = color.New(color.FgWhite).Printf(path.Base(filePath))
144-
outFileRouter, _ := filepath.Rel(adsPath(inputFolder), filePath)
154+
outFileRouter := relPath(adsPath(inputFolder), filePath)
145155
outFilePath := path.Join(adsPath(outputFolder), outFileRouter)
146156
if err := api.DecryptFile(filePath, outFilePath, conf); err != nil {
147-
if strings.Contains(err.Error(), "not encrypted") {
148-
_, _ = color.New(color.FgGreen).Println("Not encrypted")
157+
type errorFile struct {
158+
Index int
159+
Error error
160+
Status bool
161+
}
162+
var (
163+
errFile = errorFile{
164+
Index: 0,
165+
Error: err,
166+
Status: false,
167+
}
168+
errPath string
169+
)
170+
if strings.Contains(errFile.Error.Error(), "not encrypted") {
171+
_, _ = color.New(color.FgGreen).Println(" Not encrypted")
149172
var l3 []byte
150173
if l3, err = ioutil.ReadFile(filePath); err != nil {
151-
_, _ = color.New(color.FgYellow).Println(err)
174+
_, _ = color.New(color.FgYellow).Println(" ", err)
152175
os.Exit(1)
153176
}
154177
if err = ioutil.WriteFile(outFilePath, l3, 0644); err != nil {
155-
_, _ = color.New(color.FgYellow).Println(err)
178+
_, _ = color.New(color.FgYellow).Println(" ", err)
156179
os.Exit(1)
157180
}
158181
return true
159182
}
160-
_, _ = color.New(color.FgYellow).Println(err)
183+
for i, v := range errorList {
184+
if strings.Contains(v, relPath(adsPath(inputFolder), filePath)) {
185+
errFile.Status = true
186+
errFile.Index = i
187+
}
188+
}
189+
errPath = relPath(adsPath(inputFolder), filePath)
190+
if strings.Contains(errFile.Error.Error(), "correct password") {
191+
errFile.Error = errors.New("PassError:" + pass)
192+
errPath += ":PassError\n"
193+
} else {
194+
errPath += fmt.Sprintf(":[%v]\n", errFile.Error.Error())
195+
}
196+
if errFile.Status {
197+
errorList[errFile.Index] = errPath
198+
} else {
199+
errorList = append(errorList, errPath)
200+
}
201+
_, _ = color.New(color.FgYellow).Println(fmt.Sprintf(" %v", errFile.Error))
161202
return false
162203
}
163204
_, _ = color.New(color.FgGreen).Printf(" Decrypted!")
164-
_, _ = color.New(color.FgWhite).Println(" PassWord:", p)
205+
_, _ = color.New(color.FgWhite).Println(" PassWord:", pass)
165206
return true
166207
}
167208

@@ -172,20 +213,20 @@ func main() {
172213
}()
173214
Flag()
174215
flag.Parse()
175-
if help || len(os.Args) == 1 {
176-
fmt.Println(` Usage of PDF_Decrypt:
177-
-help -h
178-
Display help information
179-
-in string
180-
in explorer
181-
-out string
182-
out explorer
183-
-pass string
184-
password (abc | abc\\def\\ghi)
185-
-version -v
186-
PDF_Decrypt version`)
187-
return
188-
}
216+
//if help || len(os.Args) == 1 {
217+
// fmt.Println(` Usage of PDF_Decrypt:
218+
//-help -h
219+
// Display help information
220+
//-in string
221+
// in explorer
222+
//-out string
223+
// out explorer
224+
//-pass string
225+
// password (abc | abc\\def\\ghi)
226+
//-version -v
227+
// PDF_Decrypt version`)
228+
// return
229+
//}
189230
if version {
190231
_, _ = color.New(color.FgMagenta).Println(" | Version:", versionData)
191232
_, _ = color.New(color.FgMagenta).Println(" | BuildTime:", buildTime)
@@ -196,8 +237,9 @@ func main() {
196237
_, _ = color.New(color.FgMagenta).Println(" Start PDF_Decrypt ...")
197238
outPutFolder(outputFolder)
198239
Decrypt(inputFolders(inputFolder))
199-
for i := 0; i < 15; i++ {
240+
for i := 0; i < 50; i++ {
200241
outPutFolderClean(outputFolder)
201242
}
202243
_, _ = color.New(color.FgGreen).Printf("\n Decrypted %v File\n", len(fileList))
244+
_, _ = color.New(color.FgGreen).Printf(" Decrypt Error %v Files: %v\n", len(errorList), errorList)
203245
}

0 commit comments

Comments
 (0)