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

VC++ .NET Discussion :

cryptage avec RSA sous visual C++


Sujet :

VC++ .NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    326
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 326
    Par défaut cryptage avec RSA sous visual C++
    Citation Envoyé par nico-pyright(c)
    c'est pas si compliqué que ca ...
    le seul truc un peu délicat, c'est la génération des clés publiques et privées, si tu as besoin de les sauver
    voila pour l'instant j'ai reussi à générer deux fichiers le premier pour la clé privé et l'autre pour la clé privé
    mais reste les fonctions qui crypte et decrypte, voici mon code pour la fonction qui crypte
    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
     
    array<Byte>^ RSAEncrypt( array<Byte>^DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding )
    {
          RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
     
     
          RSA->ImportParameters( RSAKeyInfo );
     
     
          return RSA->Encrypt( DataToEncrypt, DoOAEPPadding );
    }
     
    int main(array<System::String ^> ^args)
    {
    FileStream ^fs = gcnew FileStream("c:\\test.txt", FileMode::Open);
    			 BinaryReader ^br = gcnew BinaryReader(fs);
    			 FileStream ^fsw = gcnew FileStream("c:\\test1.txt",FileMode::CreateNew);
    			 BinaryWriter ^bw = gcnew BinaryWriter(fsw);
     
     
    			 try
       {
     
     
    	  array<Byte>^dataToEncrypt = br->ReadBytes((int)fs->Length 
          array<Byte>^encryptedData;
          array<Byte>^decryptedData;
     
     
          RSACryptoServiceProvider^ RSA = gcnew RSACryptoServiceProvider;
     
          encryptedData = RSAEncrypt( dataToEncrypt, RSA->ExportParameters( false ), false );
    	  bw->Write(encryptedData);
    			 }
    			    catch ( Exception^ ) 
    				 {
    				 }
    				finally
    				{
    					br->Close();
    					fs->Close();
    					bw->Close();
    					fsw->Close();
    				}
     
    		 }
        return 0;
    }
    il me genere un fichier vide
    et je ne sais pas comment faire pour utiliser la clé génerée(sauvegardée)?
    est ce que tu peux voir ce code et me dire ce qui ne va pas ?
    merci

  2. #2
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    bon ...

    regarde cet exemple simpliste, mais cherche un peu !
    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
    using namespace System;
    using namespace System::IO;
    using namespace System::Security::Cryptography;
     
    void genererCle(String ^fichierPublicPrivee, String ^fichierClePublic)
    {
    	if (!(File::Exists(fichierPublicPrivee) || File::Exists(fichierClePublic)) )
    	{
    		RSACryptoServiceProvider ^rsaProvider = gcnew RSACryptoServiceProvider(1024);
    		String ^clesPublicEtPrivee = rsaProvider->ToXmlString(true);
    		String ^clePublic = rsaProvider->ToXmlString(false);
    		StreamWriter ^sw = gcnew StreamWriter(fichierPublicPrivee);
    		sw->Write(clesPublicEtPrivee);
    		sw->Close();
    		sw = gcnew StreamWriter(fichierClePublic);
    		sw->Write(clePublic);
    		sw->Close();
    	}
    	else
    		Console::WriteLine("fichier de clés existant");
    }
     
    array<unsigned char> ^ encrypt(String ^fichierClePublic, array<unsigned char> ^data)
    {
    	StreamReader ^sr = gcnew StreamReader(fichierClePublic);
    	String ^cle = sr->ReadLine();
    	sr->Close();
    	RSACryptoServiceProvider ^rsaProvider = gcnew RSACryptoServiceProvider(1024);
    	rsaProvider->FromXmlString(cle);
    	return rsaProvider->Encrypt(data, false);
    }
     
    array<unsigned char> ^ decrypt(String ^fichierPublicPrivee, array<unsigned char> ^data)
    {
    	StreamReader ^sr = gcnew StreamReader(fichierPublicPrivee);
    	String ^cle = sr->ReadLine();
    	sr->Close();
    	RSACryptoServiceProvider ^rsaProvider = gcnew RSACryptoServiceProvider(1024);
    	rsaProvider->FromXmlString(cle);
    	return rsaProvider->Decrypt(data, false);
    }
     
    int main(array<System::String ^> ^args)
    {
        genererCle("pp", "p");
    	String ^ chaine = "chaine à encoder";
    	array<unsigned char> ^ encrypted = encrypt("p", Text::Encoding::Unicode->GetBytes(chaine));
    	array<unsigned char> ^ decrypted = decrypt("pp", encrypted);
    	String ^chaineDecodee = Text::Encoding::Unicode->GetString(decrypted);
    	Console::WriteLine(chaineDecodee);
        return 0;
    }

  3. #3
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    326
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 326
    Par défaut
    ok..merci
    le "p" et le "pp" correspondent aux 2 fichiers de clé publique et privé c'est ca ?
    donc je peux mettre à leur place les fichiers que j'ai reussi à génerer ?

  4. #4
    Rédacteur
    Avatar de nico-pyright(c)
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    6 414
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 6 414
    Par défaut
    oui

  5. #5
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    326
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 326
    Par défaut System.Security.Cryptography.CryptographicException
    ce que je veux c'est crypter et decrypter des fichiers ..donc j'ai changé dans le main de ton code , de facon à ce que je puisse crypter le fichier "c:\\test.txt" en "c:\\test1.txt" et le decrypter après en "c:\\test2.txt"
    mais il me genere une exception
    "System.Security.Cryptography.CryptographicException"

    voici le code :



    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
     
    int main(array<System::String ^> ^args)
    {
     
     
    FileStream ^fs = gcnew FileStream("c:\\test.txt", FileMode::Open);
    BinaryReader ^br = gcnew BinaryReader(fs);
    FileStream ^fsw = gcnew FileStream("c:\\test1.txt",FileMode::CreateNew);
    BinaryWriter ^bw = gcnew BinaryWriter(fsw);
    genererCle("pp", "p");
     
    	array<unsigned char> ^ encrypted = 
    encrypt("p", br->ReadBytes((int)fs->Length));
    	bw->Write(encrypted);
     
    			   br->Close();
    		         fs->Close();
    		         bw->Close();
    		         fsw->Close();
     
    FileStream ^fs = gcnew FileStream("c:\\test1.txt", FileMode::Open);
    BinaryReader ^br = gcnew BinaryReader(fs);
    FileStream ^fsw = gcnew FileStream("c:\\test2.txt",FileMode::CreateNew);
    BinaryWriter ^bw = gcnew BinaryWriter(fsw);
     
     
     
     
     
     
    	array<unsigned char> ^ decrypted = decrypt("pp", encrypted);
    	bw->Write(decrypted);
    			   br->Close();
    		         fs->Close();
    		         bw->Close();
    		         fsw->Close();
     
    return0;
    }

  6. #6
    Membre éclairé
    Inscrit en
    Avril 2007
    Messages
    326
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 326
    Par défaut
    ce que je veux c'est crypter et decrypter des fichiers ..donc j'ai changé dans le main de ton code , de facon à ce que je puisse crypter le fichier "c:\\test.txt" en "c:\\test1.txt" et le decrypter après en "c:\\test2.txt"
    mtnt je n'ai plus cette exception .. mais il me genere un fichier ("c:\\test1.txt" ) vide
    voici le code :
    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
     
     
    int main(array<System::String ^> ^args)
    {
     
     genererCle("pp", "p");
    	FileStream ^fs = gcnew FileStream("c:\\test.txt", FileMode::Open);
    	BinaryReader ^br = gcnew BinaryReader(fs);
    	FileStream ^fsw = gcnew FileStream("c:\\test1.txt", FileMode::CreateNew);
    	BinaryWriter ^bw = gcnew BinaryWriter(fsw);
    	try
    	{
    		array<unsigned char> ^ encrypted = encrypt("p", br->ReadBytes((int)fs->Length));
    		bw->Write(encrypted);
    	}
    	catch (Exception^)
    	{
    	}
    	finally
    	{
    		br->Close();
    		fs->Close();
    		bw->Close();
    		fsw->Close();
    	}
    return 0;
    }

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

Discussions similaires

  1. Creer un etat avec hierarchie sous Visual basic 6
    Par FRED-SUCCES dans le forum VB 6 et antérieur
    Réponses: 1
    Dernier message: 31/10/2009, 09h16
  2. Réponses: 2
    Dernier message: 02/11/2007, 17h44
  3. Réponses: 10
    Dernier message: 19/03/2007, 15h37
  4. Problème avec pminub sous Visual C++ 6
    Par Flo. dans le forum x86 32-bits / 64-bits
    Réponses: 5
    Dernier message: 06/10/2006, 10h14
  5. [debutant]opengl avec sdl sous visual c++
    Par bourinator dans le forum OpenGL
    Réponses: 1
    Dernier message: 13/06/2005, 11h24

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