1
1
package main
2
2
3
3
import (
4
+ "errors"
4
5
"flag"
5
6
"fmt"
6
7
"io/fs"
@@ -17,12 +18,13 @@ import (
17
18
18
19
var (
19
20
buildTime , commitId , versionData , author string
20
- inputFolder , outputFolder , passWord string
21
+ inputFolder , outputFolder , passWords string
21
22
help , version bool
22
23
)
23
24
24
25
var (
25
- fileList []string
26
+ fileList []string
27
+ errorList []string
26
28
)
27
29
28
30
func Flag () {
@@ -32,7 +34,7 @@ func Flag() {
32
34
flag .BoolVar (& version , "version" , false , "PDF_Decrypt version" )
33
35
flag .StringVar (& inputFolder , "in" , "PDF" , "in explorer" )
34
36
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')" )
36
38
}
37
39
38
40
func passWordLists (passWord string ) []string {
@@ -51,17 +53,23 @@ func adsPath(folder string) string {
51
53
return adspath
52
54
}
53
55
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
+
54
65
func inputFolders (filePath string ) []string {
55
66
if err := filepath .Walk (adsPath (filePath ), func (path string , info os.FileInfo , err error ) error {
56
67
if filepath .Ext (path ) == ".pdf" {
57
68
fileList = append (fileList , path )
58
69
}
59
70
if info .IsDir () {
60
71
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 )
65
73
outFilePath = filepath .Join (outputFolder , outFileName )
66
74
if _ , err := os .Stat (outFilePath ); err != nil {
67
75
if err := os .Mkdir (outFilePath , 0755 ); err != nil {
@@ -102,6 +110,9 @@ func outPutFolderClean(filePath string) {
102
110
os .Exit (1 )
103
111
}
104
112
if len (files ) == 0 {
113
+ if filePath == outputFolder {
114
+ return
115
+ }
105
116
if err := os .Remove (filePath ); err != nil {
106
117
_ , _ = color .New (color .FgYellow ).Println (err )
107
118
os .Exit (1 )
@@ -119,16 +130,15 @@ func outPutFolderClean(filePath string) {
119
130
120
131
func Decrypt (fileList []string ) {
121
132
for _ , v := range fileList {
122
- for _ , p := range passWordLists (passWord ) {
133
+ for _ , p := range passWordLists (passWords ) {
123
134
if DecryptFile (v , p ) {
124
135
break
125
136
}
126
137
}
127
138
}
128
-
129
139
}
130
140
131
- func DecryptFile (filePath , p string ) bool {
141
+ func DecryptFile (filePath , pass string ) bool {
132
142
//conf.UserPW = i
133
143
filePath = adsPath (filePath )
134
144
if ! pdfcpu .MemberOf (pdfcpu .ConfigPath , []string {"default" , "disable" }) {
@@ -138,30 +148,61 @@ func DecryptFile(filePath, p string) bool {
138
148
}
139
149
}
140
150
conf := pdfcpu .NewDefaultConfiguration ()
141
- conf .OwnerPW = p
151
+ conf .OwnerPW = pass
142
152
_ , _ = color .New (color .FgCyan ).Printf (" Decrypting: " )
143
153
_ , _ = color .New (color .FgWhite ).Printf (path .Base (filePath ))
144
- outFileRouter , _ := filepath . Rel (adsPath (inputFolder ), filePath )
154
+ outFileRouter := relPath (adsPath (inputFolder ), filePath )
145
155
outFilePath := path .Join (adsPath (outputFolder ), outFileRouter )
146
156
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" )
149
172
var l3 []byte
150
173
if l3 , err = ioutil .ReadFile (filePath ); err != nil {
151
- _ , _ = color .New (color .FgYellow ).Println (err )
174
+ _ , _ = color .New (color .FgYellow ).Println (" " , err )
152
175
os .Exit (1 )
153
176
}
154
177
if err = ioutil .WriteFile (outFilePath , l3 , 0644 ); err != nil {
155
- _ , _ = color .New (color .FgYellow ).Println (err )
178
+ _ , _ = color .New (color .FgYellow ).Println (" " , err )
156
179
os .Exit (1 )
157
180
}
158
181
return true
159
182
}
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 ))
161
202
return false
162
203
}
163
204
_ , _ = color .New (color .FgGreen ).Printf (" Decrypted!" )
164
- _ , _ = color .New (color .FgWhite ).Println (" PassWord:" , p )
205
+ _ , _ = color .New (color .FgWhite ).Println (" PassWord:" , pass )
165
206
return true
166
207
}
167
208
@@ -172,20 +213,20 @@ func main() {
172
213
}()
173
214
Flag ()
174
215
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
+ // }
189
230
if version {
190
231
_ , _ = color .New (color .FgMagenta ).Println (" | Version:" , versionData )
191
232
_ , _ = color .New (color .FgMagenta ).Println (" | BuildTime:" , buildTime )
@@ -196,8 +237,9 @@ func main() {
196
237
_ , _ = color .New (color .FgMagenta ).Println (" Start PDF_Decrypt ..." )
197
238
outPutFolder (outputFolder )
198
239
Decrypt (inputFolders (inputFolder ))
199
- for i := 0 ; i < 15 ; i ++ {
240
+ for i := 0 ; i < 50 ; i ++ {
200
241
outPutFolderClean (outputFolder )
201
242
}
202
243
_ , _ = 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 )
203
245
}
0 commit comments