IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Lazarus Pascal Discussion :

DCPcrypt crypt / decrypt file [Lazarus]


Sujet :

Lazarus Pascal

  1. #1
    Invité
    Invité(e)
    Par défaut DCPcrypt crypt / decrypt file
    Bonjour,

    J'essaie d'utiliser la bibliothèque DCPCrypt.
    Je suis partie d'un code que j'ai trouvé ici.

    J'arrive à crypter/décrypter une chaine de caractères, mais pas à décrypter un fichier, alors que dans son post, il dit que cela fonctionne.

    Voici un extrait du code que j'utilise.
    J'ai enlevé les fonctions qui fonctionnaient bien.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    unit Main;
     
    {$mode objfpc}{$H+}
     
    interface
     
    uses
      Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
      ComCtrls, DCPSha1, DCPmd5, DCPsha512, DCPcrypt2, DCPblockciphers, DCPblowfish,
      dcprijndael
      ;
     
    type
     
      { TMain }
     
      TMain = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Edit3: TEdit;
        OpenDialog1: TOpenDialog;
        PageControl1: TPageControl;
        SaveDialog1: TSaveDialog;
        TabSheet1: TTabSheet;
        TabSheet2: TTabSheet;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
      private
        { private declarations }
        function CryptString(Original: String; Password: String; Password2: String): String;
        function DeCryptString(Original: String; Password: String; Password2: String): String;
     
        procedure CryptFile(Original: String; Encrypted:String; Password: String; Password2: String);
        procedure DeCryptFile(Encrypted: String; Sortie:String; Password: String; Password2: String);
      public
        { public declarations }
      end;
     
    var
      Main1: TMain;
     
    const
      PASSW1: String = 'Un1';
      PASSW2: String = 'Deux2';
     
    implementation
     
    {$R *.lfm}
     
     
    procedure TMain.Button1Click(Sender: TObject);
    // ...
     
    procedure TMain.Button2Click(Sender: TObject);
    // ...
     
    procedure TMain.Button3Click(Sender: TObject);  // crypter un fichier
    begin
      if (OpenDialog1.FileName = '') then OpenDialog1.FileName := ExtractFileDir(Application.ExeName);
      if (OpenDialog1.Execute) then
      begin
        CryptFile(OpenDialog1.FileName, OpenDialog1.FileName + '.cry', PASSW1, PASSW1);
      end;
    end;
     
    procedure TMain.Button4Click(Sender: TObject);  // décrypter un fichier
    begin
      if (OpenDialog1.FileName = '') then OpenDialog1.FileName := ExtractFileDir(Application.ExeName);
      if (OpenDialog1.Execute) then
      begin
        if (SaveDialog1.FileName = '') then SaveDialog1.FileName := ExtractFileDir(Application.ExeName);
        if (SaveDialog1.Execute) then
        begin
            CryptFile(OpenDialog1.FileName, SaveDialog1.FileName, PASSW1, PASSW1);
        end;
      end;
    end;
     
    function TMain.CryptString(Original: String; Password: String; Password2: String): String;
    // ....
     
    function TMain.DeCryptString(Original: String; Password: String; Password2: String): String;
    // ...
     
     
    procedure TMain.CryptFile(Original: String; Encrypted: String; Password: String; Password2: String);
    var
      BlowFish:TDCP_Blowfish;
      AES: TDCP_rijndael;
      OriginalStream:TFileStream;
      EncryptStream:TFileStream;
    begin
      AES := TDCP_rijndael.Create(nil);
      AES.InitStr(Password2, TDCP_Sha1);
     
      BlowFish := TDCP_Blowfish.Create(nil);
      BlowFish.InitStr(Password, TDCP_Sha1);
     
      OriginalStream := TFileStream.Create(Original, fmOpenRead);
      EncryptStream  := TFileStream.Create(Encrypted, fmCreate);
     
      Blowfish.EncryptStream(OriginalStream, EncryptStream, OriginalStream.Size);
      BlowFish.Burn;
     
      AES.EncryptStream(EncryptStream, EncryptStream, EncryptStream.Size);
      AES.Burn;
     
      EncryptStream.Free;
      OriginalStream.Free;
      Blowfish.Free;
      AES.Free;
    end;
     
    procedure TMain.DeCryptFile(Encrypted: String; Sortie: String; Password: String; Password2: String);
    var
      BlowFish:TDCP_Blowfish;
      AES: TDCP_rijndael;
      DecryptStream:TMemoryStream;
      EncryptStream:TFileStream;
    begin
      AES := TDCP_rijndael.Create(nil);
      AES.InitStr(Password2, TDCP_Sha1);
     
      BlowFish := TDCP_Blowfish.Create(nil);
      BlowFish.InitStr(Password, TDCP_Sha1);
     
      EncryptStream := TFileStream.Create(Encrypted, fmOpenRead);
      DecryptStream := TMemoryStream.Create;
     
      AES.DecryptStream(EncryptStream, DecryptStream, EncryptStream.Size);
      AES.Burn;
     
      Blowfish.DecryptStream(DecryptStream, DecryptStream, DecryptStream.Size);
      Blowfish.Burn;
     
      EncryptStream.Free;
      DecryptStream.Position := 0;
      DecryptStream.SaveToFile(Sortie);
      DecryptStream.Free;
      AES.Free;
      BlowFish.Free;
    end;
     
    end.
    Quelqu'un aurait-il une idée de ce qui ne vas dans ce code ?

    Merci.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Dans le code que j'ai trouvé, les actions suivantes été inversée. (dans la fonction pour décrypter le fichier)

    Je les ai changées dans mon code car cela ne fonctionnait pas et que cela ne me semblait pas logique.
    mais cela ne fonctionne pas cette manière non plus.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      Blowfish.DecryptStream(EncryptStream,DecryptStream ,EncryptStream.Size);
      Blowfish.Burn;
      AES.DecryptStream(DecryptStream,DecryptStream,DecryptStream.Size);
      AES.Burn;

  3. #3
    Membre confirmé

    Homme Profil pro
    Autre
    Inscrit en
    Novembre 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Autre

    Informations forums :
    Inscription : Novembre 2015
    Messages : 145
    Points : 625
    Points
    625
    Par défaut
    Si vous utilisez vraiment le code montré pour décrypter votre fichier, il y a un premier souci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    procedure TMain.Button4Click(Sender: TObject);  // décrypter un fichier
    begin
      if (OpenDialog1.FileName = '') then OpenDialog1.FileName := ExtractFileDir(Application.ExeName);
      if (OpenDialog1.Execute) then
      begin
        if (SaveDialog1.FileName = '') then SaveDialog1.FileName := ExtractFileDir(Application.ExeName);
        if (SaveDialog1.Execute) then
        begin
            CryptFile(OpenDialog1.FileName, SaveDialog1.FileName, PASSW1, PASSW1);
        end;
      end;
    end;
    PS. Indice: "CryptFile"


    Concernant votre second post ...

    Non, le décryptage doit se faire dans l'ordre inverse du cryptage. Si vous cryptez en BlowFish puis en AES, il faut ensuite décrypter en AES puis en BlowFish. Ceci étant, je ne vois pas bine l'intérêt d'utiliser deux modes d'encryptage consécutifs (à part en tant qu'exercice).

  4. #4
    Invité
    Invité(e)
    Par défaut

    je vais vérifier, mais cela semble une piste intéressante...

  5. #5
    Membre confirmé

    Homme Profil pro
    Autre
    Inscrit en
    Novembre 2015
    Messages
    145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Autre

    Informations forums :
    Inscription : Novembre 2015
    Messages : 145
    Points : 625
    Points
    625
    Par défaut
    De toute façon, il semble que cela "cafouille" aussi au niveau des stream. En fait, je ne sais pas si l'on peut utiliser le même objet stream en entrée et en sortie d'un appel DCPCrypt (il faut dire que je ne suis pas un grand fan des stream en général).

    Ceci étant, en passant par un stream mémoire temporaire (cf. TempoStream), cela fonctionne correctement, semble-t-il:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    procedure TMain.CryptFile(Original: String; Encrypted: String; Password: String; Password2: String);
    var
      BlowFish:TDCP_Blowfish;
      AES: TDCP_rijndael;
      OriginalStream:TFileStream;
      EncryptStream:TFileStream;
      TempoStream: TMemoryStream;
    begin
      AES := TDCP_rijndael.Create(nil);
      AES.InitStr(Password2, TDCP_Sha1);
     
      BlowFish := TDCP_Blowfish.Create(nil);
      BlowFish.InitStr(Password, TDCP_Sha1);
     
      OriginalStream := TFileStream.Create(Original, fmOpenRead);
      EncryptStream  := TFileStream.Create(Encrypted, fmCreate);
      TempoStream := TMemoryStream.Create;
     
      Blowfish.EncryptStream(OriginalStream, TempoStream, OriginalStream.Size);
      BlowFish.Burn;
     
      TempoStream.Position := 0;
      AES.EncryptStream(TempoStream, EncryptStream, TempoStream.Size);
      AES.Burn;
     
      EncryptStream.Free;
      OriginalStream.Free;
      TempoStream.Free;
      Blowfish.Free;
      AES.Free;
    end;
     
    procedure TMain.DeCryptFile(Encrypted: String; Sortie: String; Password: String; Password2: String);
    var
      BlowFish:TDCP_Blowfish;
      AES: TDCP_rijndael;
      DecryptStream:TMemoryStream;
      EncryptStream:TFileStream;
      TempoStream: TMemoryStream;
    begin
      AES := TDCP_rijndael.Create(nil);
      AES.InitStr(Password2, TDCP_Sha1);
     
      BlowFish := TDCP_Blowfish.Create(nil);
      BlowFish.InitStr(Password, TDCP_Sha1);
     
      EncryptStream := TFileStream.Create(Encrypted, fmOpenRead);
      DecryptStream := TMemoryStream.Create;
      TempoStream := TMemoryStream.Create;
     
      AES.DecryptStream(EncryptStream, TempoStream, EncryptStream.Size);
      AES.Burn;
     
      TempoStream.Position := 0;
      Blowfish.DecryptStream(TempoStream, DecryptStream, TempoStream.Size);
      Blowfish.Burn;
     
      EncryptStream.Free;
      DecryptStream.Position := 0;
      DecryptStream.SaveToFile(Sortie);
      DecryptStream.Free;
      TempoStream.Free;
      AES.Free;
      BlowFish.Free;
    end;

  6. #6
    Invité
    Invité(e)
    Par défaut
    Cela fonctionne maintenant correctement, je te remercie de tes réponses.

    Je n'ai pas encore testé les modifications de ton dernier message, mais cela fonctionne déjà bien.
    Je vais regarder cela rapidement.

  7. #7
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par FChrisF Voir le message
    Ceci étant, je ne vois pas bine l'intérêt d'utiliser deux modes d'encryptage consécutifs (à part en tant qu'exercice).
    Je pense que cela permet d'avoir un meilleur cryptage en combinant plusieurs méthode.

  8. #8
    Expert confirmé

    Inscrit en
    Août 2006
    Messages
    3 941
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 3 941
    Points : 5 652
    Points
    5 652
    Par défaut
    Bonjour,
    Citation Envoyé par benoit1024 Voir le message
    Je pense que cela permet d'avoir un meilleur cryptage en combinant plusieurs méthode.
    C'est en fait plutôt déconseillé.

    Un bon cryptage avec des clés bien choisies suffit
    Si les cons volaient, il ferait nuit à midi.

  9. #9
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par droggo Voir le message
    C'est en fait plutôt déconseillé.
    Ah bon, est-ce que vous savez pourquoi c'est déconseillé ?
    Je pensais que de cette manière on brouillait les pistes pour connaitre le cryptage utilisé. Avec de bonnes clés en plus évidemment.

  10. #10
    Invité
    Invité(e)
    Par défaut
    Citation Envoyé par FChrisF Voir le message
    De toute façon, il semble que cela "cafouille" aussi au niveau des stream. En fait, je ne sais pas si l'on peut utiliser le même objet stream en entrée et en sortie d'un appel DCPCrypt (il faut dire que je ne suis pas un grand fan des stream en général).

    Ceci étant, en passant par un stream mémoire temporaire (cf. TempoStream), cela fonctionne correctement, semble-t-il
    Je n'avais pas noté de problème en utilisant un seul stream, et je connais mal ce type d'objets, par contre, ta méthode semble effectivement plus "propre".

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [WM19] Crypte et Decrypte sous WM Android et WD PC
    Par courdi95 dans le forum Windev Mobile
    Réponses: 1
    Dernier message: 09/12/2014, 00h29
  2. Crypté et decrypté chaine de caractere
    Par bforzeus dans le forum Langage
    Réponses: 42
    Dernier message: 20/08/2012, 14h07
  3. Crypt / decrypt symetrique
    Par kranagard dans le forum Langage
    Réponses: 1
    Dernier message: 02/12/2010, 13h10
  4. A propos des 'File management Functions' de Windows
    Par znaidi dans le forum Windows
    Réponses: 3
    Dernier message: 01/04/2003, 17h01
  5. recupèrer file d'attente d'impression
    Par magic corp. dans le forum Langage
    Réponses: 2
    Dernier message: 25/09/2002, 15h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo