-
Notifications
You must be signed in to change notification settings - Fork 1
/
secshare.c
71 lines (69 loc) · 1.65 KB
/
secshare.c
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
#include "secshare.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int fdin;
int fdout;
int verbose=1;
int main(int argc,char**argv)
{
int amt=0,doenc,i;
char buf[1024];
if(argc<3){
fprintf(stderr,"Usage: %s amount filename secrets...\n"
"\tamount=amount of necessary secrets, negative for decryption\n"
"\tfilename=name of the file to be en-/de-crypted\n"
"\tsecrets=names of files with secrets\n"
"\t\tfor encryption it will create as many secrets,\n"
"\t\tfor decryption it will read the first $amount\n",*argv);
return 1;
}
sscanf(argv[1],"%i",&amt);
if(amt>-2 && amt<2){
fprintf(stderr,"A secret must be shared between at least two persons.\n");
return 1;
}
if(amt<0){
doenc=0;
amt=-amt;
}else{
doenc=1;
}
if(argc<(3+amt)){
fprintf(stderr,"Not enough secrets specified.\n");
return 1;
}
if(doenc){/*encrypt*/
fdin=open(argv[2],O_RDONLY);
if(fdin<0){
fprintf(stderr,"Unable to open file %s.\n",argv[2]);
return 1;
}
fdout=open(strcat(strcpy(buf,argv[2]),".enc"),O_RDWR|O_CREAT|O_TRUNC,0666);
if(fdout<0){
fprintf(stderr,"Unable to create file %s.\n",buf);
return 1;
}
secshare_encrypt();
sharevector(amt);
for(i=3;i<argc;i++)share(argv[i]);
}else{/*decrypt*/
sharematrix(amt);
for(i=3;i<argc;i++)unshare(argv[i]);
sharecalc();
fdin=open(argv[2],O_RDONLY);
if(fdin<0){
fprintf(stderr,"Unable to open file %s.\n",argv[2]);
return 1;
}
fdout=open(strcat(strcpy(buf,argv[2]),".dec"),O_RDWR|O_CREAT|O_TRUNC,0666);
if(fdout<0){
fprintf(stderr,"Unable to create file %s.\n",buf);
return 1;
}
secshare_decrypt();
}
return 0;
}