c++variablesglobal-variables

variable declaration from inside a function on another cpp file


I have an interest in cryptography and have decided to try and write an AES library. However it has been a long time since I have programmed cpp and I'm a bit rusty. I have this code in my AES.cpp file:

#include "AES.h"
#include <iostream>
#include <cstdint>

void initialize_aes(int Nk){
  int Nr;
  try {
    if(Nk == 4){
      Nr = 10;
    } else if(Nk == 6){
        Nr = 12;
    } else if(Nk == 8){
      Nr = 14;
    } else {
      throw (Nk);
    }
  }
  catch (int a){
    std::cout << "error: '" << a << "' is not an accepted value for key length";
  }
};

and this code in my main.cpp file:

#include "AES.h"
#include <iostream>
#include <cstdint>

int main(){
  uint8_t i[256];
  initialize_aes(6);
  std::cout << Nr;
}

Yes the files are linked properly and there is a AES.h file. I just didn't include that in this post.

What I want is to be able to declare and define the Nr variable then call it in the main.cpp file. I have tried classes and the extern tag but they did not work and I am not sure if I did them right. Ideally I would use a class but I am having problems putting a variable into a function and having the function set that variable as a class. AES.cpp:

class AES {
  public:
    int Nr;
    int Nk;
    int Nb = 4;
};

void initialize_aes(a, int Nk){
  try {
    if(Nk == 4){
      AES a;
      AES_4.Nr = 10;
      AES_4.Nk = 4;
    } else if(Nk == 6){
      AES a;
      AES_6.Nr = 12;
      AES_6.Nk = 6;
    } else if(Nk == 8){
      AES a;
      AES_8.Nr = 14;
      AES_6.Nk = 8;
    } else {
      throw (Nk);
    }
  }
  catch (int a){
    std::cout << "error: '" << a << "' is not an accepted value for key length";
  }
  
};

Main.cpp:

int main(){
  AES aes; //I am not sure what the best format would be to put it in the function
  initialize_aes(aes, 6);
  std::cout << aes.Nr;
}

What I need is to just create a new class object inside the function that can be called anywhere. Do I need to use a different return type? Any suggestions would be much appreciated.


Solution

  • I would recommend not being able to construct AES objects that need to be initialized with a separate function afterwards. Instead, make sure they are all constructed with a valid state.

    Example:

    AES.h

    #pragma once // or a portable header guard
    
    class AES {
    public:
        AES(int Nk);
    
        int m_Nk;
        int m_Nr;
        int m_Nb = 4;
    };
    

    AES.cpp

    #include "AES.h"
    #include <stdexcept>
    #include <string>
    
    AES::AES(int Nk) : m_Nk(Nk), m_Nr(Nk + 6) {
        if (m_Nk < 4 || m_Nk > 8 || (m_Nk % 2)) {
            throw std::runtime_error("Invalid AES key length: " +
                                        std::to_string(m_Nk));
        }
    }
    

    main.cpp

    #include "AES.h"
    #include <iostream>
    #include <stdexcept>
    
    int main(){
        try {
            AES aes(6);
            std::cout << aes.m_Nr << '\n';
    
        } catch (const std::exception& ex) {
            std::cerr << "ERROR: " << ex.what() << '\n';
        }
    }
    

    Demo