Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot connect Strings in c#

Discussion in 'Scripting' started by SomehowLuke, Apr 10, 2016.

  1. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Hello.
    I have a problem with connecting strings. I do not really know what the problem is, maybe its something with coding... but I am not absolutely into that

    I have a function that returns a string with following methods:

    char[] charsToTrim = { '*', ' ', '\''};
    return (UTF8Encoding.UTF8.GetString(resultArray)).Trim(charsToTrim);

    So this function returns a string for me and I want to connect it with my own string:

    string test = ReturnString("testString");
    string newString = test + " connectingString";

    Debug.Log(newString);

    But the Debugger just returns the string of test and does not show the " connectingString"

    Can anyone help me with that or do you need more information on the functions?

    Thank you for your help!
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,028
    I tried replicating the problem you encountered given the information you provided but it worked properly for me. You're going to have to post the code (using code tags) you're getting the problem with as your description isn't sufficient.
     
    SomehowLuke likes this.
  3. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Hi and thank you for your fast response:

    So here are my two Crypter functions: (in Class MyCrypter)
    Code (csharp):
    1.  
    2. public static string Encrypter(string toEncrypt)
    3.     {
    4.         byte[] keyArray;
    5.         byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
    6.         byte[] iv = UTF8Encoding.UTF8.GetBytes(siv);
    7.  
    8.  
    9.         string key = skey;
    10.         //System.Windows.Forms.MessageBox.Show(key);
    11.         //If hashing use get hashcode regards to your key
    12.    
    13.         keyArray = UTF8Encoding.UTF8.GetBytes(key);
    14.  
    15.         TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    16.         //set the secret key for the tripleDES algorithm
    17.         tdes.Key = keyArray;
    18.         //mode of operation. there are other 4 modes.
    19.         //We choose ECB(Electronic code Book)
    20.         tdes.Mode = CipherMode.CBC;
    21.         //padding mode(if any extra byte added)
    22.  
    23.         tdes.Padding = PaddingMode.Zeros;
    24.         tdes.IV = iv;
    25.  
    26.         ICryptoTransform cTransform = tdes.CreateEncryptor();
    27.         //transform the specified region of bytes array to resultArray
    28.         byte[] resultArray =
    29.           cTransform.TransformFinalBlock(toEncryptArray, 0,
    30.           toEncryptArray.Length);
    31.         //Release resources held by TripleDes Encryptor
    32.         tdes.Clear();
    33.         //Return the encrypted data into unreadable string format
    34.         return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    35.     }
    36.  
    37.     public static string Decrypter(string cipherString)
    38.     {
    39.         byte[] keyArray;
    40.         //get the byte code of the string
    41.  
    42.         byte[] toEncryptArray = Convert.FromBase64String(cipherString);
    43.         byte[] iv = UTF8Encoding.UTF8.GetBytes(siv);
    44.  
    45.         //Get your key from config file to open the lock!
    46.         string key = skey;
    47.  
    48.    
    49.         //if hashing was not implemented get the byte code of the key
    50.         keyArray = UTF8Encoding.UTF8.GetBytes(key);
    51.    
    52.  
    53.         TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    54.         //set the secret key for the tripleDES algorithm
    55.         tdes.Key = keyArray;
    56.         tdes.IV = iv;
    57.         //mode of operation. there are other 4 modes.
    58.         //We choose ECB(Electronic code Book)
    59.  
    60.         tdes.Mode = CipherMode.CBC;
    61.         //padding mode(if any extra byte added)
    62.         tdes.Padding = PaddingMode.Zeros;
    63.  
    64.         ICryptoTransform cTransform = tdes.CreateDecryptor();
    65.         byte[] resultArray = cTransform.TransformFinalBlock(
    66.                              toEncryptArray, 0, toEncryptArray.Length);
    67.         //Release resources held by TripleDes Encryptor            
    68.         tdes.Clear();
    69.         //return the Clear decrypted TEXT
    70.         char[] charsToTrim = { '*', ' ', '\''};
    71.         return (UTF8Encoding.UTF8.GetString(resultArray)).Trim(charsToTrim);
    72.     }
    73.  
    and here is my testscript:

    Code (csharp):
    1.  
    2. string first = "first";
    3. string second = "second";
    4. string firstEncrypt = MyCrypter.Encrypter (first);
    5. string firstDecrypt = MyCrypter.Decrypter (firstEncrypt);
    6. string secondEncrypt = MyCrypter.Encrypter (second);
    7. string secondDecrypt = MyCrypter.Decrypter (secondEncrypt);
    8. string debug1 = first + second;
    9. string debug2 = firstDecrypt + secondDecrypt;
    10. string debug3 = firstDecrypt + second;
    11.  
    12. Debug.Log (debug1); // firstsecond
    13. Debug.Log (debug2); //first
    14. Debug.Log (debug3); //first
    15.  
    Thank you again!
     
    Last edited: Apr 10, 2016
  4. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Sorry forgot the code tags but I edited it :)
     
  5. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    Hi again, I also tried it with
    Code (csharp):
    1. string debug4 = string.Concat (firstDecrypt, secondDecrypt);
    but that doesnt work either. So it must be something with the return string from my crypt functions... Any ideas?
     
  6. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    After some investigation I found out that the Decrypted string has a length of 8 but the word first should be 6 long. So there are some hidden strings which I cannnot see in the debugger what probably makes that problem. Any ieads how to get rid of the "extra" strings
     
  7. SomehowLuke

    SomehowLuke

    Joined:
    Nov 11, 2015
    Posts:
    34
    SOLVED:

    After a long run of investigation I finally found the obvious problem...(as usual :D )

    I forgot to trim the 0 bytes at the returning UTF8 string. so changing

    Code (csharp):
    1. char[] charsToTrim ={'*', ' ', '\''};
    to


    Code (csharp):
    1. char[] charsToTrim ={'*', ' ', '\'', '\0'};
    will fix that problem and strings can be connected