Recent

Author Topic: DES  (Read 6331 times)

fiazhnd

  • New Member
  • *
  • Posts: 36
DES
« on: June 09, 2017, 02:32:18 am »
Hi, guys, i want to decrypt DES in Pascal is there any example to learn, decrypting the ciphertext with the key?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: DES
« Reply #1 on: June 09, 2017, 07:02:13 am »
there are 2 3rd party libraries that can help you with cryptography.
1) dcpcrypt http://wiki.lazarus.freepascal.org/DCPcrypt
2) Lockbox https://sourceforge.net/projects/tplockbox/?source=directory

both support des as far as I remember.
Take your pick.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: DES
« Reply #2 on: June 09, 2017, 07:43:36 am »
Yup, both of these do the job.
But note DES is not considered a good or safe algorithm to use. I have seen Delphi code that can break DES: so that may be an alternative decrypt method... 8-) (Google for it...) Also, I wrote koldes.pas but that relies slightly on the KOL unit and is not considered for beginners.

« Last Edit: June 09, 2017, 10:22:30 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: DES
« Reply #3 on: June 13, 2017, 10:13:01 pm »
This code work for me perfectly  :D
However, i need a little help this key and Target is already define in script what if i want  to get "Key" from Edit1 and "Target" from Edit2

Mannually Enter Value in Edit1 and Edit2
Key: AA78BFB9A479B496
Target: 85104868F071C1F2
 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   Key: array [1..8] of Byte = (
  4.     $12,$34,$56,$78,$90,$AB,$CD,$EF,
  5.   );
  6.   Target: array [1..8] of Byte = (
  7.     $12,$34,$56,$78,$90,$AB,$CD,$EF
  8.   );
  9. var
  10.   Result: array [1..8] of Byte;
  11.   S: String;
  12.   B: Byte;
  13. begin
  14.   FillByte(Result[1],SizeOf(Result),0);
  15.   DCP_3des1.Init(Key,Length(Key) * 8,nil);
  16.   DCP_3des1.EncryptECB(Target,Result);
  17.   S := '';
  18.   for B in Result do
  19.     S := S + HexStr(B,2);
  20.   Edit1.Caption := S;
  21.   DCP_3des1.Reset;
  22. end;

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: DES
« Reply #4 on: June 14, 2017, 10:06:08 am »
I try But no luck yet any suggestion will be highly regarded
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.     Result: array [1..8] of Byte;
  4.     Key,Target: Int64;
  5.     S: String;
  6.     B: Byte;
  7.     Key1,Target1: String;
  8.   begin
  9.     Key := StrToInt64('$'+Edit1.Text);
  10.     Key1:=  IntToHex(Key,8);
  11.     Target:= StrToInt64('$'+Edit2.Text);
  12.     Target1:=IntToHex(Target,8);
  13.     FillByte(Result[1],SizeOf(Result),0);
  14.     DCP_des1.Init(Key1,Length(Key1) * 8,nil);
  15.     DCP_des1.DecryptECB(Target1,Result);
  16.     S := '';
  17.     for B in Result do
  18.       S := S + HexStr(B,2);
  19.     Edit3.Text := S;
  20.     DCP_des1.Reset;
  21.   end;


But Face following error
Invalid Key Size;
In File 'dcpcrypt.pas' at line 406;             

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: DES
« Reply #5 on: June 14, 2017, 11:07:25 am »
Your first code uses 3DES, not DES... Are you sure you want triple DES (3DES) ? because that is completely different from (the second code) DES?
« Last Edit: June 14, 2017, 11:13:51 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: DES
« Reply #6 on: June 14, 2017, 12:10:17 pm »
My original code is in reply #4, before was only to indicate that found a useful example.
I use DCP_des1 it converts to DES and provides an accurate result.

Now my issue is a Key size which acquires from Tedit.

For Sake of clarity, the below code gives correct DES result 10119803B3FFA5D0. But want to use "Key" and "Target" from TEdit.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   Key: array [1..8] of Byte = (
  4.     $83,$10,$48,$68,$D0,$71,$C1,$B2
  5.   );
  6.   Target: array [1..8] of Byte = (
  7.    $7C,68,$F7,$D1,$74,$08,$75,$0A  
  8.   );
  9. var
  10.   Result: array [1..8] of Byte;
  11.   S: String;
  12.   B: Byte;
  13. begin
  14.   FillByte(Result[1],SizeOf(Result),0);
  15.   DCP_des1.Init(Key,Length(Key) * 8,nil);
  16.   DCP_des1.DecryptECB(Target,Result);
  17.   S := '';
  18.   for B in Result do
  19.     S := S + HexStr(B,2);
  20.   Edit1.Caption := S;
  21.   DCP_3des1.Reset;
  22. end;
  23.  

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: DES
« Reply #7 on: June 17, 2017, 08:46:19 pm »
Code: Pascal  [Select][+][-]
  1. Key: array [1..8] of Byte;

How Can i convert this string into Byte, Any Help guys........ :-[
   
Code: Pascal  [Select][+][-]
  1. Key[1]:= (StrToInt('$'+Edit1.Text[1]+Edit1.Text[2]);
  2.     showmessage(IntToStr(Key[1]));
  3.     Key[2]:= StrToInt('$'+Edit1.Text[3]+Edit1.Text[4]);
  4.     Key[3]:= StrToInt('$'+Edit1.Text[5]+Edit1.Text[6]);
  5.     Key[4]:= StrToInt('$'+Edit1.Text[7]+Edit1.Text[8]);
  6.     Key[5]:= StrToInt('$'+Edit1.Text[9]+Edit1.Text[10]);
  7.     Key[6]:= StrToInt('$'+Edit1.Text[11]+Edit1.Text[12]);
  8.     Key[7]:= StrToInt('$'+Edit1.Text[13]+Edit1.Text[14]);
  9.     Key[8]:= StrToInt('$'+Edit1.Text[15]+Edit1.Text[16]);
  10.  

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: DES
« Reply #8 on: June 17, 2017, 09:10:44 pm »
why are you using strtoint? most of the times a simple type cast is what is needed. what are the context of edit1.text?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: DES
« Reply #9 on: June 17, 2017, 09:16:35 pm »
thank you for your reply Mr. taazz;

In Edit1.Text i have value obtain from hex file 83104868D071C1B2.

Required following value like this;
Code: Pascal  [Select][+][-]
  1.     Key[1]:= $83;
  2.     Key[2]:= $10;
  3.     Key[3]:= $48;
  4.     Key[4]:= $68;
  5.     Key[5]:= $D0;
  6.     Key[6]:= $71;
  7.     Key[7]:= $C1;
  8.     Key[8]:= $B2;
  9.  

i was try to convert to byte thats using strtoints

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: DES
« Reply #10 on: June 17, 2017, 09:55:18 pm »
thank you for your reply Mr. taazz;

In Edit1.Text i have value obtain from hex file 83104868D071C1B2.

Required following value like this;
Code: Pascal  [Select][+][-]
  1.     Key[1]:= $83;
  2.     Key[2]:= $10;
  3.     Key[3]:= $48;
  4.     Key[4]:= $68;
  5.     Key[5]:= $D0;
  6.     Key[6]:= $71;
  7.     Key[7]:= $C1;
  8.     Key[8]:= $B2;
  9.  

i was try to convert to byte thats using strtoints
that looks ok what is the result of the conversion? what the showmessage show?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

fiazhnd

  • New Member
  • *
  • Posts: 36
Re: DES
« Reply #11 on: June 17, 2017, 10:02:34 pm »
I think i am not able to explain well let me try again sir,

I have value 83104868D071C1B2 in Edit1 which i acquired from Hex file, what i want sir to convert value from Edit1 in the following shape;

Code: Pascal  [Select][+][-]
  1.     Key[1]:= $83;
  2.     Key[2]:= $10;
  3.     Key[3]:= $48;
  4.     Key[4]:= $68;
  5.     Key[5]:= $D0;
  6.     Key[6]:= $71;
  7.     Key[7]:= $C1;
  8.     Key[8]:= $B2;
  9.  

So that's why i was trying to convert into byte  as above;

Code: Pascal  [Select][+][-]
  1. Key[1]:= IntToStr('$'+Edit1.Text[1]+Edit1.Text[2]);


Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: DES
« Reply #12 on: June 17, 2017, 10:57:29 pm »
 DCP_3des1.Reset;?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: DES
« Reply #13 on: June 17, 2017, 11:08:25 pm »
I think i am not able to explain well let me try again sir,

I have value 83104868D071C1B2 in Edit1 which i acquired from Hex file, what i want sir to convert value from Edit1 in the following shape;

Code: Pascal  [Select][+][-]
  1.     Key[1]:= $83;
  2.     Key[2]:= $10;
  3.     Key[3]:= $48;
  4.     Key[4]:= $68;
  5.     Key[5]:= $D0;
  6.     Key[6]:= $71;
  7.     Key[7]:= $C1;
  8.     Key[8]:= $B2;
  9.  

So that's why i was trying to convert into byte  as above;

Code: Pascal  [Select][+][-]
  1. Key[1]:= IntToStr('$'+Edit1.Text[1]+Edit1.Text[2]);
start lazarus and selecct the menu project\NEw Project on the dialog select simple program then copy paste the code below execute it and paste the results here.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses sysutils;
  4.  
  5. const
  6.   cKey : array [1..8] of Byte = ($83,$10,$48,$68,$D0,$71,$C1,$B2);
  7.   cStr : string = '83104868D071C1B2';
  8. var
  9.   sKey : array[1..8] of Byte;// absolute cKey;
  10.   vCntr :Integer;
  11. begin
  12.   sKey[1] := StrToInt('$' + cStr[01] + cStr[02]);
  13.   sKey[2] := StrToInt('$' + cStr[03] + cStr[04]);
  14.   sKey[3] := StrToInt('$' + cStr[05] + cStr[06]);
  15.   sKey[4] := StrToInt('$' + cStr[07] + cStr[08]);
  16.   sKey[5] := StrToInt('$' + cStr[09] + cStr[10]);
  17.   sKey[6] := StrToInt('$' + cStr[11] + cStr[12]);
  18.   sKey[7] := StrToInt('$' + cStr[13] + cStr[14]);
  19.   sKey[8] := StrToInt('$' + cStr[15] + cStr[16]);
  20.   writeln('cstr : ', cStr[1], cStr[2], ',', cStr[3], cStr[4], ',', cStr[5], cStr[6], ',',cStr[7], cStr[8], ',',
  21.                      cStr[9], cStr[10],',', cStr[11],cStr[12],',', cStr[13],cStr[14],',',cStr[15],cStr[16] );
  22.   WriteLn('sKey : ', IntToHex(sKey[1], 2),',', IntToHex(sKey[2], 2),',', IntToHex(sKey[3], 2),',', IntToHex(sKey[4],2), ',',
  23.                      IntToHex(sKey[5], 2),',', IntToHex(sKey[6], 2),',', IntToHex(sKey[7], 2),',', IntToHex(sKey[8],2) );
  24.   ReadLn;
  25. end.
  26.  
You should get something along the lines of
Code: Text  [Select][+][-]
  1. cstr : 83,10,48,68,D0,71,C1,B2
  2. sKey : 83,10,48,68,D0,71,C1,B2
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: DES
« Reply #14 on: June 17, 2017, 11:39:34 pm »
If you want direct conversion from the Text of a TEdit to an array of byte you could use this function

Code: Pascal  [Select][+][-]
  1. type
  2.   TOctByteArray = array[1..8] of Byte;
  3.   Ts2 = string[2];
  4.  
  5. function ConvertEditTextToOctByte(anEdit: TEdit): TOctByteArray;
  6.  
  7.   function IsValidHexStr: Boolean;
  8.   var
  9.     c: Char;
  10.   begin
  11.     for c in anEdit.Text do
  12.       if not (c in ['0'..'9','A'..'F','a'..'f']) then
  13.         Exit(False);
  14.     Result:=True;
  15.   end;
  16.  
  17.   function StrToHex(s: Ts2): Byte;
  18.  
  19.     function GetValue(c: Char): Byte;
  20.     begin
  21.       if c in ['0'..'9'] then
  22.         Exit(Ord(c) - 48)
  23.       else if c in ['A'..'F'] then
  24.         Exit(Ord(c) - 55)
  25.       else if c in ['a'..'f'] then
  26.         Exit(Ord(c) - 87)
  27.       else Assert(True,'Error in hex string value');
  28.     end;
  29.   begin
  30.     Exit(GetValue(s[2]) + 16*GetValue(s[1]));
  31.   end;
  32.  
  33. var
  34.   i: Integer;
  35.   s: Ts2;
  36. begin
  37.   if not (Assigned(anEdit) and (Length(anEdit.Text)=16) and IsValidHexStr) then
  38.     ShowMessage('Invalid text to convert')
  39.   else
  40.     for i:=Low(TOctByteArray) to High(TOctByteArray) do begin
  41.       s:=Copy(anEdit.Text, Pred(i*2), 2);
  42.       Result[i]:=StrToHex(s);
  43.     end;
  44. end;

 

TinyPortal © 2005-2018