Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

  • 0
rogeriorosasilva

Programação C++ criptografia

Pergunta

Galera preciso que o resultado da criptografia e descriptografai seja salva em um arquivo txt

 

 

#include <iostream>
using std::string;
using std::cout;
using std::cin;
using std::endl;

int getFactor(char c)
{
        int ret = 0;
        if(c >= 'A' && c <= 'Z')
            ret = 65;
        else if(c >= 'a' && c <= 'z')
            ret = 97;
        return ret;
}
string vegenereCrypt(string str, string key)
{
        int strLen = str.size();
        int keyLen = key.size();
        for(int x = 0,y = 0;x < strLen;++x,++y) {
            char cur = str[x];
            if(cur == ' ')
                continue;
            int strFactor = getFactor(cur);
            int keyFactor = getFactor(key[y]);
            if(!(strFactor && keyFactor))
                throw;
            int res = cur+key[y]-keyFactor;
            if(res > strFactor+25)
                res -= 26;
            if((x >= keyLen-1) && ((x+1)%keyLen) == 0)
                y = -1;
            str[x] = (char)res;
        }
        return str;
}
string vegenereDecrypt(string str, string key)
{
        int strLen = str.size();
        int keyLen = key.size();
        for(int x = 0,y = 0;x < strLen;++x,++y) {
            char cur = str[x];
            if(cur == ' ')
                continue;
            int strFactor = getFactor(cur);
            int keyFactor = getFactor(key[y]);
        
            if(!(strFactor && keyFactor))
                throw;
            cur = cur-key[y]+keyFactor;
            if(cur < strFactor)
                cur += 26;
            if((x >= keyLen-1) && ((x+1)%keyLen) == 0)
                y = -1;
            str[x] = cur;
        }
        return str;
}
int main()
{
        while(true) {
            unsigned op;
            cout << "Menu:\n"
            << "\t 1 - Codificar texto.\n"
            << "\t 2 - Decodificar texto.\n"
            << "\t 3 - Sair.\n" << endl
            << "Digite a opcao desejada:";
            cin >> op;
            if(op == 3)
                break;
            if(op > 3) {
                cout << "Opcao invalida.\n" << endl;
                continue;
            }
            string str;
            string key;
            cout << "Digite a string:";
            cin >> str;
            cout << "Digite a chave:";
            cin >> key;
            if(op == 1)
                str = "codificado:" + vegenereCrypt(str, key);
            else
                str = "decodificado:" + vegenereDecrypt(str, key);
            cout << "\nTexto " << str << endl << endl;
              
        }
        escreveTxt(string);
        cin.sync();
        cin.get();
        return 0;
}


 

Compartilhar este post


Link para o post
Compartilhar em outros sites

1 resposta a esta questão

Recommended Posts


×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.