sinceramente non ho provato ancora perchè nel frattempo abbiamo analizzato un'altra strada. comunque per rendere forse il tutto ancora più chiaro, ti invio il codice completo cosi si capisce meglio il tutto. l'applicazione implementa l'algoritmo di vigenere utilizzando l'alfabeto italiano: legge quindi un file di testo e carattere per carattere effettua la cifratura. le lettere cifrate verranno scritte su un altro file di testo. non potendo implementare l'algoritmo nel modo classico perchè in questo caso l'alfabeto è quello italiano, ho pensato di fare in questo modo: ho creato un array ALPH con tutti i caratteri dell'alfabeto italiano
codice:
char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};
dopo di che cerco la corrispondenza della lettera della chiave e della corrispondente lettera da cifrare in ALPH in modo da ottenere gli indici, in caso affermativo. Dopo di che calcolo lettera per lettera la cifratura. per esempio:
CHIAVE: ROBERTO
TESTO: PROVA
codice:
key[0] = R = ALPH[15] --->index_key = 15;
text[0] = P = ALPH[13] --->index_text = 13;
letteracodificata[0] = ALPH[(15+13)%21] = ALPH[7] = H
il codice completo è:
codice:
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Vigenere
{
public:
string key;
vector<string> text;
Vigenere(string str){
key = str;
key = StringToUpper(key);
if(!key.empty()){
key = EraseChar(key);
}
return;
}
string StringToUpper(string convstr){
for(int i = 0; i < convstr.length(); i++){
convstr[i] = toupper(convstr[i]);
}
return convstr;
}
//load the content of a file into t
void Vigenere::LoadText(string filename){
ifstream f;
string line;
f.open(filename.c_str());
if(!f.is_open()){
cout << "Error opening file: " << filename << " !" << endl;
return;
}
while(getline(f,line)){
line = StringToUpper(line);
if(!line.empty()){
line = EraseChar(line);
text.push_back(line);
}
}
f.close();
}
//deletes all characters not in ALPH
string EraseChar(string str){
char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};
str = StringToUpper(str);
for(int i = 0; i < str.length(); i++){
bool inAlph = false;
for(int j = 0; j < 21; j++){
if(str[i] == ALPH[j]){
inAlph = true;
break; // don't need to keep checking against the rest of ALPH
}
}
if (!inAlph) {
str.erase(i,1);
i--;
}
}
return str;
}
void encode(string plaintext, string encoded_f){
ofstream f;
char ALPH[21] = {'A','B','C','D','E','F','G','H','I','L','M','N','O','P','Q','R','S','T','U','V','Z'};
TextErase();
LoadText(plaintext);
f.open(encoded_f.c_str());
if(!f.is_open()){
cout << "Error creating file: " << encoded_f << " !" << endl;
return;
}
int index_k[key.length()]; //key indexs array
for(int i = 0; i < key.length(); i++){
for(int j = 0; j < 21; j++){
if(key[i] == ALPH[j])
index_k[i] = j;
}
}
size_t* index_t = new size_t[text.size()]; //text indexs
for (size_t i = 0; i < text.size(); ++i){
size_t pos = text[i].find_first_of(ALPH);
if (pos != std::string::npos){
index_t[i] = pos;
}
char ch = ALPH[(index_k[i%key.length()] + index_t[i])%21];
f << ch;
}
f << "\n";
f.close();
return;
}
void TextErase(){
text.clear();
}
};
int main()
{
string key, encoded, plaintext, decoded;
cout << "Key: ";
cin >> key;
Vigenere cipher(key);
cout << "Input file: ";
cin >> plaintext;
cout << "Output file: ";
cin >> encoded;
cipher.encode(plaintext,encoded);
}
aprendo un file con tre righe di testo, mi restituisce un file con solo tre lettere. come posso risolvere? ormai credo di non riuscire più nemmeno a pensare, sono incollato qui da più di 5 ore