Encrypting and Decrypting using SB
Posted: Sat 11 Feb 2017 23:31
Hi all,
SecureBridge 6.1.2
I'm having trouble encrypting & decryption a file using SB. Please see the code below.
After encrypting then decrypting the file, I get something close but not identical to the original file.
I also tried using a buffer specifically for encrypting/decrypting as the last buffer more often than not will only be partially filled by the FileStream.Read. If I use cryptBytes instead of inBytes, an exception is throw "Key not ready". Suggestions / recommendations welcome.
kind regards,
Jan
procedure TMain.Encrypt(const sFileName: string);
const
BUFSIZE = 245; // Emperically determined
var
ppKey: TScKey;
fsInput, fsOutput: TFileStream;
inBytes, outBytes, cryptBytes: TBytes;
iRead, iTotalRead: integer;
begin
iRead := 0;
ppKey := TScKey.Create(nil);
try
try
with ppKey do
begin
ImportFrom(edPublicKey.Text);
KeyName := 'PublicKey';
Ready := True;
end;
meLog.Lines.Add(Format('Encrypting %s using public key %s', [sFileName, edPublicKey.Text]));
fsInput := TFileStream.Create(sFileName, fmOpenRead);
fsOutput := TFileStream.Create(sFileName + '.enc', fmCreate);
SetLength (inBytes, BUFSIZE);
while fsInput.Position < fsInput.Size do
begin
iRead := fsInput.Read(inBytes, BUFSIZE); iTotalRead := iTotalRead + iRead;
//SetLength (cryptBytes, iRead);
//Move (inBytes, cryptBytes, iRead);
//outBytes := ppKey.Encrypt(cryptBytes); // this throws exception "Key not ready???"
outBytes := ppKey.Encrypt(inBytes);
fsOutput.Write(outBytes, Length(outBytes));
end;
meLog.Lines.Add(Format('Encrypt successful. In.S,P=(%d,%d), Out.S,P=(%d,%d). Read: %d',
[fsInput.Size, fsInput.Position, fsOutput.Size, fsOutput.Position, iTotalRead]));
except
on E: Exception do
begin
meLog.Lines.Add (Format('Encrypt unsuccessful: %s', [E.Message]));
end;
end;
finally
if Assigned(fsInput) then fsInput.Free;
if Assigned(fsOutput) then fsOutput.Free;
ppKey.Free;
end;
end;
procedure TMain.Decrypt(const sFileName: string);
const
BUFSIZE = 256; // Emperically determined
var
ppKey: TScKey;
fsInput, fsOutput: TFileStream;
inBytes, outBytes, cryptBytes: TBytes;
iRead, iTotalRead: integer;
begin
iRead := 0;
ppKey := TScKey.Create(nil);
try
try
with ppKey do
begin
ImportFrom(edPrivateKey.Text);
KeyName := 'PrivateKey';
Ready := True;
end;
meLog.Lines.Add(Format('Decrypting %s using private key %s', [sFileName, edPrivateKey.Text]));
fsInput := TFileStream.Create(sFileName, fmOpenRead);
fsOutput := TFileStream.Create(sFileName + '.dec', fmCreate);
SetLength (inBytes, BUFSIZE);
while fsInput.Position < fsInput.Size do
begin
iRead := fsInput.Read(inBytes, BUFSIZE); iTotalRead := iTotalRead + iRead;
//SetLength (cryptBytes, iRead);
//Move (inBytes, cryptBytes, iRead);
//outBytes := ppKey.Decrypt(cryptBytes);
outBytes := ppKey.Decrypt(inBytes);
fsOutput.Write(outBytes, Length(outBytes));
end;
meLog.Lines.Add(Format('Decrypt successful. In.S,P=(%d,%d), Out.S,P=(%d,%d). Bytes read: %d',
[fsInput.Size, fsInput.Position, fsOutput.Size, fsOutput.Position, iTotalRead]));
except
on E: Exception do
begin
meLog.Lines.Add (Format('Decrypt unsuccessful: %s', [E.Message]));
end;
end;
finally
fsInput.Free;
fsOutput.Free;
ppKey.Free;
end;
end;
Encrypting G:\Test\UUID.txt using public key G:\public.key
Encrypt successful. In.S,P=(1506,1506), Out.S,P=(1792,1792). Read: 1506
Decrypting G:\Test\UUID.txt.enc using private key G:\private.key
Decrypt successful. In.S,P=(1792,1792), Out.S,P=(1715,1715). Bytes read: 1792
SecureBridge 6.1.2
I'm having trouble encrypting & decryption a file using SB. Please see the code below.
After encrypting then decrypting the file, I get something close but not identical to the original file.
I also tried using a buffer specifically for encrypting/decrypting as the last buffer more often than not will only be partially filled by the FileStream.Read. If I use cryptBytes instead of inBytes, an exception is throw "Key not ready". Suggestions / recommendations welcome.
kind regards,
Jan
procedure TMain.Encrypt(const sFileName: string);
const
BUFSIZE = 245; // Emperically determined
var
ppKey: TScKey;
fsInput, fsOutput: TFileStream;
inBytes, outBytes, cryptBytes: TBytes;
iRead, iTotalRead: integer;
begin
iRead := 0;
ppKey := TScKey.Create(nil);
try
try
with ppKey do
begin
ImportFrom(edPublicKey.Text);
KeyName := 'PublicKey';
Ready := True;
end;
meLog.Lines.Add(Format('Encrypting %s using public key %s', [sFileName, edPublicKey.Text]));
fsInput := TFileStream.Create(sFileName, fmOpenRead);
fsOutput := TFileStream.Create(sFileName + '.enc', fmCreate);
SetLength (inBytes, BUFSIZE);
while fsInput.Position < fsInput.Size do
begin
iRead := fsInput.Read(inBytes, BUFSIZE); iTotalRead := iTotalRead + iRead;
//SetLength (cryptBytes, iRead);
//Move (inBytes, cryptBytes, iRead);
//outBytes := ppKey.Encrypt(cryptBytes); // this throws exception "Key not ready???"
outBytes := ppKey.Encrypt(inBytes);
fsOutput.Write(outBytes, Length(outBytes));
end;
meLog.Lines.Add(Format('Encrypt successful. In.S,P=(%d,%d), Out.S,P=(%d,%d). Read: %d',
[fsInput.Size, fsInput.Position, fsOutput.Size, fsOutput.Position, iTotalRead]));
except
on E: Exception do
begin
meLog.Lines.Add (Format('Encrypt unsuccessful: %s', [E.Message]));
end;
end;
finally
if Assigned(fsInput) then fsInput.Free;
if Assigned(fsOutput) then fsOutput.Free;
ppKey.Free;
end;
end;
procedure TMain.Decrypt(const sFileName: string);
const
BUFSIZE = 256; // Emperically determined
var
ppKey: TScKey;
fsInput, fsOutput: TFileStream;
inBytes, outBytes, cryptBytes: TBytes;
iRead, iTotalRead: integer;
begin
iRead := 0;
ppKey := TScKey.Create(nil);
try
try
with ppKey do
begin
ImportFrom(edPrivateKey.Text);
KeyName := 'PrivateKey';
Ready := True;
end;
meLog.Lines.Add(Format('Decrypting %s using private key %s', [sFileName, edPrivateKey.Text]));
fsInput := TFileStream.Create(sFileName, fmOpenRead);
fsOutput := TFileStream.Create(sFileName + '.dec', fmCreate);
SetLength (inBytes, BUFSIZE);
while fsInput.Position < fsInput.Size do
begin
iRead := fsInput.Read(inBytes, BUFSIZE); iTotalRead := iTotalRead + iRead;
//SetLength (cryptBytes, iRead);
//Move (inBytes, cryptBytes, iRead);
//outBytes := ppKey.Decrypt(cryptBytes);
outBytes := ppKey.Decrypt(inBytes);
fsOutput.Write(outBytes, Length(outBytes));
end;
meLog.Lines.Add(Format('Decrypt successful. In.S,P=(%d,%d), Out.S,P=(%d,%d). Bytes read: %d',
[fsInput.Size, fsInput.Position, fsOutput.Size, fsOutput.Position, iTotalRead]));
except
on E: Exception do
begin
meLog.Lines.Add (Format('Decrypt unsuccessful: %s', [E.Message]));
end;
end;
finally
fsInput.Free;
fsOutput.Free;
ppKey.Free;
end;
end;
Encrypting G:\Test\UUID.txt using public key G:\public.key
Encrypt successful. In.S,P=(1506,1506), Out.S,P=(1792,1792). Read: 1506
Decrypting G:\Test\UUID.txt.enc using private key G:\private.key
Decrypt successful. In.S,P=(1792,1792), Out.S,P=(1715,1715). Bytes read: 1792