En fait j'utilise visual studio 2005 (sous visual c++)
j'ai réussi à trouver des méthodes pour crypter et décrypter mais le problème est que ca ne crypte que des fichiers txt .
ca peut crypter d'autres types de fichiers par exemole un test.zip, mais si je le crypte et je le decrypte j'aurais par exemple un fichier test1.zip
ce fichier quand je veux l'ouvrir il me dit fichier corrompu!
est ce que quelqu'un a une idée pour pouvoir crypter et decrypter tout genre de fichier
voici le code:
public
:
void
GenerateKey(String ^SecretPhrase,
array
<
unsigned
char
> ^&Key,
array
<
unsigned
char
> ^&IV)
{
array
<
unsigned
char
> ^bytePhrase = Encoding::ASCII->GetBytes(SecretPhrase);
SHA384Managed ^sha384 = gcnew SHA384Managed();
sha384->ComputeHash(bytePhrase);
array
<
unsigned
char
> ^result = sha384->Hash;
for
(
int
loop = 0; loop < 24; loop++)
Key[loop] = result[loop];
for
(
int
loop = 24; loop < 40; loop++)
IV[loop - 24] = result[loop];
}
public
:
String ^ Crypter(String ^original, String ^keyPhrase)
{
array
<
unsigned
char
> ^Key =
gcnew
array
<
unsigned
char
>(24);
array
<
unsigned
char
> ^IV =
gcnew
array
<
unsigned
char
>(16);
GenerateKey(keyPhrase, Key, IV);
ASCIIEncoding ^textConverter = gcnew ASCIIEncoding();
RijndaelManaged ^myRijndael = gcnew RijndaelManaged();
array
<
unsigned
char
> ^encrypted;
array
<
unsigned
char
> ^toEncrypt;
myRijndael->Key = Key;
myRijndael->IV = IV;
ICryptoTransform ^encryptor = myRijndael->CreateEncryptor(Key, IV);
MemoryStream ^msEncrypt = gcnew MemoryStream();
CryptoStream ^csEncrypt = gcnew CryptoStream(msEncrypt, encryptor, CryptoStreamMode::Write);
toEncrypt = textConverter->GetBytes(original);
csEncrypt->Write(toEncrypt, 0, toEncrypt->Length);
csEncrypt->FlushFinalBlock();
encrypted = msEncrypt->ToArray();
return
Convert::ToBase64String(encrypted);
}
public
:
String ^ Decrypter(String ^ encryptedString, String ^keyPhrase)
{
array
<
unsigned
char
> ^Key =
gcnew
array
<
unsigned
char
>(24);
array
<
unsigned
char
> ^IV =
gcnew
array
<
unsigned
char
>(16);
GenerateKey(keyPhrase, Key, IV);
array
<
unsigned
char
> ^encrypted = Convert::FromBase64String(encryptedString);
array
<
unsigned
char
> ^fromEncrypt;
RijndaelManaged ^myRijndael =
gcnew
RijndaelManaged();
ASCIIEncoding ^textConverter =
gcnew
ASCIIEncoding();
myRijndael->Key = Key;
myRijndael->IV = IV;
ICryptoTransform ^decryptor = myRijndael->CreateDecryptor(Key, IV);
MemoryStream ^msDecrypt = gcnew MemoryStream(encrypted);
CryptoStream ^csDecrypt = gcnew CryptoStream(msDecrypt, decryptor, CryptoStreamMode::Read);
fromEncrypt = gcnewarray < unsignedchar >(encrypted->Length);
csDecrypt->Read(fromEncrypt, 0, fromEncrypt->Length);
return
textConverter->GetString(fromEncrypt);
}
int main( array <System::String ^> ^args){
StreamReader ^sr = gcnew StreamReader( this ->val3);
StreamWriter ^sw = gcnew StreamWriter( this ->val5);
try
{
String ^a = Crypter(sr->ReadToEnd(), this ->GetValue1());
sw->Write(a);
}
catch
(Exception^)
{
}
finally
{
sr->Close();
sw->Close();
}
StreamReader ^sr = gcnew StreamReader( this ->val3);
StreamWriter ^sw = gcnew StreamWriter( this ->val5);
try
{
String ^a = Decrypter(sr->ReadToEnd(), this ->GetValue1());
sw->Write(a);
}
catch
(Exception^)
{
}
finally
{
sr->Close();
sw->Close();
}
return0;
}