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
|
using namespace System;
using namespace System::Text;
using namespace System::IO;
using namespace System::Collections::Generic;
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");
}
String ^ encryptString(String ^fichierClePublic, String ^data)
{
StreamReader ^sr = gcnew StreamReader(fichierClePublic);
String ^cle = sr->ReadLine();
sr->Close();
RSACryptoServiceProvider ^rsaProvider = gcnew RSACryptoServiceProvider(1024);
rsaProvider->FromXmlString(cle);
int keySize = 1024 / 8;
array<unsigned char> ^bytes = Encoding::UTF32->GetBytes( data );
int maxLength = keySize - 42;
int dataLength = bytes->Length;
int iterations = dataLength / maxLength;
StringBuilder ^stringBuilder = gcnew StringBuilder();
for( int i = 0; i <= iterations; i++ )
{
array<unsigned char> ^ tempBytes = gcnew array<unsigned char> (( dataLength - maxLength * i > maxLength ) ? maxLength : dataLength - maxLength * i);
Buffer::BlockCopy( bytes, maxLength * i, tempBytes, 0, tempBytes->Length );
array<unsigned char> ^ encryptedBytes = rsaProvider->Encrypt( tempBytes, true );
Array::Reverse( encryptedBytes );
stringBuilder->Append( Convert::ToBase64String( encryptedBytes ) );
}
return stringBuilder->ToString();
}
String ^DecryptString( String ^fichierClePublic, String ^inputString)
{
StreamReader ^sr = gcnew StreamReader(fichierClePublic);
String ^cle = sr->ReadLine();
sr->Close();
RSACryptoServiceProvider ^rsaProvider = gcnew RSACryptoServiceProvider(1024);
rsaProvider->FromXmlString(cle);
int base64BlockSize = ( ( 1024 / 8 ) % 3 != 0 ) ? ( ( ( 1024 / 8 ) / 3 ) * 4 ) + 4 : ( ( 1024 / 8 ) / 3 ) * 4;
int iterations = inputString->Length / base64BlockSize;
List<unsigned char> ^list = gcnew List<unsigned char>();
for( int i = 0; i < iterations; i++ )
{
array<unsigned char> ^ encryptedBytes = Convert::FromBase64String( inputString->Substring( base64BlockSize * i, base64BlockSize ) );
Array::Reverse( encryptedBytes );
array<unsigned char> ^arr = rsaProvider->Decrypt( encryptedBytes, true );
for each(unsigned char c in arr)
list->Add(c);
}
return Encoding::UTF32->GetString(list->ToArray());
}
int main(array<System::String ^> ^args)
{
genererCle("pp", "p");
StreamReader ^sr = gcnew StreamReader("c:\\test.txt");
String ^ chaine = sr->ReadToEnd();
sr->Close();
String ^ encrypted = encryptString("p", chaine);
String ^ decrypted = DecryptString("pp", encrypted);
//String ^chaineDecodee = Text::Encoding::Unicode->GetString(decrypted);
Console::WriteLine(decrypted);
return 0;
} |
Partager