Problemas com algoritmo AES no C++ [RESOLVIDO]

1. Problemas com algoritmo AES no C++ [RESOLVIDO]

Adriano Andrei Goede
adrianogoede

(usa Debian)

Enviado em 30/04/2021 - 10:19h

Boa noite pessoal,

Hoje decidi tentar implementar o algoritmo criptográfico AES em C++ com o puro intuito de praticar minhas habilidades com a linguagem, e por enquanto estou trabalhando no Key Schedule do mesmo. Segue abaixo o esboço da classe que estou tentando implementar:

class AES_Key
{
private:
    // Containers:
    unsigned char Key[16];
    std::vector<std::vector<unsigned char>> RoundKeys{};
    // Auxiliary Functions:
    void RotWord(std::vector<unsigned char>& word) {
        unsigned char temp{word.at(0)};
        word.at(0) = word.at(3);
        word.at(3) = temp;
        for (unsigned short i{0}; i <= 1; i++) {
            temp = word.at(i);
            word.at(i) = word.at(i+1);
            word.at(i+1) = temp;
        }
    }
    void SubWord(std::vector<unsigned char>& word) {
        for (unsigned short i{0}; i < 4; i++)
            word.at(i) = AES_SubTable[word.at(i)];
    }
    static unsigned short Rcon(unsigned short Round) {
        Round /= 4;
        switch (Round) {
            case 1: return 0x1;
            case 2: return 0x2;
            case 3: return 0x4;
            case 4: return 0x8;
            case 5: return 0x10;
            case 6: return 0x20;
            case 7: return 0x40;
            case 8: return 0x80;
            case 9: return 0x1B;
            case 10: return 0x36;
            default: throw 0;
        }
    }
    // Functions:
    void ExpandKey() {
        // Generate the first 4 words:
        std::vector<std::vector<unsigned char>> words{};
        for (unsigned short i{0}; i < 4; i++) {
            words.push_back(std::vector<unsigned char>{});
            words.back().push_back(this->Key[4*i]);
            words.back().push_back(this->Key[4*i+1]);
            words.back().push_back(this->Key[4*i+2]);
            words.back().push_back(this->Key[4*i+3]);
        }
        // Generate the next 40 words:
        std::vector<unsigned char>* temp{nullptr};
        for (unsigned short i{4}; i < 44; i++) {
            temp = &words.at(i-1);
            if ((i % 4) == 0) {
                RotWord(*temp);
                SubWord(*temp);
                temp->at(0) ^= Rcon(i);
            }
            words.push_back(std::vector<unsigned char>{});
            std::vector<unsigned char>* xorword{&words[i-4]};
            for (unsigned short j{0}; j < 4; j++) {
                words.back().push_back(xorword->at(j) ^ temp->at(j));
            }
        }
        // Construct Round Keys:

    }
public:
    // Constructors:
    AES_Key(const char* key) {     ///// Padding not implemented yet /////
        for (unsigned short i{0}; i < 16; i++)
            this->Key[i] = (unsigned char)key[i];
        this->ExpandKey();
    }
    // Getters:
    unsigned char* InitialKey() {return this->Key;}
    std::vector<unsigned char> RoundKey(unsigned short Round) {return this->RoundKeys[Round];}
};  


Pois bem, o erro da imagem em anexo insiste em me assombrar, não consigo me livrar dele. Alguém pode me ajudar?

Agradeço desde já! E peço desculpas caso o código esteja muito confuso e difícil de ler, confesso que codei meio que na pressa :P


  


2. MELHOR RESPOSTA

leandro peçanha scardua
leandropscardua

(usa Ubuntu)

Enviado em 30/04/2021 - 14:00h


Vc está referenciando um array além do tamanho dele. Vc pode usar o eletric fence p ajudar a corrigir esse tipo de erro
https://stackoverflow.com/questions/9494915/using-electric-fence-in-a-c-program





Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts