strings

This is a discussion on strings within the C++ Programming forums, part of the General Programming Boards category; bad day, because i just overwrote my program, even though it didn't work. i need help. help with encrypting and ...

  1. #1
    Unregistered
    Guest

    Question strings

    bad day, because i just overwrote my program, even though it didn't work. i need help. help with encrypting and then decrypting strings. I just need enuf to get started, my other program was a little long, sorry i don't anything for yall to look at.

    read in 2 strings from the user a WORD to be encoded and a KEY.
    call a void function Encrypt, which modifies WORD.
    create Encrypt2, which reads in a second key from the user and modifies the changed word even more, (sorta like a super encrypt). This second key must then be returned to the main program.
    create Decrypt which uses both keys to restore the original word.

    *Try shifting WORD by a randomly scrambled key, but keep track of what you do so you can Decrypt WORD.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    a very simple encryption algorithm is to just xor with the key to encrypt and xor with the key again to decrypt.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    say what? xor?

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    exclusive or

    has this truth table....

    a b a^b
    0 0 0
    1 0 1
    0 1 1
    1 1 0
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest
    Let's see if I can work through this with you. Each char has an associated decimal numeric value as stated in the compilers character set. In turn each decimal numeric value has a binary counterpart. The decimal value and binary counterpart are there for you to manipulate at any time you wish. Usually we just store the values for later use but you can do anything you want with them, like apply the binary exclusive or to change the binary value which then changes the decimal value which then changes the char value which effectively causes the original value to be encrypted.

    With exclusive or (usually depicted with as the ^ symbol) you mix two binary values to create a third. Let's say you start with the binary value for the char 'a' which is stored in a variable called char1 and you mix it with the binary value for the char 'b' stored in a varible called key to get a binary value which is stored in a variable called mixed which is then assigned back to the char variable char1. char1 is then effectively encrypted. The neat thing is if you then mix the encrypted version of char1 with the binary value of the variable key using exclusive or and assign the result back to char1, char1 should be decrypted back to the original value.

    We can try this out ourselves to prove it. Let char 1 have binary value 010 and key have binary value 110. The rules of exclusive or say that if the equivalent binary digit of each contributor is the same then the mix is 0 otherwise the mix is 1. Therefore:

    char1 010
    key 110
    mixed 100 //encryted version of char1

    Now reverse process:

    char1 100 //encrypted version of char1
    key 110
    mixed 010 //decrypted version of char1

    Even neater is you don't need to know the binary values you are mixing to do this. You can do something like this:

    char char1 = 'a';
    char key = 'b';
    char mixed;

    mixed = char1 ^ key;
    char1 = mixed;
    //etc.

    and the computer does everything else for you. Cool.

  6. #6
    Unregistered
    Guest
    Hey i have that same program...can you post some code or something?

  7. #7
    Unregistered
    Guest

    Lightbulb

    how's this, please tell me the problems cuz it doesn't really work.



    #include <iostream.h> //used for cout, cin

    void main()
    {
    char *storage; // Declare a pointer
    storage=new char[]; // and allocate space for it

    cout << "Enter: ";
    cin >> storage; //puts the value into storage

    //This is the loop that goest through the storage.
    //Instead of cout << storage[a]<<endl; you can put
    //the code that will make the manipulations. This way
    //every single digit can be manipulated!
    for (int a=0; a<sizeof(storage); a++)
    {
    cout << storage[a]<<endl;
    }

    }

  8. #8
    Unregistered
    Guest
    dang wrong one. OK this is the right one. PLEASE TELL ME WHAT'S WRONG!

    #include <iostream.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>

    //Defining global the variables
    char *word; //the inputed word
    int key1; //key1
    int key2; //key2
    int size; //the size of the word
    char *array; //temporary array
    int *store; //where the crypted word is stored


    //Function that makes the first encryption
    void encrypt_first()
    {
    for(int b=0; b<size; b++)
    {
    store[b]=store[b]+key1;
    }
    }

    //Function that makes the second encryption
    void encrypt_second()
    {
    for(int c=0; c<size; c++)
    {
    store[c]=store[c]-key2;
    }
    }

    //Function that makes the decryption
    void decrypt()
    {
    //first stage of decryption (key2)
    for(int f=0; f<size;f++)
    {
    store[f]=store[f]+key2;
    }
    //second stage of decryption
    for(int g=0; g<size; g++)
    {
    store[g]=store[g]-key1;
    }
    }

    //Main program function
    void main()
    {
    word=new char[size];
    cout <<"Enter a word: ";
    cin >> word;
    size=strlen(word); //determine the size of the word

    //get the word, and put every letter into a cell
    array=new char[size];
    store=new int[size];
    array=word;

    //translates every letter into its ASCII number
    for (int a=0; a<size; a++)
    {
    store[a]=int(array[a]);
    }

    cout << "Enter key1 (a number): ";
    cin>> key1;



    encrypt_first(); //calls the 1st encryption function

    cout <<"Enter key2 (a number): ";
    cin>>key2;



    encrypt_second(); //calls the 2nd encryption function

    //display the cripted word
    cout<<"The crypted word is: ";
    for(int d=0;d<size;d++)
    {
    cout <<char(store[d]);
    }
    cout<<endl;


    cout<<"Enter your decryption keys!:\n";
    cout<<"Key1: ";
    cin >> key1;

    cout <<endl<<"Key2: ";
    cin>> key2;

    //call the decryption function
    decrypt();

    //display the decrypted data
    cout<<"Decrypted data: ";
    for (int e=0; e<size; e++)
    {
    cout<<char(store[e]);
    }
    cout << endl;

    }

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    ok lets step through your code a little... we will start in main where the compiler does...

    void main()

    should be int main(). main() always returns an int.

    word=new char[size];

    ok im the big dumb compiler and i need to allocate size bytes and return a pointer to the memory so I will just look and see how many bytes that is .....
    int size; //the size of the word
    Whoops no size so i'll use whatever is in that memory location that I have set aside for the variable size....

    array=new char[size];
    store=new int[size];
    array=word;

    ok two dynamic arrays no problem but is there room for the null byte...... then array=word WTF! I'm just a dumb compiler I don't know how to assign arrays like that.... should i tell him to use a loop ! nah i'm just a compiler i'll give him the usual indecipherable message.

    and all that use of new without ever using delete..... MEMORY LEAK!!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Unregistered
    Guest
    eh...in english
    or c++ for that matter

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    what dont you understand....

    1) main returns an int..... not void...
    2) when you use new you have to use delete when you are finished with the memory.... There is no garbage collector in c++ like in Java... You are responsible for the memory used from the free store....
    3)How can you expect to allocate an unknown size of bytes on the free store....
    4) you cannot assign arrays to each other its illegal in c++
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Unregistered
    Guest
    dang wrong one. OK this is the right one. PLEASE TELL ME WHAT'S WRONG!

    #include <iostream.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>


    //////ROT #1--try to keep use of global variables to a minimum.
    //////They work but are frowned upon

    //Defining global the variables
    char word [256]; //the inputed word
    int key1; //key1
    int key2; //key2
    int size; //the size of the word
    char *array; //temporary array
    int *store; //where the crypted word is stored


    //Function that makes the first encryption
    void encrypt_first()
    {
    for(int b=0; b<size; b++)
    {
    store[b]=store[b]+key1;
    }
    }

    //Function that makes the second encryption
    void encrypt_second()
    {
    for(int c=0; c<size; c++)
    {
    store[c]=store[c]-key2;
    }
    }

    //Function that makes the decryption
    void decrypt()
    {
    //first stage of decryption (key2)
    for(int f=0; f<size;f++)
    {
    store[f]=store[f]+key2;
    }
    //second stage of decryption
    for(int g=0; g<size; g++)
    {
    store[g]=store[g]-key1;
    }
    }

    //Main program function

    /////use int main() in preference to void main.
    void main()
    {
    //word=new char[size]; /////size hasn't been initialized yet!!
    cout <<"Enter a word: ";
    cin >> word;
    size=strlen(word); //determine the size of the word
    //size does NOT include the null terminator!!

    //get the word, and put every letter into a cell
    //array=new char[size];
    array = new char[size + 1];//need to allow space for null char
    store=new int[size];
    //array=word; can't assign char arrays like this, use strcpy!
    strcpy(array, word);

    //translates every letter into its ASCII number
    for (int a=0; a<size; a++)
    {
    store[a]=int(array[a]);
    }

    cout << "Enter key1 (a number): ";
    cin>> key1;



    encrypt_first(); //calls the 1st encryption function

    cout <<"Enter key2 (a number): ";
    cin>>key2;



    encrypt_second(); //calls the 2nd encryption function

    //display the cripted word
    cout<<"The crypted word is: ";
    for(int d=0;d<size;d++)
    {
    cout <<char(store[d]);
    }
    cout<<endl;


    cout<<"Enter your decryption keys!:\n";
    cout<<"Key1: ";
    cin >> key1;

    cout <<endl<<"Key2: ";
    cin>> key2;

    //call the decryption function
    decrypt();

    //display the decrypted data
    cout<<"Decrypted data: ";
    for (int e=0; e<size; e++)
    {
    cout<<char(store[e]);
    }
    cout << endl;

    return 0;
    }


    Now you wanna see something neat?--compile and run this:

    #include <iostream.h>
    int main()
    {
    char ch = 'A';
    int key1 = 2;
    char ch2;
    ch2 = ch + key1;
    cout << ch2;
    return 0;
    }

    If I haven't screwed it up the output should be C. Now you can apply that to your program and shorten it if you wish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 03:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 12:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21