Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,080 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




Encrypting/Decrypting

 
Reply to this topicStart new topic

Encrypting/Decrypting, Encrypting from text to integers and decrypting from integers to text

coolgymnast01
20 Apr, 2008 - 12:30 PM
Post #1

New D.I.C Head
*

Joined: 26 Mar, 2008
Posts: 21

I'm trying to encrypt a text message into integers and decrypt from integers to a text message. First, the program asks the user whether they want to encrypt or decrypt. If the user selects encrypt, they are asked to enter a text message such as "A large dog!". The program then outputs an integer of random numbers between 1 and 254, such as "200 169 229 232 251 238 236 169 237 230 238 168." The opposite goes for if the user selects decrypt. This is what I have so far:

CODE
#include<iostream>
#include<cstring>
using namespace std;

void encrypt(char []);

int main ()
{
    char textstring[100];
    char numstring[100];
    char choice;

    cout << "Enter choice: (E)ncryption, (D)ecrpytion" << endl;
    cin >> choice;
    if (choice == 'E')
    {
        cout << "Please enter a line of text: " << endl;
        cin.getline(textstring,100);
        while(textstring != '\0')
        {
            encrypt(textstring);
        }
    }
    else
    {
        cout << "Please enter the encrypted values: " << endl;
        cin.getline(numstring,100);
    }

    return 0;
}

void encrypt(char str[])
{
    int len = strlen(str);
    for(int n=0; n<len; n++)
    {
        if(isspace(str[n]))
        {
            cout << "100 ";
        }
    }
}


I am unsure as to what I should do now. I was told the the exclusive OR (^) operator comes in handy and was even given an example:

CODE
const int KEY = 137;
    char letter = ‘A’;
    int code;
    char newletter;

    code = letter ^ KEY;
    cout << code is: “ << code << endl;
newletter = code ^ KEY;
cout << original char is: “ << newletter << endl;


However, I don't know where to put this and how to expand so that ever letter is like this without doing the long way.

Please help!!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Encrypting/Decrypting
21 Apr, 2008 - 06:57 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
first of all:
QUOTE
The program then outputs an integer of random numbers between 1 and 254, such as "200 169 229 232 251 238 236 169 237 230 238 168."


This is a GCE - Gross Conceptual Error. At least if you actually are trying to encrypt something. The output is not usually "random numbers" -- they may seem random, but they contain information. The only exception to this is a "one time pad".

Now as it turns out the exclusive or operator and the properties of a pseudo-random number generator can simulate a one time pad (although the result is not cryptographically secure).

start by initializing the random number generator (use srand) to a fixed number (or ask the user to input a number --that will be their key). Next, loop over the source message grabbing each character, doing an exclusive-OR with a random number (rand()%256) mod 256. This will produce a very random looking output.

note that when you output your encrypted message you will need to output it as integers (probably in hex) rather than letters since not all of the characters will be displayable.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month