Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Recently, i wrote an algorithm who encrypt an word into another one with the vigenere cypher and i want to know if it's optimal.
Here it's the code
C++
#include <iostream>
#include <string>
#include <vector>

using namespace std;

char encodeur( char premiereLettre, char deuxiemeLettre)
{

    int premiereChiffreLettre;
    int deuxiemeChiffreLettre;

    int lettreFiniChiffre;
    char lettreFini;

    int difference;

    premiereChiffreLettre = int(premiereLettre);
    deuxiemeChiffreLettre = int(deuxiemeLettre);

    lettreFiniChiffre = premiereChiffreLettre  + (deuxiemeChiffreLettre - 97);

    if (lettreFiniChiffre > 122)
    {
        difference = lettreFiniChiffre - 123;
        lettreFiniChiffre = 97 + difference;
    }
    else
    {

    }
    lettreFini = char(lettreFiniChiffre);
    return lettreFini;
}

string encodeurMot(string mot, string motEncodeur)
{
    int taille = mot.size();
    int tailleMotEncodant = motEncodeur.size();

    string motEncoder;

    char lettre;
    char lettreEncodant;
    char lettreEncoder;

    vector<char> tableau(taille);
    for(int i = 0, j = 0; i < taille;j++, i++)
    {
        if (j == tailleMotEncodant)
        {
            j = 0;
        }
        else
        {

        }
        lettre = mot[i];
        lettreEncodant = motEncodeur[j];
        lettreEncoder = encodeur(lettre , lettreEncodant);

        tableau[i] = lettreEncoder;
        motEncoder += tableau[i];
    }
    return motEncoder;
}
int main()
{
    string mot;
    string motEncodeur;
    string motEncoder;

    int onOff(1);
    while (onOff == 1)
    {
    cout << "what's the word you want to encrypt ?" << endl;
    cin >> mot;
    cout << "Thank you, now what's the word do you want to use for encryption ?" << endl;
    cin >> motEncodeur;

    motEncoder = encodeurMot(mot, motEncodeur);

    cout << motEncoder << endl;
    cout << "do you want to encrypt another word ?(0 for no, 1 for yes)" << endl;
    cin >> onOff;
    }
    return 0;
}

sorry for the variables's names, i'm french.

What I have tried:

i have tried to make it as optimal as possible.
Posted
Updated 14-Feb-17 15:40pm

1 solution

Quote:
sorry for the variables's names, i'm french.
C'est pas un problème.
Quote:
i want to know if it's optimal.
Non it is not.
In encodeur, you don't need to transform char to int, you can use char directly.
C++
char encodeur( char premiereLettre, char deuxiemeLettre)
{

    int premiereChiffreLettre;
    int deuxiemeChiffreLettre;

    int lettreFiniChiffre;
    char lettreFini;

    int difference;
    char difference;

    premiereChiffreLettre = int(premiereLettre);
    deuxiemeChiffreLettre = int(deuxiemeLettre);

    lettreFiniChiffre = premiereChiffreLettre  + (deuxiemeChiffreLettre - 97);

    if (lettreFiniChiffre > 122)
    {
        difference = lettreFiniChiffre - 123;
        lettreFiniChiffre = 97 + difference;
    }
    else
    {

    }
    lettreFini = char(lettreFiniChiffre);
    return lettreFini;
}

or
C++
char encodeur( char premiereLettre, char deuxiemeLettre)
{
    unsigned char lettreFini;
    unsigned char difference;

    lettreFini = premiereLettre  + (deuxiemeLettre - 97);

    if (lettreFini > 122)
    {
        difference = lettreFini - 123;
        lettreFini = 97 + difference;
    }
    return lettreFini;
}

Voilà pour quelques simplifications.
Tu peux aussi éliminer tableau

Chiffre de Vigenère — Wikipédia[^]
 
Share this answer
 
v4
Comments
Richard MacCutchan 15-Feb-17 3:57am    
Well done, now no o ne can understand the answer either.
Patrice T 15-Feb-17 4:11am    
Corrected some typos.
I know, a lot of things to remove from the function.
By the way, thanks for the comment with the downvote.
Richard MacCutchan 15-Feb-17 4:16am    
Yes, but it is still in French. This is an English language website.
Patrice T 15-Feb-17 4:23am    
I kept the main part is in English. The parts in French do not change the meaning, they are secondary.
Note i corrected a few typos that may help if you want to translate.
theCodingCat 15-Feb-17 6:10am    
Actualy, i have tried your solution but the code don't work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900