I can not find a way to encrypt a file with Lazarus and decrypt it with the .net DESCryptoServiceProvider. I tried with lockbox2, DCPcrypt but I could not. Someone can give me the code to implement this in Lazarus?
Gabriele Cappelletto

Mailing lists / lazarus / 2015-11
12 messages · 6 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
I can not find a way to encrypt a file with Lazarus and decrypt it with the .net DESCryptoServiceProvider. I tried with lockbox2, DCPcrypt but I could not. Someone can give me the code to implement this in Lazarus?
Gabriele Cappelletto
I can not find a way to encrypt a file with Lazarus and decrypt it with
the .net DESCryptoServiceProvider. I tried with lockbox2, DCPcrypt but I could not. Someone can give me the code to implement this in Lazarus?
DES is a standard encryption. If you can't decrypt what's encrypted by a standard encryption, regardless of languages and libraries, then either the library or your way to use the library is wrong. Ensure all properties (key size, block size, initialization vector, whatever) you use is the same and the resulting data is also of the same type and structure. All in all, show what you've done.
I tried to put all the same properties, but I could not. There is probably something I did wrong. So I ask if someone can give me the code. is two days I feel.
Il 14/11/2015 16:36, leledumbo ha scritto:
I can not find a way to encrypt a file with Lazarus and decrypt it with the .net…
Gabriele Cappelletto wrotethe .net DESCryptoServiceProvider.
Please show how the data was encrypted... The exact code snippet would be useful. I could then show you how to use DCPCrypt with the same settings to decrypt it.
Without knowing how it was encrypted, there is no way anybody can show you code on how to decrypt it.
Regards,
- Graeme -
Il 14/11/2015 18:11, Graeme Geldenhuys ha scritto:
Ok thanks. We start from the opposite. cryptographer a string with vb.net with the following code, what's the code to decrypt it with DCPCrypt?
Dim Key() As Byte = {}
Dim IV() As Byte = {1, 2, 3, 4, 5, 6, 7, 8}
Dim KeyStr As String = "cicciob1"
Dim vector As String = "12345678"
Dim inputByteArray(TextBox3.Text.Length) As Byte
Dim des As New DESCryptoServiceProvider
'des.Mode = CipherMode.CBC
'des.Padding = PaddingMode.Zeros
Key = System.Text.Encoding.UTF8.GetBytes(KeyStr)
inputByteArray = Encoding.UTF8.GetBytes(TextBox3.Text)
Dim ms As New MemoryStream
Dim cs As New CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length) cs.FlushFinalBlock()
TextBox4.Text = Convert.ToBase64String(ms.ToArray)
Gabriele Cappelletto wrotewhat's the code to decrypt it with DCPCrypt?
Here is one usage example... This is actually the DES3 self-test code
===========================
procedure test;
const
Key: array[0..23] of byte =
($01,$23,$45,$67,$89,$ab,$cd,$ef,$fe,$dc,$ba,$98,
$76,$54,$32,$10,$89,$ab,$cd,$ef,$01,$23,$45,$67); // whatever
your key is
PlainText: array[0..7] of byte=
($01,$23,$45,$67,$89,$ab,$cd,$e7);
CipherText: array[0..7] of byte=
($de,$0b,$7c,$06,$ae,$5e,$0e,$d5);
var
Cipher: TDCP_3des;
Block: array[0..7] of byte;
begin
dcpFillChar(Block, SizeOf(Block), 0); // initialize block to 0's
Cipher:= TDCP_3des.Create(nil);
Cipher.Init(Key,Sizeof(Key)*8,nil);
Cipher.EncryptECB(PlainText,Block); // encrypt to Block
Result:= CompareMem(@Block, at CipherText,Sizeof(CipherText));
Cipher.DecryptECB(Block,Block); // decrypt the data in Block
Result:= Result and CompareMem(@Block, at PlainText,Sizeof(PlainText));
Cipher.Free;
end;===========================
Here is another example where DCPCrypt is used to decrypt a file. It used a different cipher and hash, but the usage would be the same for DES or DES3.
==========================
procedure TDecryptDCPCrypt.DecryptFile(const pSrcFile, pDestFile: string);
var
SrcFileStream, DestFileStream: TFileStream;
DestPath: string;
lSrc, lDest: string;
begin
lSrc := tiFixPathDelim(pSrcFile);
lDest := tiFixPathDelim(pDestFile);
if FileExists(lSrc) then
begin
SrcFileStream := TFileStream.Create(lSrc, fmOpenRead);
DestFileStream := TFileStream.Create(lDest, fmCreate);
try
DestPath := ExtractFilePath(lDest);
if not DirectoryExists(DestPath) then
CreateDir(DestPath);
try
FCipher.InitStr(cDecryptKey, TDCP_haval);
FCipher.DecryptStream(SrcFileStream, DestFileStream,
LongWord(SrcFileStream.Size));
FCipher.Burn;
except
on E: Exception do
begin
Log(E.Message, lsError);
raise ESlideDecryptionFailed.Create('Failed to decrypt slide <'
+ lSrc + '>' + tiLineEnd + 'Error: ' + E.Message);
end;
end; { try.. except }
finally
SrcFileStream.Free;
DestFileStream.Free;
end; { try..finally }
end { if }
else
Log('pSrcFile does NOT exist!', lsError);
end;
==========================Regards,
- Graeme -
Il 14/11/2015 18:11, Graeme Geldenhuys ha scritto: Ok thanks. We start from the opposite.…
Am 15.11.2015 06:31 schrieb "Gabriele Cappelletto" <
vb.net with the following code, what's the code to decrypt it with DCPCrypt?
Ok thanks. We start from the opposite. cryptographer a string with vb.net with the following code, what's the code to decrypt it with
DCPCrypt?
What did you try in the Pascal side (code please)? Did you decode the Base64 stream before passing the data to the decryption?
Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151115/b8d872d8/attachment.html>
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151115/db53cc3f/attachment.html>
After quick look I noticed that
var
Vector : array of Byte;
SetLength(Vector, 8);
Vector[0] := 1;
Vector[1] := 2;
Vector[2] := 3;
Vector[3] := 4;
Vector[4] := 5;
Vector[5] := 6;
Vector[6] := 7;
Vector[7] := 8;does not match
Dim vector As String = "12345678"
Should be something like
Vector[0] := '1';
Vector[1] := '2';
Vector[2] := '3';
Vector[3] := '4';
Vector[4] := '5';
Vector[5] := '6';
Vector[6] := '7';
Vector[7] := '8';I didn't look at the rest.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151115/5e5724cd/attachment.html>
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151115/1c742fc2/attachment.html>
I see another problem: Your pascal implementation sets key badly. I think it should be 'key'[1]. However as I didn't even use buffer with constant string so I might be wrong.
You should also try to encrypt longer messages to see if first block is intact because then it's padding problem.
Please also note that such implementation is insecure because IV is static, key isn't derived with KDF, DES is insecure nowadays, and no integrity checking is done. So if you want your encryption to be of any security, fix these flaws.
Also consider cleaning up code so we don't get confused about unused variables.
(Btw. My first post on this mailing list - yay!)
W dniu .11.2015 o 12:56 Gabriele Cappelletto
pisze:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151115/0abb34fe/attachment.html>
An HTML attachment was scrubbed... URL: <http://lists.lazarus.freepascal.org/pipermail/laz…
So why is vector in your code? Including irrelevant parts just makes it harder for others to help you.
Rewrite your Pascal code to use the same variables as the VB code!
---- On Sun, 15 Nov 2015 06:56:09 -0500 Gabriele Cappelletto <> wrote ----
But the cipther constructor does not refer to vector, but the variable IV. Vector is not used in this example
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20151115/f7e2ded2/attachment-0001.html>