zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package util
 
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int encfile(char *in_filename, char *pwd, char *out_filename) {
    if (in_filename ==NULL || pwd == NULL || out_filename ==NULL) {
        printf("invalid argument!\n");
        return -3;
    }
    if (strlen(pwd)<6) {
        printf("pwd lenth must >=6\n");
        return -3;
    }
 
    FILE *fp1, *fp2;
    register char ch;
    int j =0;
    int j0=0;
    fp1 = fopen(in_filename, "r");
    if(fp1==NULL) {
        printf("cannot open in-file\n");
        return -1;
    }
    fp2=fopen(out_filename, "w");
    if(fp2==NULL) {
        printf("cannot open or create out-file\n");
        return -2;
    }
    while(pwd[++j0]);
    ch = fgetc(fp1);
    while(!feof(fp1)) {
        if(j0>6) {
            j0=0;
        }
        ch +=pwd[j0++];
        fputc(ch, fp2);
        ch = fgetc(fp1);
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}
 
int decfile(char *in_filename, char *pwd, char *out_filename) {
    if(in_filename == NULL || pwd == NULL || out_filename == NULL) {
        printf("invalid argument!\n");
        return -3;
    }
    if (strlen(pwd) <6) {
        printf("pwd lenth must >=6\n");
        return -3;
    }
 
    FILE *fp1, *fp2;
    register char ch;
    int j =0;
    int j0=0;
    fp1 = fopen(in_filename, "r");
    if(fp1==NULL) {
        printf("cannot open in-file\n");
        return -1;
    }
    fp2=fopen(out_filename, "w");
    if(fp2==NULL) {
        printf("cannot open or create out-file\n");
        return -2;
    }
    while(pwd[++j0]);
    ch = fgetc(fp1);
    while(!feof(fp1)) {
        if(j0>6) {
            j0=0;
        }
        ch -=pwd[j0++];
        fputc(ch, fp2);
        ch = fgetc(fp1);
    }
    fclose(fp1);
    fclose(fp2);
    return 0;
}
 
//将明文加密后写入到文件中
int writeContent2EncFile(char *in_data, char *pwd, char *in_filename) {
    if (in_data == NULL || pwd == NULL || in_filename == NULL) {
        printf("invalid argument!\n");
        return -3;
    }
    if (strlen(pwd) <6) {
        printf("pwd lenth must >=6\n");
        return -3;
    }
    FILE *fp = fopen(in_filename, "w");
    if(fp == NULL) {
        printf("cannot open file %s \n", in_filename);
        return -1;
    }
    int j =0;
    while(pwd[++j]);
    int i=0;
    int len = strlen(in_data);
    register char ch;
    for(i=0;i<len;i++) {
        ch = in_data[i];
        if(j>6) {
            j=0;
        }
        ch +=pwd[j++];
        fputc(ch, fp);
    }
    fclose(fp);
    return 0;
}
 
//获取加密文件的内容
int readEncryptFile(char *in_filename, char *pwd, void **outData, int *len) {
    if (in_filename ==NULL || pwd == NULL || outData == NULL || len == NULL) {
        printf("invalid argument!\n");
        return -3;
    }
    if (strlen(pwd) <6) {
        printf("pwd lenth must >=6\n");
        return -3;
    }
 
    FILE *fp = fopen(in_filename, "r");
    if(fp == NULL) {
        printf("cannot open file %s \n", in_filename);
        return -1;
    }
    fseek(fp, 0, SEEK_END);
    int size = ftell(fp);
    char *content = (char*)malloc(sizeof(char)*size);
    if(content == NULL) {
        printf("malloc file size:%d err!!!\n", size);
        fclose(fp);
        return -2;
    }
    memset(content, 0, size);
    fseek(fp, 0, SEEK_SET);
    int j = 0;
    while(pwd[++j]);
    char ch;
    int n = 0;
    while(!feof(fp)) {
        ch = fgetc(fp);
        if(j>6) {
            j=0;
        }
        ch -=pwd[j++];
        content[n++] = ch;
    }
 
    *outData = content;
    *len = size;
    fclose(fp);
    return 0;
}
 
*/
import "C"
 
import (
    "strconv"
    "unsafe"
    "errors"
)
 
const pwd = "123456"
func CallEncFile(srcAbsFileName string, outAbsPath string) error {
    inPath := C.CString(srcAbsFileName)
    cpwd := C.CString(pwd)
    outPath := C.CString(outAbsPath)
    defer func() {
        C.free(unsafe.Pointer(inPath))
        C.free(unsafe.Pointer(outPath))
        C.free(unsafe.Pointer(cpwd))
    }()
    cret := C.encfile(inPath, cpwd, outPath)
    if cret!=0 {
        if cret == -1 {
            return errors.New("cannot open original file")
        } else if cret == -2 {
            return errors.New("cannot open or create output file")
        } else {
            return errors.New("unknown enc file err")
        }
    }
    return nil
}
 
func CallDecFile(srcAbsFileName string, outAbsPath string) error {
    inPath := C.CString(srcAbsFileName)
    cpwd := C.CString(pwd)
    outPath := C.CString(outAbsPath)
    defer func() {
        C.free(unsafe.Pointer(inPath))
        C.free(unsafe.Pointer(outPath))
        C.free(unsafe.Pointer(cpwd))
    }()
    cret := C.decfile(inPath, cpwd, outPath)
    if cret!=0 {
        if cret == -1 {
            return errors.New("cannot open original file")
        } else if cret == -2 {
            return errors.New("cannot open or create output file")
        } else {
            return errors.New("unknown enc file err")
        }
    }
    return nil
}
 
func CallEncContent2File(data string, fpath string) error {
    cpath := C.CString(fpath)
    cpwd := C.CString(pwd)
    cdata := C.CString(data)
    defer func() {
        C.free(unsafe.Pointer(cpath))
        C.free(unsafe.Pointer(cpwd))
        C.free(unsafe.Pointer(cdata))
    }()
    cRet := C.writeContent2EncFile(cdata, cpwd, cpath)
    if cRet != 0 {
        return errors.New("C.writeContent2EncFile ret:"+strconv.Itoa(int(cRet)))
    }
    return nil
}
 
//解密获取文件内容
func CallDecFileContent(fpath string) ([]byte, error) {
    inPath := C.CString(fpath)
    cpwd := C.CString(pwd)
    cData := unsafe.Pointer(nil)
    cLen := C.int(0)
    defer func() {
        C.free(unsafe.Pointer(inPath))
        C.free(unsafe.Pointer(cpwd))
        C.free(unsafe.Pointer(cData))
    }()
    cRet := C.readEncryptFile(inPath, cpwd, &cData, &cLen)
    if cRet != 0 {
        return nil, errors.New("cgo call ret:"+ strconv.Itoa(int(cRet)))
    }
 
    fileData := C.GoBytes(cData, cLen)
 
    return fileData, nil
}