简体   繁体   中英

printf displays “-858993460” or “╠╠╠╠╠╠╠╠”; what are these?

I am trying to build a program that encrypts a password and then does the reverse process (tells a correct password from an encrypted one).

For that, I have:

  • a password ( char[] type);

  • an encryption key ( vector - int[] type) that has the same length as the char of the password;

  • two steps (placed also in a vector of type int step[2] ).

The requirement is that the encryption process has to be built using these two steps:

  • the first one (the value in step[0] ) is used to add the value (ASCII) starting from the first position of the password char to the first position of the encryption key vector for a number of steps equal to the first step step[0] ;

for example, adds char password[0] to int key[0] , then adds char password[1] to int key[1] and so on for a number of steps equal to the value placed in step[0] .

  • the second one ( step[1] ) subtracts from the corresponding position of the ASCII value of the password char , the value of the encryption key for a number of steps equal to the second step ( step[1] ).

for example, subtracts char password[5] from int key[5] , then subtracts char password[6] from int key[6] and so on for a number of steps equal to the value placed in step[1] .

And then, the process repeats until the end of the length of the password char .

I built a function as below that should do this (addition for a number of steps, then subtraction for a number of other steps, and repetition of the process until the end of the password char - the encryption key vector has the same length as the password char).

The result is placed in a new vector (for encryption) or in a new char (for the reverse process of finding the password).

void criptareFinal(char password4[255], 
                   int longKey1[255], 
                   int b4, 
                   int step2[2]) {
    int encryptedPass[255];
    int a = step2[0], 
        b = step2[1], 
        n = b4, 
        i,
        ap = a, 
        bp = b; 

    for (i = 0; i < n; i++) {
        if (ap > 0) {
            encryptedPass[i] = longKey1[i] + password4[i];
            ap--;
        }
        else if (bp > 0) {
            encryptedPass[i] = longKey1[i] - password4[i];
            bp--;
        }
        else if (ap == 0 && bp == 0) {
            ap = a;
            bp = b;
        }
    }

    int i1;
    printf("\n");
    for (i1 = 0; i1 < b4; i1++)
        printf("%d ", encryptedPass[i1]);
}

Then if I try to printf the vector (for encryption process) in order to show the content of the vector, a message similar to this one appears:

1090 923 916 1151 942 913 962 998 960 936 962 917 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460

why does it show "-858993460"?


Also, if a try to printf the char for showing the password in the reverse process (form an encrypted password to a readable one) using the following code,

void decriptareFinal(int encryptedPass5[255], 
                     int longKey2[255], 
                     int b5, 
                     int steps3[2]) {   
    char Password1[255];
    int a = steps3[0], 
        b = steps3[1], 
        n = b5, 
        i2, 
        ap = a, 
        bp = b;

    for (i2 = 0; i2 < n; i2++) {
        if (ap > 0) {
            Password1[i2] = encryptedPass5[i2] - longKey2[i2];
            ap--;
        }
        else if (bp > 0) {
            Password1[i2] = longKey2[i2] - encryptedPass5[i2];
            bp--;
        }
        else if (ap == 0 && bp == 0) {
            ap = a;
            bp = b;
        }
    }

    int j2;
    printf("\n");
    for (j2 = 0; j2 < b5; j2++)
        printf("%c", Password1[j2]);
}

then this message appears:

côő~ypaXOF╠╠╠╠╠╠╠╠

what is "╠"?

this "╠╠╠╠╠╠╠╠" is an incorrect display (the rest "côő~ypaXOF" is correct).


Also. I tried to add and subtract manually form [0] to the end of the string/vector and if I do that, no error message of type -858993460 or of type ╠╠╠╠╠╠╠╠ appears. It works fine and that tells me that the values in the char (password) or int (key) are correct.

If it is of importance, I work on Windows 64bit , but I use Visual Studio . Also, I have a Mac . There I have the same problem, but instead of -858993460 or ╠╠╠╠╠╠╠╠ I get other messages (for instance 0 instead of -858993460 ).

There were a lot of changes I made to your code. Here is the list:

  • Added more #include tags for standard libraries (I'm guessing you already had some and didn't post them)
  • Changed function arguments to be char* instead of char[] , it is not conventional to use char arrays in function arguments and I'm not even sure how they work in your code.
  • Got rid of uneccessary/extra variables: ap , bp and some others. Since C is pass-by-value , you don't need temporary variables for the steps variables.
  • Fixed variable naming. A lot of your variable names were unintuitive/weird and I felt I had to change the names to something more accurate.
  • Used malloc (dynamic memory allocation) instead of placing char arrays on the stack. This is more flexible and reduces the chances of those gotchas, but does open up more possibility for segfaults.
  • Fixed your encryption/decryption algorithm since it would leave the char the same if it had just finished going through both steps ( ap == 0 && bp == 0 ).
  • Fixed some syntax errors.
  • Added some debugging fprintf statements, so you can see what's going on.

As for your weird ╠ character, I can't see why your code would print that other than maybe you have some memory error or your environment is switching between Unicode and ASCII or something. Frankly, I don't want to figure it out because your code is kind of a mess and I don't think you posted all of it because it had compilation errors when I first ran it.

I also added some null terminating characters ( '\\0' ) to the ends of the malloc 'd strings that will be handy for properly printing the strings when debugging.


Anyway, enough of that. Here's the code:

INITIAL: 72 101 108 108 111 32 87 111 114 108 100 33

ENCRYPTION: 73 103 -105 -104 -106 38 94 -103 -105 -98 111 45
ENCRYPTED: Igùÿû&^Öù₧o-

DECRYPTION: 72 101 108 108 111 32 87 111 114 108 100 33
DECRYPTED: Hello World!

NOTE: There is no error handling in this code, so if the user inputs are invalid in pretty much any way, the code will break and could lead to segfaults, infinite loops, etc. You should add error handling to every char* manipulation and array iteration before you use this in a properly-made application.


And some sample output:

 INITIAL: 72 101 108 108 111 32 87 111 114 108 100 33 ENCRYPTION: 73 103 -105 -104 -106 38 94 -103 -105 -98 111 45 ENCRYPTED: Igùÿû&^Öù₧o- DECRYPTION: 72 101 108 108 111 32 87 111 114 108 100 33 DECRYPTED: Hello World! 

Let me know if you have any questions.

In the first function, you iterate i from 0 to n one step at a time. However, not all paths through the loop body assign anything to encryptedPass[i] - this is the case for both the last else if and when none of the if conditions matches. Likewise for the second function, but for Password1[i] .

At the end of each of these functions you do, however, unconditionally print all values from these arrays, which means they are uninitialized values. (And you iterate up to unknown values b6 and b7 , whereas you probably should iterate up to the same n that you use in the first loop.)

To fix, either ensure that each iteration writes something to the index, or have a separate write position index or pointer and only advance it when you write something.

Thank you all for the answers, esspecially to Arkku. That helped a lot. I fixed it and now it works fine. I added "i = i - 1" at the last condition, after "else if (ap == 0 && bp == 0)" in order to mantain the iteration on the same position.

This is what I did:

for (i2 = 0; i2 < n; i2++) {
if (ap > 0) {
    Password1[i2] = encryptedPass5[i2] - longKey2[i2];
    ap--;
}
else if (bp > 0) {
    Password1[i2] = longKey2[i2] - encryptedPass5[i2];
    bp--;
}
else if (ap == 0 && bp == 0) {
    ap = a;
    bp = b;
    i = i - 1;
}

That solved the problem.

Same at the other function:

for (i = 0; i < n; i++) {
if (ap > 0) {
    encryptedPass[i] = longKey1[i] + password4[i];
    ap--;
}
else if (bp > 0) {
    encryptedPass[i] = longKey1[i] - password4[i];
    bp--;
}
else if (ap == 0 && bp == 0) {
    ap = a;
    bp = b;
    i = i - 1;
}

No more "-858993460" when I encrypt and no more "╠╠╠╠╠╠╠╠" when I printf the password in the reverse process.

Thank you all again, and sorry for the lenght of the original message. Next time I shall take that into account.

cd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM