domingo, 27 de noviembre de 2011

Encryption/Decryption

This is a program we made for encrypt/decrypt some text.

In this code, first we declare the functions Encrypt() and Decrypt(), then we ask to the user to write something and we save this text in a string variable called text, then the Encrypt method is called. In this function, the variable len gets the text length and then is a for for 0 to the lenght of the text -1 this is to read each letter of the text that user write. Then we get the ASCII value of 'c', multiply this ASCII value by 2 and set the new ASCII value back into the char. Finally insert the new value back into the text. Decrypt function returns the encrypted text into the start text.


#include <iostream>
#include <cstring>
#include <cctype>


using namespace std;
void Encrypt(string&);
string Decrypt(string text);


int main() {
 

  string text;
  cout << "Write something: ";
  getline(cin,text);
  string temp(text);
  Encrypt(text);


  cout << "Encrypted: " << text << endl;
  cout << "Decrypted: " << Decrypt(text) << endl;


  return 0;
}


void Encrypt(string &text)
{
  int len = text.length();
  char c;
  string Finaltext(text);


  for (int i = 0; i <= (len-1); i++)
    {
      c = text.at(i);
      int a = (int)c;
      a += 2;
      if (a > 254) { a = 254; }
      c = (char)a;
      Finaltext.insert(i , 1, c);
    }
  string textEncrypted(Finaltext, 0, len);
  text = textEncrypted;
}


string Decrypt(string text)<
{

  int len = text.length();
  char c;
  string Finaltext(text);


  for (int i = 0; i <= (len-1); i++)
    {
      c = text.at(i);
      int a = (int)c;
      a -= 2;


      c = (char)a;
      Finaltext.insert(i, 1, c);
    }
  string textDecrypted(Finaltext, 0, len);
  return textDecrypted;
} 
Execution:


2 comentarios:

  1. Esto ya lo tengo calificado como parte de lo que mostraron en la presentación.

    ResponderEliminar
  2. I executed the code on my system and its working perfectly fine. I am so happy to have this code as I can use it in my application. Thank you very much for this great share.
    eSignature

    ResponderEliminar