2

I got a strange problem in here. I have two string to compare.

Lets call it :

String "A" and String "B".

Both of string have a same data

"A" : "1234" and "B" : "1234"

But when compare they didn't same or not equal.

I have try to trim both of string. But the result still not equal.

Then i try to check the string length.

"A" contain 4, but "B" contain 16.

So it is mean that

string "A" has length 4 and string "B" has length 16.

How it can be like this ?

So now i cannot compare it.

For note. String "B" data is a decrypt data from database. But the length became 16.

How i can solve this ? or return string "B" to normal.

Thank You

Edit :

I put the screenshot try see this

enter image description here

And this is the code i use :

if (Input_PIN.text.Length >= 4)
        {
            Debug.Log("A Text : " + Input_PIN.text);
            Debug.Log("A Lenght : " + Input_PIN.text.Length);
            Debug.Log("B Text : " + user_account_detail.Pin);
            Debug.Log("B Lenght : " + user_account_detail.Pin.Length);

            if (Input_PIN.text.Trim() == user_account_detail.Pin.Trim())
            {
                UnityEngine.SceneManagement.SceneManager.LoadScene("scene_foundation");
            }
            else
            {
                UtilityScript.GetComponent<utility>().MessageBox_Pin_Wrong();
                Input_PIN.text = "";
            }

        }

And this is the decrpyt code i use for string "B" :

public static string Decrypt(string prm_text_to_decrypt, string prm_key, string prm_iv)
    {

        var sEncryptedString = prm_text_to_decrypt;

        var rj = new RijndaelManaged()
        {
            Padding = PaddingMode.PKCS7,
            Mode = CipherMode.CBC,
            KeySize = 256,
            BlockSize = 128,
        };

        var key = Convert.FromBase64String(prm_key);
        var IV = Convert.FromBase64String(prm_iv);

        var decryptor = rj.CreateDecryptor(key, IV);

        var sEncrypted = Convert.FromBase64String(sEncryptedString);

        var fromEncrypt = new byte[sEncrypted.Length];

        var msDecrypt = new MemoryStream(sEncrypted);
        var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

        csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

        return (Encoding.ASCII.GetString(fromEncrypt));
    }
6
  • 1
    There is only one explanation, they don't have the same data, this is only your assumption, prove it to your self by debugging it Commented Mar 7, 2019 at 4:50
  • @MichaelRandall yes i use debug.log and the result was same "1234" and "1234" Commented Mar 7, 2019 at 4:52
  • Is this because the decryption data ? I even debug.log both of data before compare them.. But the result was same. But the length didn't same Commented Mar 7, 2019 at 4:52
  • @MichaelRandall check that screenshot. That's the two data compare. And that's i debug.log before compare. Commented Mar 7, 2019 at 4:59
  • 1
    Clearly they aren't the same, because the length of A is 4 and the length of B is 16. Commented Mar 7, 2019 at 5:03

1 Answer 1

4

My guess is you have misread an array of byte and ended up with Null Characters '\0' at the end of a string.

var pin1 = Input_PIN.text.Trim('\0');
var pin2 = user_account_detail.Pin.Trim('\0');

Update

That's correct. I trim with ('\0') and it is worked now. But why i need to put ('\0') in trim ?

Because Trim() doesn't deal with null characters, you have to specify them explicitly

However, the design problem is why you have them in the first place

Sign up to request clarification or add additional context in comments.

1 Comment

That's correct. I trim with ('\0') and it is worked now. But why i need to put ('\0') in trim ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.