Thread: Help!!! How to correct the errors linked to class?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    40

    Help!!! How to correct the errors linked to class?

    Hi guys,

    I need your help. I am trying to build a Win32 project under Windows XP using managed C++ 2005 Express Edition. I have a bit of trouble with the errors. I am trying to linked the code to the class library, but i couldn't be able to find the solutions to get rid of the errors.

    error C2227: left of '->Decrypt' must point to class/struct/union/generic type


    The error are linked to the line:

    Code:
    if (plainText->Text == enc->Decrypt(key, "key123456"))

    And I also get another error using with the same line:

    error C3845: 'myapplication::Form1::key': only static data members can be initialized inside a ref class or value type

    Code:
    String ^key = "MoY3fOB+LbiQy6V3R35FASSmy";

    here's the current code:

    Code:
    #pragma once
    
    #include "Class1.h"
    
    
    namespace myapplication {
    
        using namespace System;
        using namespace System::Collections::Generic;
        using namespace System::Text;
        using namespace System::ComponentModel;
        using namespace System::Collections;
        using namespace System::Windows::Forms;
        using namespace System::Data;
        using namespace System::Drawing;
    
        /// <summary>
        /// Summary for Form1
        ///
        /// WARNING: If you change the name of this class, you will need to change the
        ///          'Resource File Name' property for the managed resource compiler tool
        ///          associated with all .resx files this class depends on.  Otherwise,
        ///          the designers will not be able to interact properly with localized
        ///          resources associated with this form.
        /// </summary>
        public ref class Form1 : public System::Windows::Forms::Form
        {
        public:
            Form1(void)
            {
                InitializeComponent();
                //
                //TODO: Add the constructor code here
                //
            }
    
        protected:
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            ~Form1()
            {
                if (components)
                {
                    delete components;
                }
            }
        internal: System::Windows::Forms::Label^  label2;
        protected:
        internal: System::Windows::Forms::Button^  Button1;
        internal: System::Windows::Forms::TextBox^  plainText;
    
        private:
            /// <summary>
            /// Required designer variable.
            /// </summary>
            System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            void InitializeComponent(void)
            {
                this->label2 = (gcnew System::Windows::Forms::Label());
                this->Button1 = (gcnew System::Windows::Forms::Button());
                this->plainText = (gcnew System::Windows::Forms::TextBox());
                this->SuspendLayout();
                //
                // label2
                //
                this->label2->AutoSize = true;
                this->label2->Location = System::Drawing::Point(24, 46);
                this->label2->Name = L"label2";
                this->label2->Size = System::Drawing::Size(302, 13);
                this->label2->TabIndex = 6;
                this->label2->Text = L"Do you know what the password is\? if so please enter it here...";
                //
                // Button1
                //
                this->Button1->Location = System::Drawing::Point(122, 101);
                this->Button1->Name = L"Button1";
                this->Button1->Size = System::Drawing::Size(96, 34);
                this->Button1->TabIndex = 5;
                this->Button1->Text = L"Confirm password!";
                this->Button1->UseVisualStyleBackColor = true;
                this->Button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click);
                //
                // plainText
                //
                this->plainText->Location = System::Drawing::Point(27, 65);
                this->plainText->Name = L"plainText";
                this->plainText->Size = System::Drawing::Size(292, 20);
                this->plainText->TabIndex = 4;
                //
                // Form1
                //
                this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                this->ClientSize = System::Drawing::Size(341, 162);
                this->Controls->Add(this->label2);
                this->Controls->Add(this->Button1);
                this->Controls->Add(this->plainText);
                this->MaximizeBox = false;
                this->MinimizeBox = false;
                this->Name = L"Form1";
                this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Hide;
                this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
                this->Text = L"myapplication";
                this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                this->ResumeLayout(false);
                this->PerformLayout();
    
            }
    #pragma endregion
    
        myapplication ::Class1 ^enc();
        myapplication ::Class1 ^dec();
        String ^key = "MoY3fOB+LbiQy6V3R35FASSmy";
    
        private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                     this->Select();
                 }
        private: System::Void Button1_Click(System::Object^  sender, System::EventArgs^  e) {
                     if (plainText->Text == enc->Decrypt(key, "key123456"))
                     {
                         MessageBox::Show("the password is correct!");
                     }
                     else
                     {
                         MessageBox::Show("the password is incorrect");
                     }
                 }
        };
    }
    I have input the "include class.h" on the top of the header, but it doesn't help me to get correct the errors. And also I cannot understand the second error I have got which I created the name of the string.

    Can somebody please tell me how to fix those errors?

    Any advice would be much appreciated.

    Thanks in advance.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If these are supposed to be variables, lose the () at the end of them:
    Code:
        myapplication ::Class1 ^enc();
        myapplication ::Class1 ^dec();
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Thanks for your replied. I have removed the the () at the end of them which I can see it have fixed the error, but i couldn't be able to correct the error using with this line:

    Code:
    String ^key = "MoY3fOB+LbiQy6V3R35FASSmy";

    error C3845: 'myapplication::Form1::key': only static data members can be initialized inside a ref class or value type


    I don't understand what it meant to be and i can't figure out what the problem is. I want to created the string variables which it can be declared on if statement.

    Do you have any idea how to fix the error on above?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    FYI, this isn't C++ (it is C++/CLI), hence it would be better if you posted it in TechBoard instead of C++ with such questions in the future.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    i think i have fixed the problem. however when I have ran the debug and when I clicked on the button, I get an unhandled exception error of System.NullReferenceException.

    Here's the full error I get:


    An unhandled exception of type 'System.NullReferenceException' occurred in myapplication.exe

    Additional information: Object reference not set to an instance of an object.



    The error are linked to this line:

    Code:
    this->algo->IV = encIv;

    Here's the current code:

    Code:
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Diagnostics;
    using namespace System::Collections::Generic;
    using namespace System::Text;
    
    namespace myapplication {
    
    	/// <summary>
    	/// Summary for Class1
    	/// </summary>
    	public ref class Class1 :  public System::ComponentModel::Component
    	{
    	public:
    		Class1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    		Class1(System::ComponentModel::IContainer ^container)
    		{
    			/// <summary>
    			/// Required for Windows.Forms Class Composition Designer support
    			/// </summary>
    
    			container->Add(this);
    			InitializeComponent();
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Class1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			components = gcnew System::ComponentModel::Container();
    		}
    #pragma endregion
    
    
    	public: void PolyAES()
    	{
    		this->algo = gcnew System::Security::Cryptography::RijndaelManaged();
    		this->algo->Mode = System::Security::Cryptography::CipherMode::CBC;
    		this->rngAlgo = gcnew System::Security::Cryptography::RNGCryptoServiceProvider();
    	}
    
    private:
    	literal int saltSize = 32;
    
    	System::Security::Cryptography::SymmetricAlgorithm ^algo;
    	System::Security::Cryptography::RNGCryptoServiceProvider ^rngAlgo;
    	array<Byte> ^salt;
    
    
    
    private:
    	void InitializeSecureParameters(array<Byte> ^key)
    	{
    		// init rijndael IV
    		this->algo->GenerateIV();
    		salt = gcnew array<Byte>(saltSize);
    		rngAlgo->GetBytes(salt);
    		System::Security::Cryptography::Rfc2898DeriveBytes ^pwDeriveAlg = gcnew System::Security::Cryptography::Rfc2898DeriveBytes(key, salt, 2000);
    		this->algo->Key = pwDeriveAlg->GetBytes(32);
    	}
    
    	void LoadSecureParameters(array<Byte> ^key, array<Byte> ^encIv, array<Byte> ^encSalt)
    	{
    		this->algo->IV = encIv;
    		this->salt = encSalt;
    		System::Security::Cryptography::Rfc2898DeriveBytes ^pwDeriveAlg = gcnew System::Security::Cryptography::Rfc2898DeriveBytes(key, salt, 2000);
    		this->algo->Key = pwDeriveAlg->GetBytes(32);
    	}
    
    public:
    	String ^Encrypt(String ^plainText, String ^key)
    	{
    		return Convert::ToBase64String(this->Encrypt(UnicodeEncoding::UTF8->GetBytes(plainText), UnicodeEncoding::UTF8->GetBytes(key)));
    	}
    
    	String ^Decrypt(String ^cipherText, String ^key)
    	{
    		return UnicodeEncoding::UTF8->GetString(this->Decrypt(Convert::FromBase64String(cipherText), UnicodeEncoding::UTF8->GetBytes(key)));
    	}
    
    	array<Byte> ^Encrypt(array<Byte> ^plainText, array<Byte> ^key)
    	{
    		InitializeSecureParameters(key);
    		System::Security::Cryptography::ICryptoTransform ^encTransform = algo->CreateEncryptor();
    		return ConcatDataToCipherText(ConcatDataToCipherText(encTransform->TransformFinalBlock(plainText, 0, plainText->Length), salt), algo->IV);
    	}
    
    	array<Byte> ^Decrypt(array<Byte> ^cipherText, array<Byte> ^key)
    	{
    		array<Byte> ^cipherTextWithSalt = gcnew array<Byte>(1);
    		array<Byte> ^encSalt = gcnew array<Byte>(1);
    		array<Byte> ^origCipherText = gcnew array<Byte>(1);
    		array<Byte> ^encIv = gcnew array<Byte>(1);
    
    		SliceCipherTextIntoParts(cipherText, 16, cipherTextWithSalt, encIv);
    		SliceCipherTextIntoParts(cipherTextWithSalt, saltSize, origCipherText, encSalt);
    		LoadSecureParameters(key, encIv, encSalt);
    		System::Security::Cryptography::ICryptoTransform ^decTransform = algo->CreateDecryptor();
    		array<Byte> ^plainText = decTransform->TransformFinalBlock(origCipherText, 0, origCipherText->Length);
    
    		return plainText;
    	}
    
    private:
    	array<Byte> ^ConcatDataToCipherText(array<Byte> ^cipherText, array<Byte> ^iv)
    	{
    		int origLength = cipherText->Length;
    		Array::Resize(cipherText, cipherText->Length + iv->Length);
    		Buffer::BlockCopy(iv, 0, cipherText, origLength, iv->Length);
    		return cipherText;
    	}
    	void SliceCipherTextIntoParts(array<Byte> ^cipherText, int secondPartLen, array<Byte> ^%origCipherText, array<Byte> ^%iv)
    	{
    		Array::Resize(iv, secondPartLen);
    		Buffer::BlockCopy(cipherText, Convert::ToInt32(cipherText->Length - secondPartLen), iv, 0, secondPartLen);
    		Array::Resize(origCipherText, Convert::ToInt32(cipherText->Length - secondPartLen));
    		Buffer::BlockCopy(cipherText, 0, origCipherText, 0, Convert::ToInt32(cipherText->Length - secondPartLen));
    	}
    
    	};
    }

    Any idea how to fix the error?

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    The error means that "algo" is null. You need to gcnew something.

    Please note that Managed C++ | C++/CLI was never intended as a real programming language of it's own. It's a crutch to run .NET code from C++ or interface C++ to .NET code.

    If you want the power of the .NET Framework (which is great) then use C#. If you want the power of a native application (which is also great) then use C++.

    Combining them is only giving you the sum of it's limitations. You have the raw power (none) of the .NET Framework added to the syntactic mystery that is C++. Use one. Or the other. You will know when you need both and I will take a wild guess and say that if you don't know how to spot a null-reference-exception, you don't need this right now.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a (atom) class in a (struct) class makes big errors!
    By Yarin in forum Windows Programming
    Replies: 4
    Last Post: 09-11-2007, 07:18 PM
  2. Seemingly correct printf statements bringing up errors
    By hpteenagewizkid in forum C Programming
    Replies: 9
    Last Post: 11-08-2006, 12:13 PM
  3. Help me correct game errors (dos)
    By Mech0z in forum Game Programming
    Replies: 0
    Last Post: 02-11-2006, 02:21 PM
  4. Global variables used in a linked class, getting errors.
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2005, 12:25 AM
  5. Linked list, is this correct?
    By scrappy in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 12:06 AM