C++ ENCRYPTION (how to encrypt a text)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jupi13
    New Member
    • Jul 2008
    • 18

    C++ ENCRYPTION (how to encrypt a text)

    i got a problem wtih my encryption...
    would you be kind to help me with this...i'm new to this c++ programing...

    i have this expansion encryption method..where you will add a char in between the original text..example >>>>. josh..this could be >>>> jxoxsxhx...like that...

    my program goes like this

    main()
    {
    char string[10],count;
    int x,y;

    cout<<"ENTER TEXT:";
    cin>>string;
    count=strlen(st ring);

    for(x=0;x<count ;x++)

    cout<<string[x]<<string[2];

    //if i'll enter "JOSH" the output would be "JSOSSSHS"
    //if i'll enter "jupi" the output would be "jpupppip"

    getche();
    }



    //what i need to do is to store the output in another array.
    example. array[] will be equal to the cout in the program above which is
    string[x] and string[2]...>>>>> array[x]="string[x]" and "string[2]"

    how will i do that?.....
    how will i do it?
    please help everyone..
  • Brosert
    New Member
    • Jul 2008
    • 57

    #2
    you could try Something like
    Code:
    // Obviously you have to make sure that the original string is less than 10 chars....
    
    char string2[sizeof(string*2)];
    int count2;
    
    for(int x = 0; x<count; x++)
    {
      count2 = 2*x;
      string2[count2]=string[x];
      string2[coount2 + 1] = string[2];
    }
    Although I'm sure there are far more elegant ways to do this

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      This dosent look encryption as your original characters remains as it is with newly added characters.
      Try using Blow-Fish for encrypting the string and the blow-fish is available on the net.

      Raghu

      Comment

      • jupi13
        New Member
        • Jul 2008
        • 18

        #4
        Originally posted by Brosert
        you could try Something like
        Code:
        // Obviously you have to make sure that the original string is less than 10 chars....
        
        char string2[sizeof(string*2)];
        int count2;
        
        for(int x = 0; x<count; x++)
        {
          count2 = 2*x;
          string2[count2]=string[x];
          string2[coount2 + 1] = string[2];
        }
        Although I'm sure there are far more elegant ways to do this



        what will i output in this code?
        string2?
        how will i do it?
        please help?

        if you don't mind..can you give the whole code?
        im just a newbie to C++;;


        thanks..

        Comment

        • Brosert
          New Member
          • Jul 2008
          • 57

          #5
          If you want to output something at the end of it, you can still use cout....

          eg
          Code:
          cout << string2;
          This code shoudl fit nicely into your current code...
          Where you declare string and count, also declare string and count2...

          where your forloop is, try the forloop in the original response.
          Last edited by Brosert; Jul 25 '08, 04:25 AM. Reason: Wrong info re count2

          Comment

          • jupi13
            New Member
            • Jul 2008
            • 18

            #6
            ehhyyy....thank s lot...it worked..

            hehehe
            ...thanks man...

            ahm,,if you don't mind,can you help me with this also?

            if i'll enter "brosert" the output would be "boet" and the missing letters ("rsr")
            would be stored in another array....

            Comment

            • Brosert
              New Member
              • Jul 2008
              • 57

              #7
              This time...

              Set up 3 arrays (you could probably do it with 2, but that would be more complex....), loop through, if we are on an even iteration store to one array, otherwise store to the other.
              I'll assume you have three equal sized arrays called string1, string2, string3...
              string1 recieves input (as before) in a null terminated string
              Code:
              //Create a pointer to the location in the string that we're up to -> this means we
              // will still be able to access the beginning of any string we need.
              char *strin1ptr = string1;
              char *string2ptr = string2;
              char *string3ptr = string3; 
              
              bool is2 = true;
              //loop while we have more characters in string1
              while(string1ptr)
              {
                // if we are on an even character
                if(is2)
                {
                  // set this location to contain the character in string1
                  *string2ptr = *string1ptr;
                   //bump string2ptr along
                   string2ptr++;
                }
                else
                  //likewise for string3
                  *string3ptr = string1ptr;
                   string3ptr++;
                }
                //bump string1ptr along too
                string1ptr++;
              }
              
              cout << string1 <<endl;
              count << string2 <<endl;
              count << string3 <<endl;

              Comment

              • jupi13
                New Member
                • Jul 2008
                • 18

                #8
                can you do it without using a pointer?

                we're not yet using pointers...

                we are only allowed to use array..and loop..

                can you do it?
                please...

                Comment

                • Brosert
                  New Member
                  • Jul 2008
                  • 57

                  #9
                  in that case, use a for loop....imnstea d of the while loop

                  eg
                  Code:
                  int count = sizeof(string1);
                  for(int x=0; x<count; x++)
                  {
                    /Check whether x is a multiple of 2
                    if(x%2 == 0)
                    {
                      string2[x/2] = string1[x];
                    }
                    else
                    {
                      string3[x/2] = string1[x];
                  }

                  Comment

                  • jupi13
                    New Member
                    • Jul 2008
                    • 18

                    #10
                    \brosert....... .....

                    //the program you give me only works in a 6-letter word and a 7-letter word...


                    //here it is


                    main()
                    {


                    char string[10],count,string2[20];
                    int count2;

                    cout<<"ENTER TEXT:";
                    cin>>string;
                    count=strlen(st ring);

                    for(int x=0;x<count;x++ )
                    {
                    count2 = 2*x;
                    string2[count2]=string[x];
                    string2[count2 + 1] = string[2];

                    }
                    cout<<string2;

                    getche();

                    //what's wrong with it.....my problem is that i want to expand the text...it should
                    work on text with 1-10 characters.....
                    if "i" is entered it would output.. "ii"...
                    if "you is entered it would output "yuouuu"... like that..
                    if "abcd" is enterd it would output "acbcccdcec "...

                    please help me...
                    please...

                    Comment

                    • Brosert
                      New Member
                      • Jul 2008
                      • 57

                      #11
                      Part of the problem is that it always takes the THIRD character....
                      If your string is shorter than 3 characters, it takes a (probably) NULL character, which terminates the string, and would give you a single character output....

                      where you get string try making the following modification :

                      Code:
                      int charAt =2;
                      
                      count=strlen(string);
                      //If we have a short input, take the last letter instead of the third
                      if(count <charAt) count=charAt;
                      then at the point where you actually assign the value, use
                      string[charAt]
                      instead of
                      string[2]

                      Comment

                      • jupi13
                        New Member
                        • Jul 2008
                        • 18

                        #12
                        Originally posted by Brosert
                        in that case, use a for loop....imnstea d of the while loop

                        eg
                        Code:
                        int count = sizeof(string1);
                        for(int x=0; x<count; x++)
                        {
                          /Check whether x is a multiple of 2
                          if(x%2 == 0)
                          {
                            string2[x/2] = string1[x];
                          }
                          else
                          {
                            string3[x/2] = string1[x];
                        }

                        i also have a question brosert....why is it that the index of string2 and string3
                        is "x/2"? why is it not x?

                        Comment

                        • Brosert
                          New Member
                          • Jul 2008
                          • 57

                          #13
                          because only half of the characters go in each string....
                          so string2 and string3 will be half the size of string1.

                          In c (and I think most languages) if we divide a number in a place where we can only use an integer, it will autometically round the number down....
                          so 0/2 = 0 (obviously)
                          but 1/2 = 0 (because we can only use the integer part of the answer).

                          this means, that if we always take the current value/2, we will always be entering into the correct box of each string (might need to think about that for a second....)

                          it means if string1 has 7 characters,
                          the ones in position 0,2,4,6 will become 0,1,2,3 in string2
                          the ones in position 1,3,5 will become 0,1,2 in string3

                          If you need a better explanation, I can try again....

                          Comment

                          • jupi13
                            New Member
                            • Jul 2008
                            • 18

                            #14
                            brosert.......

                            this one also...it only works with letters with 4 characters above..


                            int count = sizeof(string1) ;
                            for(int x=0; x<count; x++)
                            {
                            /Check whether x is a multiple of 2
                            if(x%2 == 0)
                            {
                            string2[x/2] = string1[x];
                            }
                            else
                            {
                            string3[x/2] = string1[x];
                            }


                            waht should i do?

                            i can't figure it out..
                            please help..

                            Comment

                            • Brosert
                              New Member
                              • Jul 2008
                              • 57

                              #15
                              Not real sure....
                              try changing
                              count = sizeof(string1) to = strlen(string1) (as you had in your first program....

                              Meanwhile I'll have a deeper look/think

                              Comment

                              Working...