1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| QString EmFunctions::crypt(bool const &way, const int &arg1, const QString &arg2)
{
QString tmpStr(arg2); // Stockage de la phrase à chiffrer dans une variable temporaire
int decalage(arg1); //Stockage du nombre de lettres à décaler dans la fonction
//Choix d'utilisation de la fonction way = chiffrement, !way = déchiffrement.
if(!way)
decalage -= (2*decalage);
/* Traitement de la chaine reçue en argument et passée dans la variable tmpStr
* - Suppression des espaces et ponctuations.
* - Remplacement des accents.
* - Passage en majuscule.
*/
tmpStr.remove(QRegExp("\\W")); //Suppression des ponctuations et symboles
tmpStr.replace(QRegExp("[éèëêËÊ]"),"e"); // Remplacement des accents
tmpStr.replace(QRegExp("[àâäÄÂ]"), "a");
tmpStr.replace(QRegExp("[iïîÏÎ]"), "i");
tmpStr.replace(QRegExp("[ùüÜûÛ]"), "u");
tmpStr.replace(QRegExp("[ôöÔÖ]"), "o");
tmpStr.replace(QRegExp("[ç]"),"c");
tmpStr = tmpStr.toUpper();
// initialisation de variables temporaires
QString alphabetString = QObject::tr("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
int strLenght = tmpStr.size();
int value;
int alphabetLenght = alphabetString.size();
//traitement du chiffrage avec des QVectors
QVector<QChar> tmpIn(strLenght);
QVector<QChar> tmpOut(strLenght);
QVector<QChar> alphabetVector(alphabetLenght);
QVector<int> tmpElements(strLenght);
//Tranformation du QSTring tmpStr en QVector
for (int i(0); i<strLenght; i++)
tmpIn.insert(i, tmpStr.at(i));
//Transformation de alphabet en QVector
for(int i(0); i<alphabetLenght ; i++)
alphabetVector.insert(i, alphabetString.at(i));
//Boucle de décalage. On parcourt la chaine à chiffer et on enregistre une 'value' pour chaque élément
for (int i(0); i<strLenght; i++)
{
for (int j(0); j<alphabetLenght; j++)
{
if(tmpIn.at(i) == alphabetVector.at(j))
{
value = j + decalage;
if(value >= (alphabetLenght-value))
tmpElements.insert(i, (value %alphabetLenght));
else
tmpElements.insert(i, value);
}
}
}
//Pour chaque valeur du tableau éléments, on enregistre le caractère correspondant dans tmpOut
for (int i(0); i<strLenght; i++)
{
for(int j(0); j<alphabetLenght; j++)
{
if(tmpElements.at(i) == j)
tmpOut.insert(i, alphabetVector.at(j));
}
}
//Conversion du QVector en Qstring
QString tmpStr2;
for(int i(0); i<strLenght; i++)
tmpStr2 += tmpOut.at(i);
return tmpStr2;
} |
Partager