Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net AES文件加密在C++/CLI_.net_Encryption_C++ Cli - Fatal编程技术网

.net AES文件加密在C++/CLI

.net AES文件加密在C++/CLI,.net,encryption,c++-cli,.net,Encryption,C++ Cli,我正试图加密一个文件。做了一次搜索,找到了。这是我将其转换为C++/CLI的尝试 using namespace System; using namespace System::IO; using namespace System::Security::Cryptography; void DecryptFile(String^ sourceFilename, String^ destinationFilename, String^ password, array<Byte>^ s

我正试图加密一个文件。做了一次搜索,找到了。这是我将其转换为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;
}
使用名称空间系统;
使用名称空间系统::IO;
使用名称空间System::Security::Cryptography;
无效解密文件(字符串^sourceFilename、字符串^destinationFilename、字符串^password、数组^salt、整数迭代){
试一试{
RijndaelManaged^aes=gcnew RijndaelManaged();
aes->BlockSize=aes->LegalBlockSize[0]->MaxSize;
aes->KeySize=aes->LegalKeySizes[0]->MaxSize;
//注意:Rfc2898DeriveBytes初始化和对GetBytes的后续调用在加密和解密端都必须完全相同,包括顺序。
Rfc2898DeriveBytes^key=gcnew Rfc2898DeriveBytes(密码、salt、迭代次数);
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=gcnewfilestream(destinationFilename,FileMode::OpenOrCreate,FileAccess::Write,FileShare::None);
CryptoStream^CryptoStream=gcnew CryptoStream(目标、转换、CryptoStreamMode::Write);
FileStream^source=gcnewfilestream(sourceFilename,FileMode::Open,FileAccess::Read,FileShare::Read);
源->复制到(加密流);
}
catch(异常^Exception){Console::WriteLine(异常->消息);}
}
void EncryptFile(字符串^sourceFilename,字符串^destinationFilename,字符串^password,数组^salt,int迭代次数){
试一试{
RijndaelManaged^aes=gcnew RijndaelManaged();
aes->BlockSize=aes->LegalBlockSize[0]->MaxSize;
aes->KeySize=aes->LegalKeySizes[0]->MaxSize;
//注意:Rfc2898DeriveBytes初始化和对GetBytes的后续调用在加密和解密端都必须完全相同,包括顺序。
Rfc2898DeriveBytes^key=gcnew Rfc2898DeriveBytes(密码、salt、迭代次数);
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=gcnewfilestream(destinationFilename,FileMode::OpenOrCreate,FileAccess::Write,FileShare::None);
CryptoStream^CryptoStream=gcnew CryptoStream(目标、转换、CryptoStreamMode::Write);
FileStream^source=gcnewfilestream(sourceFilename,FileMode::Open,FileAccess::Read,FileShare::Read);
源->复制到(加密流);
}
catch(异常^Exception){Console::WriteLine(异常->消息);}
}
int main(){
srand(时间(空));
数组^salt=gcnew数组{0x00,0x00,0x00,0x00,0x00,0x00,0x00};//必须至少为八个字节。请使此数组更咸!
int迭代次数=rand();
加密文件(“C:\\Users\\test\\Desktop\\test.txt”、“C:\\Users\\test\\Desktop\\test1.txt”、“afckingpinecone.”、salt、iterations);
解密文件(“C:\\Users\\test\\Desktop\\test1.txt”、“C:\\Users\\test\\Desktop\\test2.txt”、“afckingpinecone.”、salt、iterations);
返回0;
}

我测试了2个文件。一个.png和一个纯文本。图像加密和解密很好,但文本文件没有。原始文件包含一个15字节的ASCII字符串,但加密和解密的文件是0字节。

如果忘记关闭流,请在加密/解密函数的末尾添加以下行

cryptoStream->Close();
destination->Close();

C#程序员往往会因为使用语句而陷入成功的陷阱。你必须学会找到同一个坑。按原样,CryptoStream->FlushFinalBlock()丢失,并且您也没有调用delete CryptoStream。所以没有最后一个街区了,谢谢。如果你把它作为一个答案贴出来,我会接受的,只要你自己贴出来,展示你使用的代码,然后接受它来结束你的问题。