Cryptage de fichiers avec l'algorithme RSA

Signaler
Messages postés
34
Date d'inscription
lundi 19 mars 2007
Statut
Membre
Dernière intervention
25 août 2007
-
Messages postés
34
Date d'inscription
lundi 19 mars 2007
Statut
Membre
Dernière intervention
25 août 2007
-
je veux faire une petite application qui crypte et decrypte des fichiers avec l'algo RSA
pour l'instant lorsque je crypte il me genere un fichier vide
voici mon code :






using


namespace
System;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>








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 ^ encrypt(String ^fichierClePublic, array ^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 ^ decrypt(String ^fichierPublicPrivee, array ^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"
);










           

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<unsignedchar> ^ encrypted = encrypt("c:\\testpke.txt", br->ReadBytes((int)fs->Length));






           








           








            bw->Write(encrypt("pp", br->ReadBytes((int)fs->Length)));






 







            MessageBox::Show(encrypted->ToString());






      }






     
catch (Exception^)






      {






           








      }






     
finally







      {






            br->Close();






            fs->Close();






           

bw->Close();






            fsw->Close();
       }
return 0;
}

1 réponse

Messages postés
34
Date d'inscription
lundi 19 mars 2007
Statut
Membre
Dernière intervention
25 août 2007
1
voici plutot le code :












 









 





using namespace System;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" /??>









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 ^ encrypt(String ^fichierClePublic, array ^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 ^ decrypt(String ^fichierPublicPrivee, array ^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");









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 ^ encrypted = encrypt("c:\\testpke.txt", br->ReadBytes((int)fs->Length));









 









           












 









           












 









            bw->Write(encrypt("pp", br->ReadBytes((int)fs->Length)));









 









 
 












 









            MessageBox::Show(encrypted->ToString());









 









      }









 









      catch (Exception^)









 









      {









 









           












 









      }









 









      finally









 









      {









 









            br->Close();









 









            fs->Close();









 









            bw->Close();









 









            fsw->Close();










       }









 








 








 








 









    return 0;







}