0

我有 C#、VB.net 和 VC++(VS 2008)中的加密解密代码,任何人都可以解密任何人的加密文件。

现在的要求是,我必须在 VC++ 6.0 中使用 VC++ (VS 2008) 静态库文件。

或建议任何其他替代方法(不带 dll)

namespace RijndaelEncDec
{

    void Rijndael::LN_EncryptFile(String^ fileSource, String^ fileDestination, String^ password)
    {
        // Declare the streams used
        // to encrypt to an in memory
        // array of bytes.

        CryptoStream^   cryptoStream;

        FileStream^ fsIn;
        FileStream^ fsOut ;

        // Declare the RijndaelManaged object
        // used to encrypt the data.
        RijndaelManaged^ RijndaelCipher;

        try
        {           
            fsIn=gcnew FileStream(fileSource, FileMode::Open, FileAccess::Read);
            fsOut = gcnew FileStream(fileDestination, FileMode::Create, FileAccess::Write);


            array<Byte>^  salt = gcnew array<Byte>(13){ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };
            Rfc2898DeriveBytes ^secretKey =gcnew Rfc2898DeriveBytes(password,salt);

            RijndaelCipher = gcnew RijndaelManaged();
            RijndaelCipher->Padding = PaddingMode::PKCS7;
            RijndaelCipher->Mode=CipherMode::CBC;
            RijndaelCipher->BlockSize=128;
            RijndaelCipher->Key = secretKey->GetBytes(32);
            RijndaelCipher->IV = secretKey->GetBytes(16);


            ICryptoTransform^ encryptor = RijndaelCipher->CreateEncryptor();
            cryptoStream = gcnew CryptoStream(fsOut, encryptor, CryptoStreamMode::Write);


            int ByteData;
            while ((ByteData=fsIn->ReadByte()) != -1)
            {
              cryptoStream->WriteByte(ByteData);
            }

          cryptoStream->FlushFinalBlock();

        }
         catch (FileNotFoundException^ ex)
            {
                if ((ex->FileName->CompareTo(fileSource) == 0) && (GlobalVariableClass::logPermission == true))
                {
                    StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                    writer->WriteLine(ex->Message);
                    writer->Close();
                }
                if ((ex->FileName->CompareTo(fileDestination) == 0) && GlobalVariableClass::logPermission == true)
                {
                    StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                    writer->WriteLine(ex->Message + " i.e. Input file for Encryption");
                    writer->Close();
                }

            }
         catch (Exception^ ex)
            {
                if(GlobalVariableClass::logPermission==true)
                {
                    StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                    writer->WriteLine(ex->Message + " During Encrypting File");
                    writer->Close();

                }


            }
        finally
        {


            // Close the streams.

            if (cryptoStream)
                cryptoStream->Close();
            if(fsIn)
                fsIn->Close();
            if(fsOut)
                fsOut->Close();
            // Clear the RijndaelManaged object.
            if (RijndaelCipher)
                RijndaelCipher->Clear();
        }

        // Return the encrypted bytes from the memory stream.
        return ;//msEncrypt->ToArray();
    }

    void Rijndael::DecryptFile(String^ fileSource, String^ fileDestination, String^ password)
    {



        array<Byte>^  salt = gcnew array<Byte>(13) { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };
        Rfc2898DeriveBytes ^secretKey =gcnew Rfc2898DeriveBytes(password,salt);       

        CryptoStream^ csDecrypt;
        StreamReader^ srDecrypt;
        FileStream^ fsIn;
        FileStream^ fsOut;

        // Declare the RijndaelManaged object
        // used to decrypt the data.
        RijndaelManaged^ RijndaelCipher;



        try
        {

            fsIn  = gcnew FileStream(fileSource, FileMode::Open, FileAccess::Read);
            fsOut = gcnew FileStream(fileDestination, FileMode::Create, FileAccess::Write);   

            RijndaelCipher = gcnew RijndaelManaged();
            RijndaelCipher->Padding = PaddingMode::PKCS7;
            RijndaelCipher->Key = secretKey->GetBytes(32);
            RijndaelCipher->IV = secretKey->GetBytes(16);

            // Create a decrytor to perform the stream transform.
            ICryptoTransform^ decryptor = RijndaelCipher->CreateDecryptor();

            // Create the streams used for decryption.

            csDecrypt = gcnew CryptoStream(fsOut, decryptor, CryptoStreamMode::Write);

            int ByteData;
            while ((ByteData=fsIn->ReadByte()) != -1)

            {

              csDecrypt->WriteByte(ByteData);

            }





        }
         catch (FileNotFoundException^ ex)
                {
                    if ((ex->FileName->CompareTo(fileSource) == 0) && (GlobalVariableClass::logPermission == true))
                    {

                        StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                        writer->WriteLine(ex->Message + " i.e. Input file for decryption");
                        writer->Close();
                    }
                    if ((ex->FileName->CompareTo(fileDestination) == 0) && GlobalVariableClass::logPermission == true)
                    {
                        StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                        writer->WriteLine(ex->Message + " i.e. Output file while decryption");
                        writer->Close();
                    }

                }
                catch (Exception^ ex)
                {
                    if (GlobalVariableClass::logPermission == true)
                    {
                        StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                        writer->WriteLine(ex->Message + " During Decrypting File");
                        writer->Close();
                    }

                }
        finally
        {
            // Clean things up.

            // Close the streams.
            if (srDecrypt)
                srDecrypt->Close();
            if (csDecrypt)
                csDecrypt->Close();

               fsIn->Close();
               fsOut->Close();


            // Clear the RijndaelManaged object.
            if (RijndaelCipher)
                RijndaelCipher->Clear();
        }

        return ;
    }

    String^ Rijndael:: ReadEncryptFileToBuffer(String^ fileSource,[System::Runtime::InteropServices::OutAttribute]  String ^% buffer,String^ password)
    {
        FileStream^ fsIn; 
        MemoryStream^ memoryStream;
        CryptoStream^ cryptoStream;
        StreamReader^ fileReader,^streamReader;
        RijndaelManaged^ RijndaelCipher;
        String^ decryptText;

         // First we are going to open the file streams
        try
        {
          RijndaelCipher = gcnew RijndaelManaged();
          RijndaelCipher->Mode = CipherMode::CBC;
          RijndaelCipher->KeySize = 256;
          RijndaelCipher->BlockSize = 128;
          RijndaelCipher->Padding = PaddingMode::PKCS7;

          fsIn = gcnew FileStream(fileSource, FileMode::Open, FileAccess::Read);
          fileReader = gcnew StreamReader(fsIn,System::Text::Encoding::Default);
          String^ cipherText = fileReader->ReadToEnd();
          array<Byte>^ cipherByte = System::Text::Encoding::Default->GetBytes(cipherText);



          array<Byte>^  salt = gcnew array<Byte>(13) { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };  
          Rfc2898DeriveBytes^ secretKey = gcnew Rfc2898DeriveBytes(password, salt);
          ICryptoTransform^ Decryptor = RijndaelCipher->CreateDecryptor(secretKey->GetBytes(32), secretKey->GetBytes(16));





          memoryStream = gcnew MemoryStream(cipherByte);
          cryptoStream = gcnew CryptoStream(memoryStream, Decryptor, CryptoStreamMode::Read);
          streamReader = gcnew StreamReader(cryptoStream);
          decryptText = streamReader->ReadToEnd();
          buffer = decryptText;
          Console::WriteLine(decryptText);



        }
         catch (FileNotFoundException^ ex)
        {
            if (ex->FileName->CompareTo(fileSource) == 0 && GlobalVariableClass::logPermission == true)
            {

                StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                writer->WriteLine(ex->Message + " i.e. Input file for decryption Buffer");
                writer->Close();

            }


        }
        catch (Exception^ ex)
        {
            if (GlobalVariableClass::logPermission == true)
            {
                StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                writer->WriteLine(ex->Message + "i.e. During Reading Encrypting File to Decrypted Buffer");
                writer->Close();
            }

        }
        finally
        {
          if(fsIn)
          fsIn->Close();
          if(memoryStream)
          memoryStream->Close();
          if(cryptoStream)
          cryptoStream->Close();
          if(streamReader)
          streamReader->Close();

        }
        return decryptText;
    }

    void Rijndael::WriteEncFileFromDecBuffer(String^ buffer, String^ fileDestination, String^ password)
    {
        FileStream^ fsOut; 
        CryptoStream^ cryptoStream;
        StreamReader^ streamReader;

        try
        {
            RijndaelManaged^ RijndaelCipher = gcnew RijndaelManaged();
            RijndaelCipher->Mode = CipherMode::CBC;
            RijndaelCipher->KeySize = 256;
            RijndaelCipher->BlockSize = 128;
            RijndaelCipher->Padding = PaddingMode::PKCS7;

            FileStream^ fsOut = gcnew FileStream(fileDestination, FileMode::Create, FileAccess::Write);

            array<Byte>^  salt = gcnew array<Byte>(13) { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };
            array<Byte>^ plainText = System::Text::Encoding::Default->GetBytes(buffer);
            Rfc2898DeriveBytes^ secretKey = gcnew Rfc2898DeriveBytes(password, salt);
            ICryptoTransform^ Encryptor = RijndaelCipher->CreateEncryptor(secretKey->GetBytes(32), secretKey->GetBytes(16));

            cryptoStream = gcnew CryptoStream(fsOut, Encryptor, CryptoStreamMode::Write);

            cryptoStream->Write(plainText, 0, plainText->Length);

            cryptoStream->FlushFinalBlock();
            cryptoStream->Close();
            fsOut->Close();
        }

        catch(FileNotFoundException^ ex)
        {
            if ( GlobalVariableClass::logPermission == true)
            {

                StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                writer->WriteLine(ex->Message + " i.e. Output file from Dencrypted Buffer");
                writer->Close();
            }

        }
        catch(Exception^ ex)
        {
            if (GlobalVariableClass::logPermission == true)
            {
                StreamWriter^ writer = gcnew StreamWriter(GlobalVariableClass::logFileLocation);
                writer->WriteLine(ex->Message + " During Writing Encrypting File from Decrypted Buffer");
                writer->Close();

            }
        }
        finally
        {
          fsOut->Close();
          cryptoStream->Close();
          streamReader->Close();

        } 
    }
}
4

3 回答 3

1

你不可以做这个。VS6 甚至早于 .NET 存在,因此您需要为托管组件提供一个接口,该接口可通过 VS6 所知道的技术进行访问。两个最简单的选择:

  1. 为托管库创建一个 COM 接口并在 C++ 中使用它
  2. 创建一个 C 风格的接口并在 C++ 中使用它。不是 C++ 接口,因为 C++ 对象和功能跨越 DLL 边界的问题。

我不明白需要 .lib 文件的要求;两个明显的解决方案都不会使用这个。如果您想要更好的答案,您应该更清楚为什么“需要” .lib。问题是您有一组不可能的要求:

  1. 您不能使用 DLL(仅限于一个 PE 模块)
  2. 托管和非托管不能在同一个 PE 中共存。
  3. VS6 无法编写托管代码。

这三个限制证明不可能如你所愿;你需要在某个地方妥协。

于 2012-08-29T10:17:02.003 回答
0

我想你最好的选择是为你的一个实现中的函数编写一个 C 包装器,并从中制作一个 DLL。

于 2012-08-29T10:08:55.550 回答
0

相反,我建议您将 VC++ 6.0 代码升级到 VC++ 9.0(或任何包含 .NetFramework 支持的版本),该版本也可以在托管环境中编译您的应用程序代码。所以现在您可以使用您的托管静态库,也可以嵌入您的库托管代码,因为它可以根据您的要求使用它的所有功能。

您的要求似乎与使用静态库而不是使用 DLL 进行加密/解密代码相关,因为 DLL 可以在客户端进行逆向工程。

是的还有一件事你的代码是用 C++/CLI(manged C++)编写的,所以在 VC++ 9.0 中编译之前,请确保在“CLR 支持”(通过通用属性)上切换。前进。

于 2012-08-31T06:45:18.653 回答