You are here:

C/creating a GUI for my program on ms visual studio

Advertisement


Question

Dear sir,

Glad you are back!:) Everybody said you could help me better with this.

I have a program in c++ which demonstrates the RSA encryption and it runs perfectly.

However, i would like to add a Graphical user interface to the program, which would collect input from the user and display the desired results. MS Visual C++, does have a possibilty of creating this interface. however, i am at loss on how to integrate my prog code into the GUI code or vice versa.

I am sending you both codes, the working program code, and the gui code generated by visual studio. the attached image is the image of the window.

I sometimes think complicated. if there is an easier way to do this, i am all ears!

[code]


// Die RSA Verschlüsselung in C++. Version for MS Visio  

// RSA ENCRYPTION REQUIREMENTS
// 'p' and 'q' Primenumbers. n= p*q
// 'e' encryption key 'd' decryption key. [n, e] Public key pair, known to all. [n, d] Private key
//pair known to user alice bob or whoever.
// Encryption function C= M^e mod n. Decryption function M= C^d mod n  ; M = message in ASCII code. C= Coded message
// e is greater than  1 und has no prime factors with z in common.
//(e*d)mod z=1




#include "stdafx.h"
#include<string>
#include<iostream>

long getlong(const char* prompt)
{
   std::string userInput;
   std::cout << prompt;
   std::getline(std::cin, userInput);
return atoi(userInput.c_str());
}

char getchar(const char* prompt)
{
   std::string userInput;
   std::cout << prompt;
   std::getline(std::cin, userInput);
return userInput[0];
}





long z, n, e, d, C, M;

/*Defining functions*/
/*** PRIME GEN ***/
void primegen()
{
int a[10000];
int b = 2;
int count;
int x;

   a[0] = 0;
   a[1] = 0;
for (x=2;x<(sizeof(a)/sizeof(int));x++)
   {
       a[x] = 4;
   }
for(b;b <= (sizeof(a)/sizeof(int));b++)
/*All elements of the array are examined*/
   {
if(a[b]==4)
       {
           std::cout << b << std::endl;
        for(count = 1;(count*b) < (sizeof(a)/sizeof(int));++count)

           {
               a[(count*b)] = 1;
        }
      }
   }
}

/***ENCRYPTION FUNCTION***/
void ziffern()
{
int i; //for schleife.
   C=1;
for (i=0; i<e; i++)
   {
       C=C*M%n;
       C=C%n;

   }
   std::cout << "\t Chiffrierter Text: " << C << std::endl;


}

/***DECRYPTION FUNCTION***/
void entziffern()
{
int i;
   M=1;
for (i=0; i<d; i++)
       M=M*C%n;
   M=M%n;
   std::cout << "\t Dechiffrierter Text: " << M << std::endl;

}

/***PRIME NUMBER CHECK***/
/*the user chooses prime numbers from the list of numbers generated by PRIMEGEN and in case he makes an error, the NEXTPRIME finds the nearest prime number*/

bool isPrime(long number)
{
if (number <= 1) return false;
if (number == 2) return true;
for (long i = 2; i < number; ++i)
   {
if (number % i == 0) return false;
   }
return true;
}

long nextPrime(long number)
{
if (isPrime(number)) return number;
while(!isPrime(++number));
   std::cout << "Eingegebene Zahl ungueltig! Naechstgroesste Primzahl ist: " << number << std::endl;
return number;
}

/***HIGHEST COMMON FACTOR***/

bool isCoprime(long A, long B)
{
long end = A < B ? A : B;
for(int test = 2; test < end; ++test)
   {
if (A % test == 0 && B % test == 0) return false;
   }
return true;
}

// User prompt
/***MAIN PROGRAM***/

int main()
{

long p, q, i;

   std::cout << "primegen:\n";
   primegen();

   p=getlong("Waehlen Sie die erste Primzahl 'p' aus der Liste: \t");
   p=nextPrime(p);

  q=getlong("Waehlen Sie die zweite Primzahl 'q' aus der Liste: \t");
   q=nextPrime(q);


if (q == p) q = nextPrime(q+1);
   std::cout << "p = " << p << " q = " << q << std::endl;

   z = (p-1)*(q-1);
   n = p * q;
   std::cout << "\t Z = " << z << "\n";

   e = getlong("Geben Sie einen Wert fuer 'e' ein\t: ");


if (!isCoprime(e, z))
   {
while(!isCoprime(++e, z));
       std::cout << "Eingabe ungueltig. Den Wert " << e << " wurde fuer e ausgesucht!\n";
   }

do
   {
       i=(d*e)%z;
       d++;

   }while(i!=1);
   d=d-1;
   std::cout << "Der Wert fuer 'd' ist " << d << std::endl;


switch (getchar("\n\n Moechten Sie ziffern oder entziffern?(Z\\E):")) {
case 'z': case 'Z':
       M = getlong("\n\n Geben Sie die Nachricht ein die Sie ziffern moechten\t:");
       ziffern();
break;
case 'e': case 'E':
       C = getlong("\n\n Geben Sie die Nachricht ein die Sie entziffern moechten \t: ");
       entziffern();
break;
default : printf("\nz oder e eingeben\n");
   }
  getchar();
return 0;
}


[/code]

GENERATED GUI CODE
[/code]
#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace RSAGui {

  /// <summary>
  /// Zusammenfassung für RSA1
  ///
  /// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
  ///          die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
  ///          das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
  ///          Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
  ///          arbeiten, die diesem Formular zugewiesen sind.
  /// </summary>
  public ref class RSA1 : public System::Windows::Forms::Form
  {
  public:
     RSA1(void)
     {
        InitializeComponent();
        //
        //TODO: Konstruktorcode hier hinzufügen.
        //
     }

  protected:
     /// <summary>
     /// Verwendete Ressourcen bereinigen.
     /// </summary>
     ~RSA1()
     {
        if (components)
        {
           delete components;
        }
     }
  private: System::Windows::Forms::Label^  lblP;
  protected:

  private: System::Windows::Forms::Label^  lblQ;
  private: System::Windows::Forms::TextBox^  txtP;
  private: System::Windows::Forms::TextBox^  txtQ;




  private: System::Windows::Forms::Label^  lblN;
  private: System::Windows::Forms::Label^  lblZ;
  private: System::Windows::Forms::Label^  lblKlartext;
  private: System::Windows::Forms::Label^  lblGeheimtext;

  private: System::Windows::Forms::Label^  lblPrivatschlüssel;
  private: System::Windows::Forms::Label^  lblÖfflSchlusselE;
  private: System::Windows::Forms::TextBox^  txtN;
  private: System::Windows::Forms::TextBox^  txtZ;
  private: System::Windows::Forms::TextBox^  txtE;
  private: System::Windows::Forms::TextBox^  txtD;
  private: System::Windows::Forms::TextBox^  txtKlartext;
  private: System::Windows::Forms::TextBox^  txtGeheimtext;
  private: System::Windows::Forms::Button^  cmdZiffern;
  private: System::Windows::Forms::Button^  cmdEntziffern;
  private: System::Windows::Forms::Button^  cmdprimecheckP;






  private: System::Windows::Forms::Button^  button2;
  private: System::Windows::Forms::Button^  btValidE;




















  protected:

  private:
     /// <summary>
     /// Erforderliche Designervariable.
     /// </summary>
     System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
     /// <summary>
     /// Erforderliche Methode für die Designerunterstützung.
     /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
     /// </summary>
     void InitializeComponent(void)
     {
        this->lblP = (gcnew System::Windows::Forms::Label());
        this->lblQ = (gcnew System::Windows::Forms::Label());
        this->txtP = (gcnew System::Windows::Forms::TextBox());
        this->txtQ = (gcnew System::Windows::Forms::TextBox());
        this->lblN = (gcnew System::Windows::Forms::Label());
        this->lblZ = (gcnew System::Windows::Forms::Label());
        this->lblKlartext = (gcnew System::Windows::Forms::Label());
        this->lblGeheimtext = (gcnew System::Windows::Forms::Label());
        this->lblPrivatschlüssel = (gcnew System::Windows::Forms::Label());
        this->lblÖfflSchlusselE = (gcnew System::Windows::Forms::Label());
        this->txtN = (gcnew System::Windows::Forms::TextBox());
        this->txtZ = (gcnew System::Windows::Forms::TextBox());
        this->txtE = (gcnew System::Windows::Forms::TextBox());
        this->txtD = (gcnew System::Windows::Forms::TextBox());
        this->txtKlartext = (gcnew System::Windows::Forms::TextBox());
        this->txtGeheimtext = (gcnew System::Windows::Forms::TextBox());
        this->cmdZiffern = (gcnew System::Windows::Forms::Button());
        this->cmdEntziffern = (gcnew System::Windows::Forms::Button());
        this->cmdprimecheckP = (gcnew System::Windows::Forms::Button());
        this->button2 = (gcnew System::Windows::Forms::Button());
        this->btValidE = (gcnew System::Windows::Forms::Button());
        this->SuspendLayout();
        //
        // lblP
        //
        this->lblP->AutoSize = true;
        this->lblP->Location = System::Drawing::Point(52, 35);
        this->lblP->Name = L"lblP";
        this->lblP->Size = System::Drawing::Size(96, 17);
        this->lblP->TabIndex = 0;
        this->lblP->Text = L"1. Primzahl \'p\'";
        this->lblP->Click += gcnew System::EventHandler(this, &RSA1::label1_Click);
        //
        // lblQ
        //
        this->lblQ->AutoSize = true;
        this->lblQ->Location = System::Drawing::Point(52, 97);
        this->lblQ->Name = L"lblQ";
        this->lblQ->Size = System::Drawing::Size(92, 17);
        this->lblQ->TabIndex = 1;
        this->lblQ->Text = L"2.Primzahl \'q\'";
        //
        // txtP
        //
        this->txtP->Location = System::Drawing::Point(154, 30);
        this->txtP->Name = L"txtP";
        this->txtP->Size = System::Drawing::Size(100, 22);
        this->txtP->TabIndex = 2;
        //
        // txtQ
        //
        this->txtQ->Location = System::Drawing::Point(162, 92);
        this->txtQ->Name = L"txtQ";
        this->txtQ->Size = System::Drawing::Size(100, 22);
        this->txtQ->TabIndex = 3;
        //
        // lblN
        //
        this->lblN->AutoSize = true;
        this->lblN->Location = System::Drawing::Point(380, 37);
        this->lblN->Name = L"lblN";
        this->lblN->Size = System::Drawing::Size(53, 17);
        this->lblN->TabIndex = 4;
        this->lblN->Text = L"n = p*q";
        //
        // lblZ
        //
        this->lblZ->AutoSize = true;
        this->lblZ->Location = System::Drawing::Point(380, 68);
        this->lblZ->Name = L"lblZ";
        this->lblZ->Size = System::Drawing::Size(89, 17);
        this->lblZ->TabIndex = 5;
        this->lblZ->Text = L"z =(p-1)(q-1)";
        //
        // lblKlartext
        //
        this->lblKlartext->AutoSize = true;
        this->lblKlartext->Location = System::Drawing::Point(52, 263);
        this->lblKlartext->Name = L"lblKlartext";
        this->lblKlartext->Size = System::Drawing::Size(55, 17);
        this->lblKlartext->TabIndex = 6;
        this->lblKlartext->Text = L"Klartext";
        this->lblKlartext->Click += gcnew System::EventHandler(this, &RSA1::lblKlartext_Click);
        //
        // lblGeheimtext
        //
        this->lblGeheimtext->AutoSize = true;
        this->lblGeheimtext->Location = System::Drawing::Point(52, 365);
        this->lblGeheimtext->Name = L"lblGeheimtext";
        this->lblGeheimtext->Size = System::Drawing::Size(83, 17);
        this->lblGeheimtext->TabIndex = 7;
        this->lblGeheimtext->Text = L"Geheimtext ";
        this->lblGeheimtext->Click += gcnew System::EventHandler(this, &RSA1::label1_Click_1);
        //
        // lblPrivatschlüssel
        //
        this->lblPrivatschlüssel->AutoSize = true;
        this->lblPrivatschlüssel->Location = System::Drawing::Point(52, 184);
        this->lblPrivatschlüssel->Name = L"lblPrivatschlüssel";
        this->lblPrivatschlüssel->Size = System::Drawing::Size(120, 17);
        this->lblPrivatschlüssel->TabIndex = 8;
        this->lblPrivatschlüssel->Text = L"Privatschlüssel \'d\'";
        this->lblPrivatschlüssel->Click += gcnew System::EventHandler(this, &RSA1::lblPrivatschlüssel_Click);
        //
        // lblÖfflSchlusselE
        //
        this->lblÖfflSchlusselE->AutoSize = true;
        this->lblÖfflSchlusselE->Location = System::Drawing::Point(52, 141);
        this->lblÖfflSchlusselE->Name = L"lblÖfflSchlusselE";
        this->lblÖfflSchlusselE->Size = System::Drawing::Size(163, 17);
        this->lblÖfflSchlusselE->TabIndex = 9;
        this->lblÖfflSchlusselE->Text = L"Öffentlicher Schlüssel \'e\'";
        this->lblÖfflSchlusselE->Click += gcnew System::EventHandler(this, &RSA1::lblÖfflSchlussel_Click);
        //
        // txtN
        //
        this->txtN->Location = System::Drawing::Point(474, 32);
        this->txtN->Name = L"txtN";
        this->txtN->Size = System::Drawing::Size(243, 22);
        this->txtN->TabIndex = 10;
        this->txtN->TextChanged += gcnew System::EventHandler(this, &RSA1::txtN_TextChanged);
        //
        // txtZ
        //
        this->txtZ->Location = System::Drawing::Point(475, 69);
        this->txtZ->Name = L"txtZ";
        this->txtZ->Size = System::Drawing::Size(242, 22);
        this->txtZ->TabIndex = 11;
        //
        // txtE
        //
        this->txtE->Location = System::Drawing::Point(217, 136);
        this->txtE->Name = L"txtE";
        this->txtE->Size = System::Drawing::Size(363, 22);
        this->txtE->TabIndex = 12;
        //
        // txtD
        //
        this->txtD->Location = System::Drawing::Point(217, 179);
        this->txtD->Name = L"txtD";
        this->txtD->Size = System::Drawing::Size(363, 22);
        this->txtD->TabIndex = 13;
        this->txtD->TextChanged += gcnew System::EventHandler(this, &RSA1::textBox6_TextChanged);
        //
        // txtKlartext
        //
        this->txtKlartext->Location = System::Drawing::Point(154, 263);
        this->txtKlartext->Name = L"txtKlartext";
        this->txtKlartext->Size = System::Drawing::Size(543, 22);
        this->txtKlartext->TabIndex = 14;
        this->txtKlartext->TextChanged += gcnew System::EventHandler(this, &RSA1::textBox7_TextChanged);
        //
        // txtGeheimtext
        //
        this->txtGeheimtext->Location = System::Drawing::Point(154, 365);
        this->txtGeheimtext->Name = L"txtGeheimtext";
        this->txtGeheimtext->Size = System::Drawing::Size(543, 22);
        this->txtGeheimtext->TabIndex = 15;
        this->txtGeheimtext->TextChanged += gcnew System::EventHandler(this, &RSA1::textBox8_TextChanged);
        //
        // cmdZiffern
        //
        this->cmdZiffern->Location = System::Drawing::Point(763, 263);
        this->cmdZiffern->Name = L"cmdZiffern";
        this->cmdZiffern->Size = System::Drawing::Size(103, 23);
        this->cmdZiffern->TabIndex = 16;
        this->cmdZiffern->Text = L"Ziffern";
        this->cmdZiffern->UseVisualStyleBackColor = true;
        //
        // cmdEntziffern
        //
        this->cmdEntziffern->Location = System::Drawing::Point(763, 359);
        this->cmdEntziffern->Name = L"cmdEntziffern";
        this->cmdEntziffern->Size = System::Drawing::Size(103, 23);
        this->cmdEntziffern->TabIndex = 17;
        this->cmdEntziffern->Text = L"Entziffern";
        this->cmdEntziffern->UseVisualStyleBackColor = true;
        //
        // cmdprimecheckP
        //
        this->cmdprimecheckP->Location = System::Drawing::Point(268, 12);
        this->cmdprimecheckP->Name = L"cmdprimecheckP";
        this->cmdprimecheckP->Size = System::Drawing::Size(75, 42);
        this->cmdprimecheckP->TabIndex = 18;
        this->cmdprimecheckP->Text = L"prime check p";
        this->cmdprimecheckP->UseVisualStyleBackColor = true;
        this->cmdprimecheckP->Click += gcnew System::EventHandler(this, &RSA1::button1_Click);
        //
        // button2
        //
        this->button2->Location = System::Drawing::Point(268, 68);
        this->button2->Name = L"button2";
        this->button2->Size = System::Drawing::Size(75, 46);
        this->button2->TabIndex = 19;
        this->button2->Text = L"prime check q";
        this->button2->UseVisualStyleBackColor = true;
        //
        // btValidE
        //
        this->btValidE->Location = System::Drawing::Point(642, 111);
        this->btValidE->Name = L"btValidE";
        this->btValidE->Size = System::Drawing::Size(144, 47);
        this->btValidE->TabIndex = 20;
        this->btValidE->Text = L"validity check e";
        this->btValidE->UseVisualStyleBackColor = true;
        //
        // RSA1
        //
        this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(887, 447);
        this->Controls->Add(this->btValidE);
        this->Controls->Add(this->button2);
        this->Controls->Add(this->cmdprimecheckP);
        this->Controls->Add(this->cmdEntziffern);
        this->Controls->Add(this->cmdZiffern);
        this->Controls->Add(this->txtGeheimtext);
        this->Controls->Add(this->txtKlartext);
        this->Controls->Add(this->txtD);
        this->Controls->Add(this->txtE);
        this->Controls->Add(this->txtZ);
        this->Controls->Add(this->txtN);
        this->Controls->Add(this->lblÖfflSchlusselE);
        this->Controls->Add(this->lblPrivatschlüssel);
        this->Controls->Add(this->lblGeheimtext);
        this->Controls->Add(this->lblKlartext);
        this->Controls->Add(this->lblZ);
        this->Controls->Add(this->lblN);
        this->Controls->Add(this->txtQ);
        this->Controls->Add(this->txtP);
        this->Controls->Add(this->lblQ);
        this->Controls->Add(this->lblP);
        this->Name = L"RSA1";
        this->Text = L"RSA1";
        this->Load += gcnew System::EventHandler(this, &RSA1::RSA1_Load);
        this->ResumeLayout(false);
        this->PerformLayout();

     }
#pragma endregion
  private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
         }
  private: System::Void RSA1_Load(System::Object^  sender, System::EventArgs^  e) {
         }
private: System::Void label1_Click_1(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void lblPrivatschlüssel_Click(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void lblÖfflSchlussel_Click(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void textBox6_TextChanged(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void textBox7_TextChanged(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void textBox8_TextChanged(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void lblKlartext_Click(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void txtN_TextChanged(System::Object^  sender, System::EventArgs^  e) {
      }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
};
}

[code]

I would be very much obliged, if you could take a look at it. the prog code should look familiar.


I appreciate your time and ...
Thanks in advance

akah

Answer
Hello akah

Please send me your visual studio project files to xxxxxxxxxxxxxxxxxx and I'll try to look at it in the next couple of days. Today and tomorrow, my days are long, but I'll try. It's best if you do a clean of your project, then compress the whole folder and send it.

Best regards
Zlatko
    Questioner's Rating
    Rating(1-10)Knowledgeability = 10Clarity of Response = 10Politeness = 10
    CommentThanks a million. The video was very helpful. motivated to do the rest. cant thank you enough. i owe you one


  • Add to this Answer
  • Ask a Question

Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.