summaryrefslogtreecommitdiff
path: root/deencrypt.pas
blob: 3e30911761e95c30c33122d2569ced3a644e2089 (plain)
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
unit deencrypt;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;

// requires gpg

function encrypt(content, recipient: string): string;
function decrypt(content: string): string;

implementation

uses
  Process, Math;

function encrypt(content, recipient: string): string;
var
  p: tProcess;
  s: string;
begin
  result:='';
  p:=tProcess.create(nil);
  with p do begin
    executable:='gpg';
    parameters.add('-e');
    parameters.add('-q');
    parameters.add('--batch');
    parameters.add('--no-tty');
    parameters.add('-r');
    parameters.add(recipient);
    parameters.add('-o');
    parameters.add('-');
    parameters.add('-');
    options:=options + [poUsePipes];
    execute;
    closeStderr;
  end;
  while p.running or (p.output.numBytesAvailable>0) do begin
    if p.output.numBytesAvailable>0 then begin
      setLength(s,p.output.numBytesAvailable);
      setLength(s,p.output.read(s[1],length(s)));
      result:=result+s;
      continue;
    end;
    if content<>'' then begin
      delete(content,1,p.input.write(content[1],min(128,length(content))));
      if content='' then
        p.closeInput;
      continue;
    end;
    sleep(10);
  end;
  p.waitOnExit;
  p.free;

  content:=result;
  result:='';
  p:=tProcess.create(nil);
  with p do begin
    executable:='base64';
    parameters.add('-w');
    parameters.add('0');
    options:=options + [poUsePipes];
    execute;
    closeStderr;
  end;
  while p.running or (p.output.numBytesAvailable>0) do begin
    if p.output.numBytesAvailable>0 then begin
      setLength(s,p.output.numBytesAvailable);
      setLength(s,p.output.read(s[1],length(s)));
      result:=result+s;
      continue;
    end;
    if content<>'' then begin
      delete(content,1,p.input.write(content[1],min(128,length(content))));
      if content='' then
        p.closeInput;
      continue;
    end;
    sleep(10);
  end;
  p.waitOnExit;
  p.free;
end;

function decrypt(content: string): string;
var
  p: tProcess;
  s: string;
begin
  result:='';
  p:=tProcess.create(nil);
  with p do begin
    executable:='base64';
    parameters.add('-d');
    options:=options + [poUsePipes];
    execute;
    closeStderr;
  end;
  while p.running or (p.output.numBytesAvailable>0) do begin
    if p.output.numBytesAvailable>0 then begin
      setLength(s,p.output.numBytesAvailable);
      setLength(s,p.output.read(s[1],length(s)));
      result:=result+s;
      continue;
    end;
    if content<>'' then begin
      delete(content,1,p.input.write(content[1],min(128,length(content))));
      if content='' then
        p.closeInput;
      continue;
    end;
    sleep(10);
  end;
  p.waitOnExit;
  p.free;

  content:=result;
  result:='';
  p:=tProcess.create(nil);
  with p do begin
    executable:='gpg';
    parameters.add('-d');
    parameters.add('-q');
    parameters.add('--batch');
    parameters.add('--no-tty');
    parameters.add('-o');
    parameters.add('-');
    parameters.add('-');
    options:=options + [poUsePipes];
    execute;
    closeStderr;
  end;
  while p.running or (p.output.numBytesAvailable>0) do begin
    if p.output.numBytesAvailable>0 then begin
      setLength(s,p.output.numBytesAvailable);
      setLength(s,p.output.read(s[1],length(s)));
      result:=result+s;
      continue;
    end;
    if content<>'' then begin
      delete(content,1,p.input.write(content[1],min(128,length(content))));
      if content='' then
        p.closeInput;
      continue;
    end;
    sleep(10);
  end;
  p.waitOnExit;
  p.free;
end;

end.