diff options
author | Erich Eckner <git@eckner.net> | 2019-04-05 15:13:46 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2019-04-05 15:13:46 +0200 |
commit | a5253b3bc9bc2a0f75db8601346bb9ccdcc26ce2 (patch) | |
tree | 37a3285b09d62601abb25fc3047aeabf84165a92 | |
parent | 44fb734adb505a12298db20853012b357c9661c0 (diff) | |
download | units-a5253b3bc9bc2a0f75db8601346bb9ccdcc26ce2.tar.xz |
deencrypt.pas neu
-rw-r--r-- | deencrypt.pas | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/deencrypt.pas b/deencrypt.pas new file mode 100644 index 0000000..3e30911 --- /dev/null +++ b/deencrypt.pas @@ -0,0 +1,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. + |