Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal, estou conseguindo criar meu próprio código desta Cifra, porém não estou conseguindo fazer o C++ escolher qual das opções utilizar na criptografia.
Código Abaixo FUNCIONANDO, porém, sem montar a String Criptografada:
/*
* Projeto: Código de Vigenère
* Por: Bruno Alano Medina
* Licença: GPLv3
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const char tabela[26][26] = {
{ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' },
{ 'B','C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A' },
{ 'C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B' },
{ 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C' },
{ 'E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D' },
{ 'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E' },
{ 'G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F' },
{ 'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G' },
{ 'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H' },
{ 'J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I' },
{ 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J' },
{ 'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K' },
{ 'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L' },
{ 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M' },
{ 'O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N' },
{ 'P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O' },
{ 'Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P' },
{ 'R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q' },
{ 'S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R' },
{ 'T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S' },
{ 'U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T' },
{ 'V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U' },
{ 'W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V' },
{ 'X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W' },
{ 'Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X' },
{ 'Z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y' }
};
int main() {
char string[80];
char criptografada[80];
int subir = 5;
int lado = 3;
cout << "String a Ser Criptografada: ";
gets(string);
cout << "\nAguarde, analisando...";
for (int i = 0; string[i]; i++) {
cout << "PARA O CARACTER: " << string[i];
for (int y = 0; y < 26; y++) {
for (int x = 0; x < 26; x++) {
if (string[i] == tabela[y][x]) {
cout << "\nEncontrei: " << y << " / " << x << "\n";
}
}
}
cout << "\n============================\n";
}
cout << "\n";
return 0;
}
Grato, Bruno Alano.
Tipo, eu realmente não tinha visto como funcionava esta cifra. Só tinha algumas noções básicas, agora descobri que da para fazer sem utilizar a Array Bidimensional.
Obrigado Lord Evil, sempre me ajudando... Vlw
Dá pra fazer com matemática pura.A pouco tempo atrás fiz uma versão simples em C++.
Código:
#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;
}
cin.sync();
cin.get();
return 0;
}>
Dá pra fazer com matemática pura.A pouco tempo atrás fiz uma versão simples em C++.
Código:
#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;
}
cin.sync();
cin.get();
return 0;
}
Nesta parte:
int getFactor(char c)
{
int ret = 0;
if(c >= 'A' && c <= 'Z')
ret = 65;
else if(c >= 'a' && c <= 'z')
ret = 97;
return ret;
}
Poderia me dizer, pq ret=65?
Por que códigos tão extensos?
Tbm fiz um código apenas matemático, sem a necessidade da tabela armazenada em matriz.
#include<stdio.h>
#include<string.h>
#define MAX 200
int main()
{
//variaveis de entrada
char texto_claro[MAX];
printf("\n Escreva o texto claro: ");
gets(texto_claro);
strlwr(texto_claro);
char chave[MAX];
printf("\n Escreva a chave: ");
gets(chave);
strlwr(chave);
//variaveis internas do programa
//contadores
int i,j,
//medidas de tamanho
m = strlen(texto_claro),
m2 = strlen(chave),n,
//ids, são inteiros que correspondem aos valores ascii dos chars
id_texto, id_chave, id_texto_cifrado, id_texto_descifrado;
//variaveis de saida
char texto_cifrado[m],texto_descifrado[m];
//Criptografando
printf("\n Frase cifrada: \n");
for(i = 0,j=0; i < m; i++){
if (texto_claro*!= ' '){*
*
id_texto = texto_claro** - 'a'; //-'a' -> subtrai 97 do range de contagem das letras, que vai de 0 a 25 ao invés de 97 a 122 do ascii*
*
id_chave = chave[j % m2] - 'a';// mesmo processo*
*
id_texto_cifrado = ((id_texto + id_chave )%26) + 'a'; //volta o id calculado ao range normal da tabela ascii*
*
texto_cifrado** = id_texto_cifrado;*
*
j++;*
*
}else texto_cifrado = texto_claro; //copia os espaços*
*
}*
*
puts(texto_cifrado);
*
*
**
//Descriptografando*
*
printf("\n Frase descifrada: \n");*
*
for(i = 0,j=0; i < m; i++){*
*
if (texto_cifrado**!= ' '){*
*
id_texto = texto_cifrado** - 'a'; //-'a' -> subtrai 97 do range de contagem das letras, que vai de 0 a 25 ao invés de 97 a 122 do ascii*
*
id_chave = chave[j % m2] - 'a';// mesmo processo*
*
if(id_texto >= id_chave)*
*
id_texto_descifrado = ((id_texto - id_chave)) + 'a'; //subtrai o valor da chave do valor do texto, e soma 'a' para voltar o id calculado ao range normal da tabela ascii*
*
else id_texto_descifrado = 'z' + ((id_texto - id_chave)) + 1; //a subtração da linha acima passou do a, e o valor da negativo, então ele volta do z pra trás e soma 1 pq o valor 0 tem q ser contado*
*
texto_descifrado** = id_texto_descifrado;*
*
j++;*
*
}else texto_descifrado = texto_cifrado; //copia os espaços*
*
}*
*
puts(texto_descifrado);*
*
return 0;*
*
}*
*
*
Olá!
O problema é que você está implementando a cifra errado.
A cifra é feita assim: você insere uma string e uma chave para codificá-la (o que está faltando).
Aí vce pega essa imagem:
/applications/core/interface/imageproxy/imageproxy.php?img=http://upload.wikimedia.org/wikipedia/commons/c/c7/Vigenere-square.png&key=d0b017d92d170753370afc99c502aefe249de2178b3d905446ac6f2c224fbe09" alt="Vigenere-square.png" />
Thx to Wikipedia
E põe a primeira letra da string em cima, enquanto põe a primeira letra da chave na esquerda, e vai fazendo isso até acabar a string. Se por acaso a chave for menor que a string, se repete a chave.
Abraços :D