Doubt in a simple c++ ENCRYPTION program

Asked

Viewed 1,633 times

2

I have a question about implementing 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, makes the calculations and returns the new value (encryption) of the letter "A", which in this case goes to "Ç", and at the time of decrypting the value returns to "A".

If we ever 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 at the time of decrypt until 1 moment before the FOR loop of the decryption function the value of 'message' = "is" which is what we want, but when entering the loop the value assumes another value (negative) and ends up resulting in another character of the ASCII table, and by checking the value of 'message[i]' = (negative value). of course why if message[i] assumed a negative value whatever the 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 already checked the values of the ASCII table to see if it was extrapolating when doing the multiplication, but not the case.

2 answers

1

This "encryption" algorithm presented is Cifra de César, and is one of the simplest and most well-known encryption techniques in existence.

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

The cipher of the letter x using a displacement n can be written mathematically as:

inserir a descrição da imagem aqui

Deciphering is done in a similar way:

inserir a descrição da imagem aqui

To n=1, the replacement table would be something like:

inserir a descrição da imagem aqui

Where the text encryption OLA MUNDO! would be:

PMB NVOEP!

It follows an implementation in C++ able to encrypt and decrypt a string using 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;
}

Exit (chave=13):

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!

Exit (chave=7):

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!

Exit (chave=1):

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!

0

The problem reported happens because the type "char" is being considered as "Signed char", which is normal, but when a variable "Signed char" is converted to int the value may be negative if the character Codepoint is greater than 127.

To solve this problem, by converting from "char" to "int" you can use an explicit cast to "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';
}  

Exit:

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

D:\projects\misc
$

Browser other questions tagged

You are not signed in. Login or sign up in order to post.