Search Unity

Question How exactly can I encrypt a script of my game?

Discussion in 'General Discussion' started by dnzblg58, Mar 30, 2023.

  1. dnzblg58

    dnzblg58

    Joined:
    Jul 6, 2018
    Posts:
    15
    Hello, I made a game and I plan to sell this game with its source codes. However, I want to encrypt a file or script in my game. Is this possible?
     
  2. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,698
    Not truly encrypted. So that a game runs on the computer it needs to be decrypted. So you'd have to send the encryption key alongside it which renders that aproach pointless.
    Instead there is a form of inherent one-way encryption through compilation which turns C# into machine specific code. It's possible to compile C# to a Dll and then use it externally.
    Note that this complicates distribution for different platforms and due to the fact that all your game content has to be processed by the hardware, there unfortunately always are loopholes (hope you are not considering to store precious information like server access data in that file).
    If it's just to prevent most people from "stealing your code", that is enough.
     
    dnzblg58 likes this.
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,947
    If it's about some kind of access token or account login information that's personalized to you, you must wipe that data and make sure in the instructions that users understand that and how to set up their own token/account. Code should check for unset token/login data to inform users who don't RTFM.
     
    dnzblg58 likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
    Put it on the server.

    You can not protect any files from being viewed, decompiled, unencrypted, once they land on local machine.
    Specially if your application need to read it anyway.
    You may make it harder to some, but someone eventually crack it anyway.
    You will spend amount of time and resources, to make a encryption feature, which become soon obsolete, as someone will decrypt them and possibly make such assets public. Not worth an effort.
     
    dnzblg58 likes this.
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,439
    It's not possible. You have to give an executable to an untrusted client.

    Always assume that the kind of person who wants to steal your code or cheat at your game has the equipment to make it trivial to do so. They have Administrator/root. They have debuggers that can stop processes. They have memory chip dumping hardware. They have code analysis tools that will give them all your subroutines, albeit with funny names for variables. They have time and patience and bosses who want to clone your game because it's cheaper and more certain than making their own.

    You may trust the company you're selling this game to honor a contract that says the DLL is to be used as-is and remains your intellectual property. That's all on the honor system with legal documentation as your only real protection.
     
    dnzblg58 and Ryiah like this.
  6. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,175
    Yes, but it's always a temporary measure and never a permanent solution. You can obfuscate source code which will rearrange the code, rename the methods and variables, and so on, but having had to work with obfuscated code in the past let me assure you it's not hard for someone to change the code or extract information.

    File encryption is just a temporary measure too because at some point the data has to be decrypted in order for the hardware to be able to use it. Extracting information straight out of system and video memory is a trivial task with tools that you can download straight off of Github to do the work.
     
    Last edited: Mar 30, 2023
    dnzblg58 likes this.
  7. dnzblg58

    dnzblg58

    Joined:
    Jul 6, 2018
    Posts:
    15