Doubt in a simple CRYPTOGRAPHY program in c ++

2

I have a question to implement an encryption code. Follow the code below:

#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    string criptografar(string mensagem);
    string descriptografar(string mensagem);
    string mens_original;
    string mens_criptografada;
    string mens_descriptografada;
    mens_original = "A";

    cout<< "Mensagem original: "<< mens_original << endl;

    mens_criptografada = criptografar(mens_original);
    cout << "Mensagem criptografada: " << mens_criptografada << endl;

    mens_descriptografada = descriptografar(mens_criptografada);
    cout << "Mensagem descriptografada: " << mens_descriptografada << endl;

    return 0;
}

string criptografar(string mensagem)
{
    int tamanho = 0;
    tamanho = mensagem.length();

    for (int i = 0 ; i < tamanho; i++)
    {

        mensagem[i] = (int) mensagem[i] * 2 ;

    }



    return mensagem;
}

string descriptografar(string mensagem)
{
    int tamanho = 0;
    tamanho = mensagem.length();


    for (int i = 0 ; i < tamanho ; i++)
    {

        mensagem[i] = (int) mensagem [i] / 2;


    }


    return mensagem;
}

Well, when we put the following:

    mensagem[i] = (int) mensagem[i] + 2 //criptografar;


    mensagem[i] = (int) mensagem[i] - 2 // descriptografar;

The program runs, calculates and returns the new value (encryption) of the letter "A", which in the case goes to "Ç", and at the time of decrypting the value returns to "A". >

If we do:

    mensagem[i] = (int) mensagem[i] * 2 //criptografar;


    mensagem[i] = (int) mensagem[i] / 2 // descriptografar;

The value of "A" goes to "is" normal (2 * 65 = 130 -> value of "is") but in the decrypt up to 1 moment before the FOR loop of the function decrypts the value of 'message' = 'is' which is what we want, but when entering into the loop the value takes on another value (negative) and ends up resulting in another character of the ASCII table, and checking the value of 'message [i]' = (negative value). of course why if message [i] took a negative value any result, in the end it would have to be negative. My question is: Why is this happening? If you have solutions please help me! NOTE: I have already checked the ASCII table values to see if you were extrapolating when multiplying, but that is not the case.

    
asked by anonymous 31.08.2017 / 18:56

2 answers

1

This "cryptography" algorithm presented here is about the Cifra de César , and is one of the most simple and well-known cryptographic techniques.

It is a type of substitution cipher in which each letter of the text is replaced by another one.

The encryption of the letter x using a n offset can be written mathematically as:

Thedecryptionisdoneinasimilarway:

Forn=1,thereplacementtablewouldlooksomethinglike:

WheretheencryptionofthetextOLAMUNDO!wouldbe:

PMBNVOEP!

FollowingisanimplementationwithC++abletoencryptanddecryptastringusingthe Cifra de César . :

#include <string>
#include <iostream>

using namespace std;

char converter(char ch, int chave)
{
    if (!isalpha(ch)) return ch;
    char offset = isupper(ch) ? 'A' : 'a';
    return (char)((((ch + chave) - offset) % 26) + offset);
}

string criptografar( string entrada, int chave )
{
    string saida = "";
    size_t len = entrada.size();
    for( size_t i = 0; i < len; i++ )
        saida += converter( entrada[i], chave );
    return saida;
}

string decriptografar( string entrada, int chave )
{
    return criptografar( entrada, 26 - chave );
}

int main( void )
{
    int chave = 13;

    string txt = "O rato roeu a roupa do rei de Roma!";
    cout << "Texto Original: " << txt << endl;

    string cripto = criptografar( txt, chave );
    cout << "Texto Cifrado: " << cripto << endl;

    string decripto = decriptografar( cripto, chave );
    cout << "Texto Decifrado: " << decripto << endl;

    return 0;
}

Output (% with%):

Texto Original: O rato roeu a roupa do rei de Roma!
Texto Cifrado: B engb ebrh n ebhcn qb erv qr Ebzn!
Texto Decifrado: O rato roeu a roupa do rei de Roma!

Output (% with%):

Texto Original: O rato roeu a roupa do rei de Roma!
Texto Cifrado: V yhav yvlb h yvbwh kv ylp kl Yvth!
Texto Decifrado: O rato roeu a roupa do rei de Roma!

Output (% with%):

Texto Original: O rato roeu a roupa do rei de Roma!
Texto Cifrado: P sbup spfv b spvqb ep sfj ef Spnb!
Texto Decifrado: O rato roeu a roupa do rei de Roma!
    
05.07.2018 / 03:22
0

The reported problem occurs because the char type is being considered a signed char, which is normal, but when a signed char variable is converted to int, the value can be negative if the character is greater than 127.

To solve this problem, when converting from "char" to "int" you can use an explicit cast for "unsigned char".

Example

#include <iostream>
#include <string>
using namespace std;

int main()
{
  string x = "é";

  cout << "* char: " << int(x[0]) << '\n';
  cout << "* unsigned char: " << int((unsigned char)(x[0])) << '\n';
}  

Output:

D:\projects\misc
$ teste.exe
* char: -23
* unsigned char: 233

D:\projects\misc
$
    
25.10.2018 / 21:17