我想使用XOR方法制作加密器/解密器。我已經在Console C++中完成了這個工作,並且我是VC++的新手,所以我希望你能幫助我。 加密的工作原理是這樣的:Visual C++奇怪的編譯器錯誤
key = 3
x = 5 // The item being encrypted
encryptedx = x^key // the "^" is XOR
所以,現在,我希望把它在VC++,使這更好看的控制檯窗口。 在VC++的設計視圖中,我有兩個富文本框,編輯框和一些按鈕來啓動過程。 這聽起來很容易,但是我得到這些錯誤:
------ Build started: Project: Encryptor, Configuration: Debug Win32 ------
1> Encryptor.cpp
1>c:\users\**********c*********ncryptor\encryptor\Form1.h(264): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(707): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(std::basic_string<_Elem,_Traits,_Ax> &&)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(762): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(767): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(772): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::string, System::String ^)'
1>c:\*******gl\*****cryptor\encryptor\Form1.h(281): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'System::String ^' (or there is no acceptable conversion)
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(707): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(std::basic_string<_Elem,_Traits,_Ax> &&)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(762): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(767): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\xstring(772): or 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> while trying to match the argument list '(std::string, System::String ^)'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
這裏是代碼:
#include <string>
#include "crypFunc.cpp"
int key = 0;
char tempChar;
int stringLenght = 0;
string strBuffer = "";
using namespace std;
//
//
//
//
//
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if(KeyBox->Text)
{
key =Convert::ToInt32(KeyBox->Text);
numericUpDown1->Value = key;
}
}
//
//
//
//
private: System::Void EncryptButton_Click(System::Object^ sender, System::EventArgs^ e)
{
EncryptedBox->Text = "";
strBuffer = DecryptedBox->Text;
stringLenght = strBuffer.size();
if(strBuffer.size() && key)
{
for(int i = 0; i < stringLenght; i++)
{
tempChar = encrypt(strBuffer[i], key);
EncryptedBox->Text = EncryptedBox->Text + tempChar;
}
}
}
private: System::Void DecryptButton_Click(System::Object^ sender, System::EventArgs^ e)
{
DecryptedBox->Text = "";
strBuffer = Convert::ToString(EncryptedBox->Text);
stringLenght = strBuffer.size();
if(strBuffer.size() && key)
{
for(int i = 0; i < stringLenght; i++)
{
tempChar = decrypt(strBuffer[i], key);
DecryptedBox->Text = DecryptedBox->Text + tempChar;
}
}
}
};
}
我真的希望你們中的一些可以和想幫助我。
嘿。我理解你的答案,並且在源代碼中設置了它,但它仍然無效。 你能告訴我你將如何解決在System :: Void EncryptButton_Click這個問題? – Janman 2011-02-08 19:28:42