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