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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Need to crypt a text and decrypt (md5)

Discussion in 'Scripting' started by malak, Aug 29, 2018.

  1. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    Hello everybody and thanks for your time .

    I got a trouble with my script , i can encrypt my text but i dont understand how to decrypt it .

    I encrypt the text with this line of code :
    Code (CSharp):
    1. JSONObject response = new JSONObject (CommUnity.Instance.ReturnResponse(CommUnity.Instance.commUnityURL + "/api.php",
    2. "sendMessage", "username="+CommUnity.Instance.userInfo[0]+
    3. "&password="+CommUnity.Instance.userInfo[1]+
    4. "&id="+id+"
    5. &text="+TypeConversion.Md5Sum(CommUnity.Instance.pwdSalt +
    6. panel[6].transform.Find("Msg").gameObject.GetComponent<InputField>().text)));
    7.  

    to to get the message back i use this line code :
    Code (CSharp):
    1.  inst.transform.Find("News/Background/Text")
    2. .gameObject.GetComponent<Text>().text =
    3. response["Messages"][i]["Text"].str;
    4.  

    Please can you help me to decrypt the text , like this i got the encrypted text .

    Thank for your help i really appreciate and sorry for my bad english i'm french ;)
     
    Last edited: Aug 29, 2018
  2. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    Woah! Firstly you really need to split your code up over several lines. It's impossible to read and debug when it's written like that.

    As to your question - you can't "decrypt" what you're doing. You're using MD5 to "encrypt" your data. MD5 isn't actually encryption, it's a one way hash function. That means that you can't reverse it. If you want to encrypt and decrypt text you'll have to use some kind of cryptographic cipher instead. You could use a simple XOR cipher or public-key encryption.

    Sam
     
  3. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    Ho now i see , thanks sam i will take a look in other encryption decryption methode.
    Do you know a the most simple, basic way ?
     
  4. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    Yep the XOR cipher that I mentioned above is a very simple encryption algorithm. It's also very easy to crack.

    Sam
     
  5. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    How secure do you need it to be? The simplest is base64, but any engineer would immediately recognize it as such and be able to decrypt it. You could probably do a simple substitution cipher, maybe combined with something like base64. This is security through obfuscation.

    You could also do a public key system. It's quite easy, you just need to have a way to have a string of text be used to encrypt and decrypt your values. From there you use a series of messages to establish a secure connection

    Client A generates a key and then encrypts the key using itself, that then gets sent to Client X
    Client B generates a key, receives the encrypted package and encrypts it again using it's own key, then sends it back
    Client A then decrypts the package using it's own key and sends it back
    Client B then takes the package and decrypts it using it's own key.

    Client B now has Client A's randomly generated key and they can communicate using said key. It'll be different every single time, and there is never an unencrypted key sent anywhere.
     
  6. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    I need a very basic encryption something easy to do for a noob , i find something seam to be very easy to encrypt but i still get trouble with the decrypt :(


    here the code to encrypt :
    Code (CSharp):
    1. AvoEx.AesEncryptor.Encrypt( panel[6].transform.Find("Msg").gameObject.GetComponent<InputField>().text)));
    and here the normally code to decrypt :
    Code (CSharp):
    1. AvoEx.AesEncryptor.DecryptString(response["Messages"][i]["Text"].str);
    but i get error when i try decrypt :(

    can you help me ?

    I use AES Encryptor
    https://www.assetstore.unity3d.com/en/?stay#!/content/39501
     
    Last edited: Aug 29, 2018
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Just curious, but why are you trying to deal with encryption/decryption? Are you hooking into your own backend service?

    Also, when you get an error, you should post what error you get.
     
  8. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    hey brathnann , i just like to solve any problem when i'm getting stuck , that's how i learn ;)

    here is the long error code :
    Code (CSharp):
    1. FormatException: Invalid length.
    2. System.Convert.FromBase64String (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Convert.cs:146)
    3. AvoEx.AesEncryptor.DecryptString (System.String encrypted) (at Assets/AvoEx/AesEncryptor/AesEncryptor.cs:121)
    4. Body.ShowMessages () (at Assets/CommUnity/Scripts/Body.cs:446)
    5. Header.MessagesBtn () (at Assets/CommUnity/Scripts/Header.cs:44)
    6. UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
    7. UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
    8. UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
    9. UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
    10. UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
    11. UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
    12. UnityEngine.EventSystems.EventSystem:Update()
     
  9. malak

    malak

    Joined:
    Jan 23, 2014
    Posts:
    65
    Solved with B64R Encode ;)