-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassSave.go
More file actions
163 lines (153 loc) · 3.45 KB
/
passSave.go
File metadata and controls
163 lines (153 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"math/rand"
"encoding/base64"
"strings"
"io/ioutil"
"errors"
"os"
"fmt"
"flag"
)
var cs []rune=[]rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
func main(){
act:=flag.String("act","e","-act=e表示加密 -act=d表示解密 默认解密")
pass:=flag.String("pass","","-pass 表示密码,解密会用到")
sF:=flag.String("sF","","-sF 源文件")
dF:=flag.String("dF","","-dF 处理后的文件")
flag.Parse()
// fmt.Printf("act\t:\t%s\tpass\t:\t%s\tsF\t:\t%s\tdF\t:\t%s\n",*act,*pass,*sF,*dF)
params:=map[string]string{"act":*act,"pass":*pass,"sF":*sF,"dF":*dF}
for k,v:=range params{
if len(v)<1{
fmt.Println("缺少参数:",k)
}
}
err:=error(nil)
switch(*act){
case "e":{
err=encFile(*sF,*pass,*dF)
}
case "d":{
err=decFile(*sF,*pass,*dF)
}
case "help":{
fmt.Println(`命令格式:passSave(.exe) -act=act -pass=pass -sF=sF -dF=dF`)
}
default:{
err=errors.New("未识别的指令 调用 -help 查看帮助")
}
}
if err!=nil{
fmt.Println(err.Error())
}
}
//pick a Seed String
func randSeed()string{
l:=len(cs)
cs1:=make([]rune,l)
rcs:=make([]rune,l)
copy(cs1,cs)
for i:=0;i<l;i++{
ll:=len(cs1)
idx:=rand.Intn(ll)
rcs[i]=cs1[idx]
cs1_c:=make([]rune,ll-1)
copy(cs1_c,cs1[:idx])
if idx<ll-1{
for x:=idx+1;x<ll;x++{
cs1_c[x-1]=cs1[x]
}
}
cs1=cs1_c
if len(cs1_c)>200{
break;
}
}
return string(rcs)
}
func readAllLines(file string)(res []string, err error){
if data,err1:=ioutil.ReadFile(file);err!=nil{
res,err=nil,err1
}else{
str:=string(data)
lines:=strings.Split(str,"\n")
res,err=lines,nil
}
return res,err
}
//check password
func checkPass(pass string)error{
if passlen:=len(pass);passlen<8||passlen>18{
return errors.New("密码长度应该在8-18之间")
}
return nil
}
func encrypt(content []byte,seed []byte)[]byte{
l:=len(seed)
for i,b:=range content{
content[i]=b^seed[i%l]
}
return content;
}
func encFile(oriFile ,password ,dstFile string)error{
//1.check if pass valid
if err:=checkPass(password);err!=nil{
return err
}
//3.give a random base64 seed
seed:=randSeed()
//4.seed enc the password
encoder:=base64.NewEncoding(seed)
contentEncSeed:=encoder.EncodeToString([]byte(password))
header:=seed[:32]+contentEncSeed+seed[32:]
//5.read File Contents
buf,err:=ioutil.ReadFile(oriFile)
if err!=nil{
return err
}
out:=encrypt(buf,[]byte(contentEncSeed))
outContent:=[]byte(header)
outContent=append(outContent,byte('\n'))
outContent=append(outContent,out...)
if err=ioutil.WriteFile(dstFile,outContent,os.ModePerm);err!=nil{
return err
}
return nil
}
func decFile(oriFile ,password ,dstFile string)error{
data,err1:=ioutil.ReadFile(oriFile);
if err1!=nil{
return err1
}
headerIndex:=-1;
for i:=0;i<85;i++{
if(data[i]==byte('\n')){
headerIndex=i;
break;
}
}
if(headerIndex==-1){
return errors.New("解析错误")
}
header:=string(data[:headerIndex])
headerl:=len(header)
seed:=header[0:32]+header[headerl-32:]
//4.seed enc the password
encoder:=base64.NewEncoding(seed)
dout,err1:=encoder.DecodeString(header[32:headerl-32])
if err1!=nil{
return err1
}
storePass:=string(dout)
if storePass!=password{
return errors.New("incorrect password")
}
contentEncSeed:=encoder.EncodeToString([]byte(password))
contentByteArr:=data[headerIndex+1:]
out:=encrypt(contentByteArr,[]byte(contentEncSeed))
if err:=ioutil.WriteFile(dstFile,out,os.ModePerm);err!=nil{
return err
}
return nil
}