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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;
using namespace System::IO;
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];
}
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);
}
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 = gcnew array<unsigned char>(encrypted->Length);
csDecrypt->Read(fromEncrypt, 0, fromEncrypt->Length);
return textConverter->GetString(fromEncrypt);
}
int main(array<System::String ^> ^args)
{
StreamReader ^sr = gcnew StreamReader("c:\\test.txt");
StreamWriter ^sw = gcnew StreamWriter("c:\\testCrypt.txt");
try
{
sw->Write(Crypter(sr->ReadToEnd(), "code secret"));
}
catch (Exception^)
{
}
finally
{
sr->Close();
sw->Close();
}
sr = gcnew StreamReader("c:\\testCrypt.txt");
sw = gcnew StreamWriter("c:\\testDeCrypt.txt");
try
{
sw->Write(Decrypter(sr->ReadToEnd(), "code secret"));
}
catch (Exception^)
{
}
finally
{
sr->Close();
sw->Close();
}
return 0;
} |
Partager