Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1241 connectés 

  FORUM HardWare.fr
  Programmation
  C

  Crypter en DES (ou DES3) avec la Crypto Api

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Crypter en DES (ou DES3) avec la Crypto Api

n°1134918
rclsilver
Posté le 29-06-2005 à 16:27:43  profilanswer
 

Alors voila ça fait quelques temps que j'essaie de trouver sur MSDN et sur le site comment le faire.. or je n'arrive a rien.. c'est tres peu documenté :s Donc je voudrais savoir si quelqu'un aurait un exemple fonctionnel en stock :s Merci

mood
Publicité
Posté le 29-06-2005 à 16:27:43  profilanswer
 

n°1135939
Juntao2k2
Posté le 30-06-2005 à 14:33:43  profilanswer
 

Sinon tu peux regarder du côté de Crypto++ (http://www.eskimo.com/~weidai/cryptlib.html), ca pourra peut être t'aider. Sinon il y a aussi Cryptlib (http://www.cs.auckland.ac.nz/~pgut001/cryptlib/ qui peut être intéressant.


Message édité par Juntao2k2 le 30-06-2005 à 14:34:08
n°1136680
rclsilver
Posté le 01-07-2005 à 01:13:25  profilanswer
 

hum, je te remercie pour ces liens, mais j'aurais preferé rester au maximum avec du C et de l'API windows :) je lis le 2e lien quand meme, au cas où :)

n°1136752
_darkalt3_
Proctopathe
Posté le 01-07-2005 à 08:42:05  profilanswer
 

Code le là la main :D
(c'est faisable y'avais pas de lib pour ma plateforme :o )


---------------
Töp of the plöp
n°1136778
rclsilver
Posté le 01-07-2005 à 09:02:37  profilanswer
 

oui, mais je trouve stupide de coder un truc a la main alors que c'est geré par API :o

n°1137549
_darkalt3_
Proctopathe
Posté le 01-07-2005 à 17:13:29  profilanswer
 

Citation :

(c'est faisable y'avais pas de lib pour ma plateforme :o )


 
quand y'a pas de lib dispo pour ta plate forme, t'as pas le choix :o


---------------
Töp of the plöp
n°1137569
rclsilver
Posté le 01-07-2005 à 17:25:09  profilanswer
 

j'ai pas dit le contraire, mais la, y en a une, donc autant l'utiliser :)
 
donc j'ai réussi a pondre un code a partir d'une réponse sur un forum, le code fonctionne pas :s je vous le mets si jamais quelqu'un connait et saurait résoudre le pb :)

Code :
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <wincrypt.h>
  4. #define  BUFFER_SIZE 1024
  5. void error(const char *mess)
  6. {
  7.    fprintf(stderr, "%s\n",mess);
  8.    system("pause" );
  9.    ExitProcess(EXIT_FAILURE);
  10. }
  11. int DESEncrypt(const char * texte, const char * cle, char * buffer);
  12. int DESDecrypt(const char * texte, const char * cle, char * buffer);
  13. int main()
  14. {
  15.    char buffer[BUFFER_SIZE+1] = {0};
  16.    char texte[BUFFER_SIZE+1] = "test !";
  17.    char * cle   = "25d55ad283aa400af464c76d713c07ad";
  18.    DESEncrypt(texte, cle, (char*)&buffer);
  19.    printf("Texte crypte : %s\n", buffer);
  20.    strcpy(texte, buffer);
  21.    memset(buffer, 0, BUFFER_SIZE);
  22.    DESDecrypt(texte, cle, (char*)&buffer);
  23.    printf("Texte decrypte : %s\n", buffer); 
  24.    system("PAUSE" );
  25.    return 0;
  26. }
  27. int DESEncrypt(const char * texte, const char * cle, char * buffer)
  28. {
  29.    HCRYPTPROV   hProv = 0;
  30.    HCRYPTHASH   hHash = 0;
  31.    HCRYPTKEY   hKey = 0;
  32.    DWORD      Len_txt = (DWORD)strlen(texte);
  33.    DWORD      Len_cle = (DWORD)strlen(cle);
  34.    DWORD      ret;
  35.    if ( !CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, 0) )
  36.    {
  37.       ret = GetLastError();
  38.       if (ret !=  NTE_BAD_KEYSET)
  39.       {
  40.          error("1" );
  41.       }
  42.       else 
  43.       {
  44.          if ( !CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET) )
  45.          {
  46.             error("2" );
  47.          }
  48.       }
  49.    }
  50.    if ( !CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash) )
  51.    { 
  52.       if (hProv)   CryptReleaseContext(hProv, 0);
  53.       error("3" );
  54.    }
  55.    if ( !CryptHashData (hHash, (PBYTE)cle,  Len_cle, 0) )
  56.    {
  57.       if (hHash)   CryptDestroyHash(hHash); 
  58.       if (hProv)   CryptReleaseContext(hProv, 0);
  59.       error("4" );
  60.    }
  61.    if ( !CryptDeriveKey (hProv, CALG_DES, hHash, 0, &hKey) )
  62.    {
  63.       if (hHash)   CryptDestroyHash(hHash); 
  64.       if (hProv)   CryptReleaseContext(hProv, 0);
  65.       error("5" );
  66.    }
  67.    if ( !CryptEncrypt (hKey, 0, 1, 0, buffer, &Len_txt, BUFFER_SIZE) )
  68.    {
  69.       if (hKey)   CryptDestroyKey(hKey);
  70.       if (hHash)   CryptDestroyHash(hHash); 
  71.       if (hProv)   CryptReleaseContext(hProv, 0);
  72.       error("6" );
  73.    }
  74.    if (hKey)   CryptDestroyKey(hKey);
  75.    if (hHash)   CryptDestroyHash(hHash); 
  76.    if (hProv)   CryptReleaseContext(hProv, 0);
  77.    return Len_txt;
  78. }
  79. int DESDecrypt(const char * texte, const char * cle, char * buffer)
  80. {
  81.    HCRYPTPROV   hProv = 0;
  82.    HCRYPTHASH   hHash = 0;
  83.    HCRYPTKEY   hKey = 0;
  84.    DWORD      Len_txt = (DWORD)strlen(texte);
  85.    DWORD      Len_cle = (DWORD)strlen(cle);
  86.    DWORD      ret;
  87.    if ( !CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, 0) )
  88.    {
  89.       ret = GetLastError();
  90.       if (ret !=  NTE_BAD_KEYSET)
  91.       {
  92.          error("1" );
  93.       }
  94.       else 
  95.       {
  96.          if ( !CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET) )
  97.          {
  98.             error("2" );
  99.          }
  100.       }
  101.    }
  102.    if ( !CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash) )
  103.    { 
  104.       if (hProv)   CryptReleaseContext(hProv, 0);
  105.       error("3" );
  106.    }
  107.    if ( !CryptHashData (hHash, (PBYTE)cle,  Len_cle, 0) )
  108.    {
  109.       if (hHash)   CryptDestroyHash(hHash); 
  110.       if (hProv)   CryptReleaseContext(hProv, 0);
  111.       error("4" );
  112.    }
  113.    if ( !CryptDeriveKey (hProv, CALG_DES, hHash, 0, &hKey) )
  114.    {
  115.       if (hHash)   CryptDestroyHash(hHash); 
  116.       if (hProv)   CryptReleaseContext(hProv, 0);
  117.       error("5" );
  118.    }
  119.    if ( !CryptDecrypt(hKey, 0, 0, 0, buffer, &Len_txt) ) 
  120.    {
  121.       if (hKey)   CryptDestroyKey(hKey);
  122.       if (hHash)   CryptDestroyHash(hHash); 
  123.       if (hProv)   CryptReleaseContext(hProv, 0);
  124.       error("6" );
  125.    }
  126.    if (hKey)   CryptDestroyKey(hKey);
  127.    if (hHash)   CryptDestroyHash(hHash); 
  128.    if (hProv)   CryptReleaseContext(hProv, 0);
  129.    return Len_txt;
  130. }


 
Merci d'avance
 
ps voila le résultat :

Code :
  1. Texte crypte : K■ÕR═Jþù
  2. Texte decrypte :
  3. )&►vº¦Y
  4. Appuyez sur une touche pour continuer...

(voila pourquoi je dis que le code fonctionne pas :s)

n°1137602
Taz
bisounours-codeur
Posté le 01-07-2005 à 17:40:21  profilanswer
 

openssl

n°1137675
rclsilver
Posté le 01-07-2005 à 20:20:11  profilanswer
 

des API Windows sont disponibles, je vois pas l'utilité de joindre des librairies supplémentaires :s surtout que la réponse "openssl" n'est pas tres constructive..

n°1137683
Taz
bisounours-codeur
Posté le 01-07-2005 à 20:31:10  profilanswer
 

t'es trop idiot pour taper "openssl" dans google, j'y peux rien.
 
http://www.openssl.org/docs/crypto/des.html#

mood
Publicité
Posté le 01-07-2005 à 20:31:10  profilanswer
 

n°1137809
rclsilver
Posté le 01-07-2005 à 22:41:40  profilanswer
 

Non, c'est juste que j'aurais aimé utiliser l'API windows... et restes calme stp... sur ce, merci quand meme...


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  Crypter en DES (ou DES3) avec la Crypto Api

 

Sujets relatifs
Utilisation des API C de MySQLAPI / Classes ASN.1 Java ???
Sampler de PC Speaker et APIAPI Reflection
[Dev-Cpp] erreur de link avec API GetStockObject[Api windows (win32) / C] Edition d'un subitem dans une listview
Crypter code sourceintegrer une API (librairie) a java
crypto 3DES ou AES: pb d'implementationTutorials sur le parsing de l'API DOM de PHP5 ?
Plus de sujets relatifs à : Crypter en DES (ou DES3) avec la Crypto Api


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR