Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Mis-use of Char or Proper?  (Read 5037 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Mis-use of Char or Proper?
« on: December 23, 2016, 09:42:40 AM »
Picked back up on my one project to work on it further and after months of not looking at my code, I read through it again and realized that I might be mis-using code in which it works, but is not correct usage. I looked on google to try to find information on how it works to support whether its correct or not or if I am actually using a buffer batch like exploit in my code and this is the closest match to what I have: http://stackoverflow.com/questions/21675628/why-am-i-able-to-put-more-than-one-character-into-a-variable-of-type-char

Without listing the entire huge program I am just going to discuss the key areas. I initialize a Char at ch1 and have an input that is rested inside a while loop. This input into ch1 will take quite a few characters without a specified string length set. This is sort of sloppy when string length is unknown, but it works. When the user inputs data at the input such as Hello_World and then presses the enter key, it handles each character of the string input individually to end of the string. But the code is not told to handle the input as a string, instead the code appears to handle Hello_World as almost a batched character input where it will run the while loop for the number of characters entered. So the input is almost batching the characters as (  H, e, l, l, o, _, W, o, r, l, d ) then runs each character at a time as a single input stepping to the next then it ends when no more characters to process as an input.

Looking online as to how and why this works, I found this comment:
   
Quote
You type the whole string, the first getchar() get first character, the remaining string stay at input stream, and your later getchar() inside while loop read them(one by one) and print out(one by one) until there are no char left(EOF), that's what happen. – moeCake Feb 10 '14 at 11:22

Trying to dig further into if this is proper use or not I find nothing. Additionally I was trying to figure out how a string could be held at a char input which is intended for just one single character to be stored at. Where does it store or buffer or batch this string for picking away at it 1 character at a time to end of the input character length.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Mis-use of Char or Propper?
« Reply #1 on: December 23, 2016, 10:12:24 AM »
Standard Input and Standard Output are default streams attached to most processes. They operate like any other streams and this includes the presence of a buffer.

getchar() reads the next character in the standard input stream. SO when the code reaches the getchar() function, it blocks waiting for the next character.

However text is only added to the standard input buffer from the keyboard when you type enter. so when you type a line- say your Hello World example- and press enter, you are putting "Hello World" into the buffer. So getchar() reads the first character, "H", then drops into the loop, where it prints "H", reads the next character, "E", prints it, reads the next character, "L" ... etc.

Coincidentally, I had a run-in with a standard buffer issue myself, in something of a different way.

We have a Java program which our C# software invokes for reports. Recently, we added a capability to batch reports; that is, a single invocation of the Java program would be able to effectively run the same report for different sets of parameters (this avoided Java startup time and a bunch of other overhead connecting to the database and such).

What we found (or... well, actually our customers found it...) was that in using the new feature to try to mass reprint a large number of them at the same time, it would get stuck at about 300 or so. Even stranger, if the C# program that launched it was closed, suddenly the Java program started churning out report files again! It was quite a curious problem.

As it happens, these standard stream buffers- the same one that get's filled  when you type a line of input into that getchar() loop- have a maximum size and that was the problem. The C# program redirects the standard input and output streams because it needs to send in database login info through stdin. Now, normally, it will have a background thread making sure the standard output is "drained" so it doesn't fill up, however a logic issue that dated back to 2014 was preventing that thread from ever being started (Woops!). We only saw an issue crop up recently because it wasn't until we added that batched feature that the standard output buffer was filled up- when the buffer gets filled, then the Java program blocks on the System.out.println() waiting for the standard output stream to be read to free space in the buffer, which never happens. Closing the original program fixes  the issue as it stops the redirection, and if the buffer is never filled then the java program finishes it's task and exits without issue and all those standard streams get closed.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Mis-use of Char or Proper?
« Reply #2 on: December 23, 2016, 11:40:43 AM »
Thanks for the info... also do you know what the maximum size is for the buffer. Is it the same as the default clipboard buffer or its own buffer?

I had a oddity happening within my loop with my latest code and not ready to tap out yet and ask for help on it as for maybe looking at my code with fresh eyes tonight it will jump out at me, but I have a counter that counts to 1 from 0 and resets to 0 when counter =1 after it completed the iteration instruction for a 0,1,0,1,0,1,0,1... flipflop and this way it uses two different arrays alternating. Current code starts at 0 and gets stuck at 1, so its 0,1,1,1,1,1,1,1,1,1 and looking at the code it should with the IF statement when counter=1 set it back to 0 to run the next iteration as 0 and use the other array. When the code looked correct and the outcome of how it runs was messing up I started digging for a cause and when i saw my string passed to char through the buffer, I got curious if this was breaking it and maybe I was misusing char with unknown string length and getting abnormal execution within it as a result of this.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Mis-use of Char or Proper?
« Reply #3 on: December 23, 2016, 12:36:46 PM »
Thanks for the info... also do you know what the maximum size is for the buffer. Is it the same as the default clipboard buffer or its own buffer?

According to this the default size is 4K. Would be consistent with what I was seeing.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Mis-use of Char or Proper?
« Reply #4 on: December 23, 2016, 07:33:22 PM »
Cool thanks for your help with this. 4k is pretty decent size. Better than the 512 character limit that I thought I might have had. So it can store 1024 to 4096 characters before overflow. http://stackoverflow.com/questions/4100324/how-many-characters-can-be-stored-in-4kb

Now to tackle the bug in my code with that counter that should be as clear as day and is acting up.  ;D

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Mis-use of Char or Proper?
« Reply #5 on: December 23, 2016, 07:49:25 PM »
Now to tackle the bug in my code with that counter that should be as clear as day and is acting up.  ;D

If you like, you can plop it here I could take a look and see if I spot anything off.
I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Mis-use of Char or Proper?
« Reply #6 on: December 26, 2016, 06:03:41 PM »
Fixed my counter issue...and its a strange fix.

So in my code if you have

IF (flipflop==1){
flipflop=0;
}


It doesnt work.

BUT if you use this it works

IF (flipflop==1){
flipflop=-1;
}


Now its no longer doing the 01111111111111 and is now proper with 010101010101010101....

Here is a copy/paste from my program. The ABCDEFGHI = shows the crypt substitute for those characters per crypt. there are 2 crypts used so you see this twice below but different characters substituting in the 2nd group.

Quote
Crypt #1 String Key =

X=rGg"kZ:,@wf<msx3#tUL/*pnRNCc9-Q6SK!Y~?1_EP7h>TIF$4HJl(0jo%O;2Aedaivu5Vz&M)B.y8
bWD]'^q+\[

ABCDEFGHI = X=rGg"kZ:


Crypt #2 String Key =

o1zDMj";m(ER%h!7_eC=NT*P^Ygt4pn+v53#L'O$u~J6dKlwF-Q2IWyiq\9>k&8V]/?,rHSx:U)Xs@aA
[BG.f<cbZ0

ABCDEFGHI = o1zDMj";m


Enter Info to Crypt in correct case
To Exit Inner Program enter  ( ` )


AAAABBBBCCCCDDDDEEEEFFFFGGGGHHHHIIII
XoXo=1=1rzrzGDGDgMgM"j"jk"k"Z;Z;:m:m

The alternating character substitution shows it working as I wanted it to for 2 crypts. Not sure why it will work with -1 and not with 0, but i was like ok... this makes no sense at all, but what if I remove 1 with -1 from flipflop instead of setting it back to 0.  ::)

At least now I can move forward with making it more complex.  :P



Geek-9pm


    Mastermind
  • Geek After Dark
  • Thanked: 1026
    • Gekk9pm bnlog
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Mis-use of Char or Proper?
« Reply #7 on: December 26, 2016, 06:57:21 PM »
flipflop=-1;
This has been documented elsewhere.
Both Intel and Microsoft say you mist use -1 as a signed 16 bit integer to get all ones up and alive.
1111 1111b
This is more often an issue in verily low level code. Like assemble.
I can not find this in a Google,but I know it it is what Intel requires at the machine code level.
true = -1
false = 0
In x86 code you have a non zero and zero test.  Using all ones for true allows bit testing for program control.

See also two's complement arithmetic.





BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Mis-use of Char or Proper?
« Reply #8 on: December 27, 2016, 01:06:30 AM »
I don't even really understand the problem. was flipflop not 1? Did you not initialize it to a known value beforehand?

Why not initialize it to 1 or 0 and use:

Code: [Select]
flipflop = !flipflop;
It will flipflop from 1 to 0. !1 is 0. !0 is 1.

I was trying to dereference Null Pointers before it was cool.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Mis-use of Char or Proper?
« Reply #9 on: December 27, 2016, 09:24:39 AM »
Initialized initially as 0

Then first time through the loop it is 0 ( This part works correct )

Then next iteration of loop it has flipflop++; which increments +1 ( This part works correct )

After its done processing for that loop in which the value is 1, the value is switched back to 0 with

IF(flipflop==1){
flipflop=0;
}

And this part doesnt work and for such one of the most basic of instructions I hit this problem months ago and it was frustrating in how this should switch flipflop to 0 if flipflip is 1, but it wasnt doing that.

Now here is where it gets really weird... I didnt change the placement of where the flipflop is reset in the program. And I tried using modulus even as a means to have even where  mod = 0 and odd having mod = 1 and that didnt work. I said what if I set the value to -1 and compiled and ran it and all of a sudden it was working.

Here below is the latest program that is built upon my first. I ran into problems with the conversion from what i had initially to ASCII calls, so I stuck with the long list of IF statements to test the input character and then assign it a value reference to a shuffled crypt as a 1 for 1 replacement.

In this newest code there are 2 crypts and it alternates between use of crypt key 1 and crypt key 2. With this the character A substituted with say X would only be X at say every odd array element in which there is an A to substitute for in the input string that is to be jumbled and cryptic. My first program was just a 1 for 1 replacement and so it was pretty easy to break and see the message knowing that every character that is the same represents the same character substitution. With the newer more complex program that uses 2 crypt keys to generate the cryptic output A and X wont represent the same character so

AVAILABLE in my first program was    X5X:#X8k!                     

AVAILABLE in my second program is   X5X:#$8k!

The colored letters/characters representing where A is in each output. A to be X A has to land at a odd interval, whereever A lands at an even interval it is no longer X but instead it is $ since $ is from the second crypt key and same array element placeholder as the first. So if A was 1 then Array 1 element 1 is X and Array 2 element 1 is $

Also very cool on sharing the
Quote
flipflop = !flipflop;
method. Ive never seen this trick done before. Question i have with its use is if its only good for 010101 pattern or if you can use a value specification with its use to get say 012301230123 etc? For years I have used the Increment and IF reset method.

Here is the entire program below. The part that might be confusing is the key strength part. If you use a value such as 9 it will use a weak key. 9 states that 9 characters can be the same between the original character from the string and the crypt output. Some shuffles have characters that stay in their original place within the array. I added this test for shuffle strength I think after I shared my first program last year, so this might be new to anyone who remembers the first program. I wrote a smaller program that used this key strength object as a means to try to discover the worst keys in which I told it to run for 1 billion keys from 1 to 1 billion shuffles of the array and look for characters that remained in their original element location within the array. I found a few that had 14 characters that remained in their original location, but didnt find the worst shuffle possible in which it is possible to use a value as a key and the shuffle places every character back into their original position in which there is none of the input cryptic, it all would go in as Hello_World and it would be written to file was Hello_World. I was very curious if there was the worst shuffle of all within the first billion keys and if so, how many times it hit that worst of all shuffle in which nothing ever got shuffled. I then realized after running my AMD FX-8350 4Ghz 8-core computer for almost a week that the best means to avoid use of a horrible key would be instead to just have it test the key right then and there as its input by user. So the electric wasted through this process was heating my home last winter for a week, and at least i know that the first billion keys dont have the worst shuffle of all in which the input becomes the same as the output.  ;D Additionally my program is single-threaded and I had it processing in 8 programs running with 125 million keys each in ranges from 1 to 125 million, 125 million and 1 to 250 million and 1, and so on in 8 groups each using a core of the 8-core CPU to process it all that much sooner vs 1 core of 8 pegged and it taking 8 times longer to get to 1 billion because all processing power not utilized.

After you put in 2 keys and it shows the 2 keys scrambled on the screen that it is going to use, you then type in whatever you want, however it does not support spaces and so underscores can be used as space holders. You then press the enter key when everything to scramble is typed and you will see it display characters from left to right, this can happen fast with a fast computer or slow if your on an Intel Atom or Pentium 4 computer. When you see this scrambled output to exit the program you press the [ ` ] key which is the key that the tilde key is but the other character that skips my mind as to its name. And press enter after pressing the [ ` ] key. This will then write the last character of the scramble to the data file that saves the message etc. You also will have 3 other files created. A counter file that is used to keep track of what was the last value to have them listed in the files and be able to know that data at line 5 goes with the 2 key files that are also line 5. So reassembly of the data requires you to have Key 1 and Key 2 from line 5 which goes with the data at line 5 in the data file. *Note: The program to unscramble/decrypt it is not contained within the main program. They are intended to be isolated as well as the keys and the data also isolated so that they can be physically separated and brought back together like pieces of a puzzle to get the messages back out from if you have the decrypt program, correct keys to put in and the data that needs to be unscrambled.

Reasoning behind this program is to take my very important data and put it on a thumb drive that I can store elsewhere and if my home burned to the ground I wouldnt lose this data. I use unique complex passwords with just about everything from banking to even online games that I dont want to get hacked etc. This data isnt safe on a cloud. I like having physical control of my data. I was for a while leaving a small notebook in my car with this data, but it was far too risky having something so easily read by another. I then put it on a thumb drive in my car with this data in a hidden location but the one day i realized I would be really scrambling to change passwords if this ever got lost ( fell out of car etc ) as well as if someone got their hands on a thumb drive say when getting an oil change and they are cleaning and they find it and pocket it the first thing they would want to do is see what is on it. I could have bank accounts cleaned out etc. So this is why I am working on making my own tool for my data to keep it safe. I plan on adding a salt program later to this that also adds extra characters so that makes the data even that much more difficult to crack not knowing the message length, character substitution not always the same, and what is real from what is noise in the data.  ;D

The IDE I am using btw is Bloodshed Dev C++ 4.9.9.2 in which I have been aware that this IDE could have bugs that I might face some day and maybe just maybe the way that this IDE is compiling my program the reset to 0 doesnt work unless you use -1.  ::)

Code: [Select]
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>     

using namespace std;


int main ()
{
    long long int seed1=0;
    long long int seed2=0;
    int code1=0;
    int run=1;
    int again=1;
    int test=1;
    int writeenable=0;
    int counter=0;
    int intcheck=0;
    int intcheck2=0;
    char ch1;
    char ch2;
    int ST,ST2,match2,match3,flag;
    int flipflop=0;
   
        while(again==1){
        system("color 8e"); // Black text with Yellow background       
   
   while(intcheck==0){
    cout<<"                  XXXXXXXXXXXXXXXXXXXXXXXXXX \n\n";
    cout<<"                  X  Crypt Version 2.00C   X\n\n";
    cout<<"                  X Build 002 - 12/26/2016 X\n\n";
    cout<<"                  XXXXXXXXXXXXXXXXXXXXXXXXXX\n\n\n";
    cout<<"                     Enter Integer Seed 1: \n"; // Asks user to input integer seed
    cout<<"\n\n";
    cin >> seed1; // Input user seed1
    cout<<"\n"<<"                     Enter Integer Seed 2: \n"; // Asks user to input integer seed
    cout<<"\n\n";
    cin >> seed2; // Input user seed2
    cout<<"\n\n\n";
    cout<<"Enter Key Strength Value\n\n";
    cout<<"Note: 0 = complete scramble and 3 = 3 matching pairs in crypt\n\n";
    cin>>flag;
   
   // Seed 1 Test
   // --------------------------------------------------------------------------
    string str3="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;[]\"\'";
    string str4="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;[]\"\'";
   
    srand(seed1); // Allows user custom seeded starting algorithm position for random
   
    random_shuffle(str4.begin(), str4.end()); // Shuffle the string
   
    // -------------------------------------------------------------------------
   
    ST=0;
match2=-1;


    while(ST<=89){
                  if(str3[ST]==str4[ST]){
                                        //cout<<str[ST]<<"\n";
                                        match2++;
                                        }
                                        else{
                                             }
                                             ST++;
                                             }

                                             if(match2>=flag){
                                                               match2++;
                                                               intcheck=0;
    system("cls");
    system("color 4F");
    cout<<" Warning: Crypt Key #1 Weak with [ "<<match2<<" ] matching characters \n";
    cout<<" Please enter a different key\n\n\n";
                                                                           
}
else{
    system("cls");
    cout<<"Key #1 Approved\n\n\n";
    intcheck=1;
     }
   
    // ----------------------------------------------------------------------

 
// } closed loop on 110 and prior, remains open on 200 and after

   // Seed 2 Test
   // --------------------------------------------------------------------------
    string str5="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;[]\"\'";
    string str6="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;[]\"\'";
   
    srand(seed2); // Allows user custom seeded starting algorithm position for random
   
    random_shuffle(str6.begin(), str6.end()); // Shuffle the string
   
    // -------------------------------------------------------------------------
   
    ST2=0;
match3=-1;


    while(ST2<=89){
                  if(str5[ST2]==str6[ST2]){
                                        //cout<<str[ST2]<<"\n";
                                        match3++;
                                        }
                                        else{
                                             }
                                             ST2++;
                                             }

                                             if(match3>=flag){
                                                               match3++;
                                                               intcheck2=0;
    system("cls");
    system("color 4F");
    cout<<" Warning: Crypt Key #2 Weak with [ "<<match3<<" ] matching characters \n";
    cout<<" Please enter a different key\n\n\n";
                                                                           
}
else{
    system("cls");
    cout<<"Key #2 Approved\n\n\n";
    intcheck2=1;
     }
   
    // ----------------------------------------------------------------------

 
}


    system("color 8f"); // Black text with Yellow background

   
   
        // Read in from text file the value for foldercounter variable
        ifstream myfile("counter200.txt", ifstream::in);
        while(myfile >> counter);

       counter=counter+1;

               //write value to pick up with next time program is run here
       std::ofstream write ("counter200.txt", std::ofstream::out);
       write << counter;
       write.close();

               //write value to pick up with next time program is run here
       std::ofstream write2 ("Crypt200Key1.txt", std::ofstream::out | ios::app);
       write2 <<"\n"<<counter<<"   "<<seed1;
       write2.close();
       
                      //write value to pick up with next time program is run here
       std::ofstream write5 ("Crypt200Key2.txt", std::ofstream::out | ios::app);
       write5 <<"\n"<<counter<<"   "<<seed2;
       write5.close();
 
                 //write value to pick up with next time program is run here
       std::ofstream write3 ("Crypt200Data.txt", std::ofstream::out | ios::app);
       write3 <<"\n"<<counter<<"   ";
       write3.close();
   
    //Initialize Valid Characters for String Shuffle Output
    //Note Bug corrected with blank space for \ by use of escape character proceeding
    string str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;[]\"\'";
    srand(seed1); // Allows user custom seeded starting algorithm position for random
    random_shuffle(str.begin(), str.end()); // Shuffle the string
    cout<<"\n\n"<<"Crypt #1 String Key =\n\n";
    cout << str << "\n\n\n"; // Output the shuffle sequence

        //Pass String Output into an array to pair up pointer value with associated character
    string tmp = str; //Pass str output to string tmp
    char tab2[128]; // Memory Allocation for array population
    strncpy(tab2, tmp.c_str(), sizeof(tab2)); //string copy tmp into tab2 array
    tab2[sizeof(tab2) - 1] = 0;
   
    //HERE #2
        //Initialize Valid Characters for String Shuffle Output
    //Note Bug corrected with blank space for \ by use of escape character proceeding
    string str7="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()_-+=?<>:\\/~.,;[]\"\'";
    srand(seed2); // Allows user custom seeded starting algorithm position for random
    random_shuffle(str7.begin(), str7.end()); // Shuffle the string
    cout<<"\n\n"<<"Crypt #2 String Key =\n\n";
    cout << str7 << "\n\n\n"; // Output the shuffle sequence

        //Pass String Output into an array to pair up pointer value with associated character
    string tmp2 = str7; //Pass str output to string tmp
    char tab20[128]; // Memory Allocation for array population
    strncpy(tab20, tmp2.c_str(), sizeof(tab20)); //string copy tmp into tab2 array
    tab20[sizeof(tab20) - 1] = 0;

   
        cout<<"Enter Info to Crypt in correct case\n";
        cout<<"To Exit Inner Program enter  ( ` ) \n\n\n";
   
    while(run==1){

    cin>>ch1;
//supress write to file until 1 loop run ( bug fix for double character write )
if(writeenable==1){
                   
               if(flipflop==0){   
       //write value to pick up with next time program is run here
       //string tab2;
       std::ofstream write4 ("Crypt200Data.txt", std::ofstream::out | ios::app);
       write4 << tab2[code1];
       write4.close();
       }
       
       if(flipflop==1){
       
       //string tab20;
       std::ofstream write4 ("Crypt200Data.txt", std::ofstream::out | ios::app);
       write4 << tab20[code1];
       write4.close();
       
       } // While FF1
} //write enable
else{
     //do nothing and continue
     }
    writeenable=1; //enable write to file
   
    if (ch1=='A'){
         code1=0;
                }
    else if (ch1=='B'){
         code1=1;
         }
    else if (ch1=='C'){
         code1=2;
         }
    else if (ch1=='D'){
         code1=3;
         }
    else if (ch1=='E'){
         code1=4;
         }
    else if (ch1=='F'){
         code1=5;
         }
    else if (ch1=='G'){
         code1=6;
         }
    else if (ch1=='H'){
         code1=7;
         }
    else if (ch1=='I'){
         code1=8;
         }
    else if (ch1=='J'){
         code1=9;
         }
    else if (ch1=='K'){
         code1=10;
         }
    else if (ch1=='L'){
         code1=11;
         }
    else if (ch1=='M'){
         code1=12;
         }
    else if (ch1=='N'){
         code1=13;
         }
    else if (ch1=='O'){
         code1=14;
         }
    else if (ch1=='P'){
         code1=15;
         }
    else if (ch1=='Q'){
         code1=16;
         }
    else if (ch1=='R'){
         code1=17;
         }
    else if (ch1=='S'){
         code1=18;
         }
    else if (ch1=='T'){
         code1=19;
         }
    else if (ch1=='U'){
         code1=20;
         }
    else if (ch1=='V'){
         code1=21;
         }
    else if (ch1=='W'){
         code1=22;
         }
    else if (ch1=='X'){
         code1=23;
         }
    else if (ch1=='Y'){
         code1=24;
         }
    else if (ch1=='Z'){
         code1=25;
         }
    else if (ch1=='a'){
         code1=26;
         }
    else if (ch1=='b'){
         code1=27;
         }
    else if (ch1=='c'){
         code1=28;
         }
    else if (ch1=='d'){
         code1=29;
         }
    else if (ch1=='e'){
         code1=30;
         }
    else if (ch1=='f'){
         code1=31;
         }
    else if (ch1=='g'){
         code1=32;
         }
    else if (ch1=='h'){
         code1=33;
         }
    else if (ch1=='i'){
         code1=34;
         }
    else if (ch1=='j'){
         code1=35;
         }
    else if (ch1=='k'){
         code1=36;
         }
    else if (ch1=='l'){
         code1=37;
         }
    else if (ch1=='m'){
         code1=38;
         }
    else if (ch1=='n'){
         code1=39;
         }
    else if (ch1=='o'){
         code1=40;
         }
    else if (ch1=='p'){
         code1=41;
         }
    else if (ch1=='q'){
         code1=42;
         }
    else if (ch1=='r'){
         code1=43;
         }
    else if (ch1=='s'){
         code1=44;
         }
    else if (ch1=='t'){
         code1=45;
         }
    else if (ch1=='u'){
         code1=46;
         }
    else if (ch1=='v'){
         code1=47;
         }
    else if (ch1=='w'){
         code1=48;
         }
    else if (ch1=='x'){
         code1=49;
         }
    else if (ch1=='y'){
         code1=50;
         }
    else if (ch1=='z'){
         code1=51;
         }
    else if (ch1=='1'){
         code1=52;
         }
    else if (ch1=='2'){
         code1=53;
         }
    else if (ch1=='3'){
         code1=54;
         }
    else if (ch1=='4'){
         code1=55;
         }
    else if (ch1=='5'){
         code1=56;
         }
    else if (ch1=='6'){
         code1=57;
         }
    else if (ch1=='7'){
         code1=58;
         }
    else if (ch1=='8'){
         code1=59;
         }
    else if (ch1=='9'){
         code1=60;
         }
    else if (ch1=='0'){
         code1=61;
         }
    else if (ch1=='!'){
         code1=62;
         }
    else if (ch1=='@'){
         code1=63;
         }
    else if (ch1=='#'){
         code1=64;
         }
    else if (ch1=='$'){
         code1=65;
         }
    else if (ch1=='%'){
         code1=66;
         }
    else if (ch1=='^'){
         code1=67;
         }
    else if (ch1=='&'){
         code1=68;
         }
    else if (ch1=='*'){
         code1=69;
         }
    else if (ch1=='('){
         code1=70;
         }
    else if (ch1==')'){
         code1=71;
         }
    else if (ch1=='_'){
         code1=72;
         }
    else if (ch1=='-'){
         code1=73;
         }
    else if (ch1=='+'){
         code1=74;
         }
    else if (ch1=='='){
         code1=75;
         }
    else if (ch1=='?'){
         code1=76;
         }
    else if (ch1=='<'){
         code1=77;
         }
    else if (ch1=='>'){
         code1=78;
         }
    else if (ch1==':'){
         code1=79;
         }
    else if (ch1=='\\'){ // Escape Character \ needed to allow \ check
         code1=80;
         }
    else if (ch1=='/'){
         code1=81;
         }
    else if (ch1=='~'){
         code1=82;
         }
    else if (ch1=='.'){
         code1=83;
         }
    else if (ch1==','){
         code1=84;
         }
    else if (ch1==';'){
         code1=85;
         }
    else if (ch1=='['){ // added 86 thru 89 in version 1.10
         code1=86;
         }
    else if (ch1==']'){
         code1=87;
         }
    else if (ch1=='\"'){
         code1=88;
         }
    else if (ch1=='\''){
         code1=89;
         }
    else if (ch1=='\`'){ //Escape Character \ before ` to exit
         run=0; //Run = False at 0 and leaves while loop
         }
    else {
         cout<<"Invalid Input = No Match\n\n";
         system("color CF"); // White text with Red background
         }
         system("color 8b"); // Black text with Green background
         // Display output
         //string tab2; //why?  `tab2' undeclared (first use this function)
         
         //Key #1 crypt
         if(flipflop==0){
         
         cout<<tab2[code1];
         
         }
         //Key #2 crypt
         if(flipflop==1){
                         
         cout<<tab20[code1];

                         }
         if(flipflop==1){
                         flipflop=-1; //Flipflop -1 because setting back to 0 is broken
                         }
flipflop++; //starts at 0 and alternates 010101010101 for 2 keysets
         }// end inner while loop
               
  test=1;
  while(test==1){
          system("CLS");
          cout<<"Enter ( Y ) to continue - or - ( N ) to end program\n\n\n";
          cin>>ch2;
          if (ch2=='N'||ch2=='n'){
          // line return to file  ( formatting purposes )   
       std::ofstream write4 ("Crypt200Data.txt", std::ofstream::out | ios::app);
       write4<<"\n";
       write4.close();
                     
           again=0;
           test=0;
                }
    else if (ch2=='Y'||ch2=='y'){
         // line return to file  ( formatting purposes )
       std::ofstream write4 ("Crypt200Data.txt", std::ofstream::out | ios::app);
       write4<<"\n";
       write4.close();
       
         intcheck=0; 
         again=1;
         test=0;
         run=1;
         writeenable=0; //reset write enable to start of 0
         system("CLS");
         }
    else {
         cout<<"Invalid Input, please choose Y or N \n\n";
         test=1;
         }   
         }
         
         
} // end outter while loop
    system("CLS");
    system("PAUSE");
    //return EXIT_SUCCESS;
    return(0);
}




DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: Mis-use of Char or Proper?
« Reply #10 on: December 27, 2016, 11:32:29 AM »
Quote
         if(flipflop==1){
                         flipflop=-1; //Flipflop -1 because setting back to 0 is broken
                         }
flipflop++; //starts at 0 and alternates 010101010101 for 2 keysets


Mystery solved when more awake to look at own code  ;D ... Looking back at my code it looks like there is no good place for flipflop++ to increment and not affect the 0 to flip it to 1 and so the -1 fix I did, its changing the -1 to 0 and then counting to 1 in which it then is flagged to go back to -1 and then immediately set to 0 from the flipflop++ which +1 to -1 to make 0.

So thats why it was 011111111 when resetting to 0 because immediately it was incrementing the 0 to 1.  Cant believe how stupid the fix was and I didnt catch that increment was hitting the 0 right away. :-[

So its doing:
-1
0 // immediate from flipflop++ following -1
1
-1
0 // immediate from flipflop++ following -1
1
-1
0 // immediate from flipflop++ following -1
1