我试图对文件进行AES加密。进行了搜索并找到了this C# sample。这是我尝试将其转换为C ++ / CLI。
using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;
void DecryptFile(String^ sourceFilename, String^ destinationFilename, String^ password, array<Byte>^ salt, int iterations){
try{
RijndaelManaged^ aes = gcnew RijndaelManaged();
aes->BlockSize = aes->LegalBlockSizes[0]->MaxSize;
aes->KeySize = aes->LegalKeySizes[0]->MaxSize;
// NB: Rfc2898DeriveBytes initialization and subsequent calls to GetBytes must be eactly the same, including order, on both the encryption and decryption sides.
Rfc2898DeriveBytes ^key = gcnew Rfc2898DeriveBytes(password, salt, iterations);
aes->Key = key->GetBytes(aes->KeySize / 8);
aes->IV = key->GetBytes(aes->BlockSize / 8);
aes->Mode = CipherMode::CBC;
ICryptoTransform^ transform = aes->CreateDecryptor(aes->Key, aes->IV);
FileStream^ destination = gcnew FileStream(destinationFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::None);
CryptoStream^ cryptoStream = gcnew CryptoStream(destination, transform, CryptoStreamMode::Write);
FileStream^ source = gcnew FileStream(sourceFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
source->CopyTo(cryptoStream);
}
catch (Exception^ exception){Console::WriteLine(exception->Message);}
}
void EncryptFile(String^ sourceFilename, String^ destinationFilename, String^ password, array<Byte>^ salt, int iterations){
try{
RijndaelManaged^ aes = gcnew RijndaelManaged();
aes->BlockSize = aes->LegalBlockSizes[0]->MaxSize;
aes->KeySize = aes->LegalKeySizes[0]->MaxSize;
// NB: Rfc2898DeriveBytes initialization and subsequent calls to GetBytes must be eactly the same, including order, on both the encryption and decryption sides.
Rfc2898DeriveBytes^ key = gcnew Rfc2898DeriveBytes(password, salt, iterations);
aes->Key = key->GetBytes(aes->KeySize / 8);
aes->IV = key->GetBytes(aes->BlockSize / 8);
aes->Mode = CipherMode::CBC;
ICryptoTransform^ transform = aes->CreateEncryptor(aes->Key, aes->IV);
FileStream^ destination = gcnew FileStream(destinationFilename, FileMode::OpenOrCreate, FileAccess::Write, FileShare::None);
CryptoStream^ cryptoStream = gcnew CryptoStream(destination, transform, CryptoStreamMode::Write);
FileStream^ source = gcnew FileStream(sourceFilename, FileMode::Open, FileAccess::Read, FileShare::Read);
source->CopyTo(cryptoStream);
}
catch(Exception^ exception){Console::WriteLine(exception->Message);}
}
int main(){
srand(time(NULL));
array<Byte>^ salt = gcnew array<Byte> { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Must be at least eight bytes. MAKE THIS SALTIER!
int iterations=rand();
EncryptFile("C:\\Users\\test\\Desktop\\test.txt", "C:\\Users\\test\\Desktop\\test1.txt", "afckingpinecone.", salt, iterations);
DecryptFile("C:\\Users\\test\\Desktop\\test1.txt", "C:\\Users\\test\\Desktop\\test2.txt", "afckingpinecone.", salt, iterations);
return 0;
}
我测试了2个文件。一个.png和一个明文。图像加密和解密正常,但文本文件没有。原始文件包含15个字节的ASCII字符串,但加密和解密的文件为0个字节。
答案 0 :(得分:0)
您忘记关闭流,将以下行放在加密/解密函数的末尾。
cryptoStream->Close();
destination->Close();