Dennis Liu
Dennis Liu

Reputation: 2338

String Length Not Equal To String Data Unity C#

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));
    }

Upvotes: 2

Views: 920

Answers (1)

TheGeneral
TheGeneral

Reputation: 81493

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

Upvotes: 4

Related Questions