.NET Terraria Encrypt/Decrypt

Discussion in 'Source Code & Tutorial Database' started by Visual Studio, Mar 26, 2013 with 13 replies and 3,263 views.

  1. Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,340
    If anyone plans to work on anything related to save modding I have made a version of the application that allows the loading of decrypted files, If you want it PM me.

    The encryption and decryption functions for Terraria:
    Code:
    private static void encryptFile(string inputFile, string outputFile)
    {
    string s = "h3y_gUyZ";
    UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
    byte[] bytes = unicodeEncoding.GetBytes(s);
    FileStream fileStream = new FileStream(outputFile, FileMode.Create);
    RijndaelManaged rijndaelManaged = new RijndaelManaged();
    CryptoStream cryptoStream = new CryptoStream(fileStream, rijndaelManaged.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
    FileStream fileStream2 = new FileStream(inputFile, FileMode.Open);
    int num;
    while ((num = fileStream2.ReadByte()) != -1)
    {
    cryptoStream.WriteByte((byte)num);
    }
    fileStream2.Close();
    cryptoStream.Close();
    fileStream.Close();
    }
    
    Code:
    private static bool decryptFile(string inputFile, string outputFile)
    {
    string s = "h3y_gUyZ";
    UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
    byte[] bytes = unicodeEncoding.GetBytes(s);
    FileStream fileStream = new FileStream(inputFile, FileMode.Open);
    RijndaelManaged rijndaelManaged = new RijndaelManaged();
    CryptoStream cryptoStream = new CryptoStream(fileStream, rijndaelManaged.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
    FileStream fileStream2 = new FileStream(outputFile, FileMode.Create);
    try
    {
    int num;
    while ((num = cryptoStream.ReadByte()) != -1)
    {
    fileStream2.WriteByte((byte)num);
    }
    fileStream2.Close();
    cryptoStream.Close();
    fileStream.Close();
    }
    catch
    {
    fileStream2.Close();
    fileStream.Close();
    File.Delete(outputFile);
    return true;
    }
    return false;
    }
    
    Code:
    public static void SavePlayer(Player newPlayer, string playerPath)
    {
    try
    {
    Directory.CreateDirectory(Main.PlayerPath);
    }
    catch
    {
    }
    if (playerPath == null || playerPath == "")
    {
    return;
    }
    string destFileName = playerPath + ".bak";
    if (File.Exists(playerPath))
    {
    File.Copy(playerPath, destFileName, true);
    }
    string text = playerPath + ".dat";
    using (FileStream fileStream = new FileStream(text, FileMode.Create))
    {
    using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
    {
    binaryWriter.Write(Main.curRelease);
    binaryWriter.Write(newPlayer.name);
    binaryWriter.Write(newPlayer.difficulty);
    binaryWriter.Write(newPlayer.hair);
    binaryWriter.Write(newPlayer.male);
    binaryWriter.Write(newPlayer.statLife);
    binaryWriter.Write(newPlayer.statLifeMax);
    binaryWriter.Write(newPlayer.statMana);
    binaryWriter.Write(newPlayer.statManaMax);
    binaryWriter.Write(newPlayer.hairColor.R);
    binaryWriter.Write(newPlayer.hairColor.G);
    binaryWriter.Write(newPlayer.hairColor.B);
    binaryWriter.Write(newPlayer.skinColor.R);
    binaryWriter.Write(newPlayer.skinColor.G);
    binaryWriter.Write(newPlayer.skinColor.B);
    binaryWriter.Write(newPlayer.eyeColor.R);
    binaryWriter.Write(newPlayer.eyeColor.G);
    binaryWriter.Write(newPlayer.eyeColor.B);
    binaryWriter.Write(newPlayer.shirtColor.R);
    binaryWriter.Write(newPlayer.shirtColor.G);
    binaryWriter.Write(newPlayer.shirtColor.B);
    binaryWriter.Write(newPlayer.underShirtColor.R);
    binaryWriter.Write(newPlayer.underShirtColor.G);
    binaryWriter.Write(newPlayer.underShirtColor.B);
    binaryWriter.Write(newPlayer.pantsColor.R);
    binaryWriter.Write(newPlayer.pantsColor.G);
    binaryWriter.Write(newPlayer.pantsColor.B);
    binaryWriter.Write(newPlayer.shoeColor.R);
    binaryWriter.Write(newPlayer.shoeColor.G);
    binaryWriter.Write(newPlayer.shoeColor.B);
    for (int i = 0; i < 11; i++)
    {
    if (newPlayer.armor[i].name == null)
    {
    newPlayer.armor[i].name = "";
    }
    binaryWriter.Write(newPlayer.armor[i].netID);
    binaryWriter.Write(newPlayer.armor[i].prefix);
    }
    for (int j = 0; j < 48; j++)
    {
    if (newPlayer.inventory[j].name == null)
    {
    newPlayer.inventory[j].name = "";
    }
    binaryWriter.Write(newPlayer.inventory[j].netID);
    binaryWriter.Write(newPlayer.inventory[j].stack);
    binaryWriter.Write(newPlayer.inventory[j].prefix);
    }
    for (int k = 0; k < Chest.maxItems; k++)
    {
    if (newPlayer.bank[k].name == null)
    {
    newPlayer.bank[k].name = "";
    }
    binaryWriter.Write(newPlayer.bank[k].netID);
    binaryWriter.Write(newPlayer.bank[k].stack);
    binaryWriter.Write(newPlayer.bank[k].prefix);
    }
    for (int l = 0; l < Chest.maxItems; l++)
    {
    if (newPlayer.bank2[l].name == null)
    {
    newPlayer.bank2[l].name = "";
    }
    binaryWriter.Write(newPlayer.bank2[l].netID);
    binaryWriter.Write(newPlayer.bank2[l].stack);
    binaryWriter.Write(newPlayer.bank2[l].prefix);
    }
    for (int m = 0; m < 10; m++)
    {
    binaryWriter.Write(newPlayer.buffType[m]);
    binaryWriter.Write(newPlayer.buffTime[m]);
    }
    for (int n = 0; n < 200; n++)
    {
    if (newPlayer.spN[n] == null)
    {
    binaryWriter.Write(-1);
    break;
    }
    binaryWriter.Write(newPlayer.spX[n]);
    binaryWriter.Write(newPlayer.spY[n]);
    binaryWriter.Write(newPlayer.spI[n]);
    binaryWriter.Write(newPlayer.spN[n]);
    }
    binaryWriter.Write(newPlayer.hbLocked);
    binaryWriter.Close();
    }
    }
    Player.EncryptFile(text, playerPath);
    File.Delete(text);
    }
    
    Code:
    public static Player LoadPlayer(string playerPath)
    {
    bool flag = false;
    if (Main.rand == null)
    {
    Main.rand = new Random((int)DateTime.Now.Ticks);
    }
    Player player = new Player();
    try
    {
    string text = playerPath + ".dat";
    flag = Player.DecryptFile(playerPath, text);
    if (!flag)
    {
    using (FileStream fileStream = new FileStream(text, FileMode.Open))
    {
    using (BinaryReader binaryReader = new BinaryReader(fileStream))
    {
    int num = binaryReader.ReadInt32();
    player.name = binaryReader.ReadString();
    if (num >= 10)
    {
    if (num >= 17)
    {
    player.difficulty = binaryReader.ReadByte();
    }
    else
    {
    bool flag2 = binaryReader.ReadBoolean();
    if (flag2)
    {
    player.difficulty = 2;
    }
    }
    }
    player.hair = binaryReader.ReadInt32();
    if (num <= 17)
    {
    if (player.hair == 5 || player.hair == 6 || player.hair == 9 || player.hair == 11)
    {
    player.male = false;
    }
    else
    {
    player.male = true;
    }
    }
    else
    {
    player.male = binaryReader.ReadBoolean();
    }
    player.statLife = binaryReader.ReadInt32();
    player.statLifeMax = binaryReader.ReadInt32();
    if (player.statLifeMax > 400)
    {
    player.statLifeMax = 400;
    }
    if (player.statLife > player.statLifeMax)
    {
    player.statLife = player.statLifeMax;
    }
    player.statMana = binaryReader.ReadInt32();
    player.statManaMax = binaryReader.ReadInt32();
    if (player.statManaMax > 200)
    {
    player.statManaMax = 200;
    }
    if (player.statMana > 400)
    {
    player.statMana = 400;
    }
    player.hairColor.R = binaryReader.ReadByte();
    player.hairColor.G = binaryReader.ReadByte();
    player.hairColor.B = binaryReader.ReadByte();
    player.skinColor.R = binaryReader.ReadByte();
    player.skinColor.G = binaryReader.ReadByte();
    player.skinColor.B = binaryReader.ReadByte();
    player.eyeColor.R = binaryReader.ReadByte();
    player.eyeColor.G = binaryReader.ReadByte();
    player.eyeColor.B = binaryReader.ReadByte();
    player.shirtColor.R = binaryReader.ReadByte();
    player.shirtColor.G = binaryReader.ReadByte();
    player.shirtColor.B = binaryReader.ReadByte();
    player.underShirtColor.R = binaryReader.ReadByte();
    player.underShirtColor.G = binaryReader.ReadByte();
    player.underShirtColor.B = binaryReader.ReadByte();
    player.pantsColor.R = binaryReader.ReadByte();
    player.pantsColor.G = binaryReader.ReadByte();
    player.pantsColor.B = binaryReader.ReadByte();
    player.shoeColor.R = binaryReader.ReadByte();
    player.shoeColor.G = binaryReader.ReadByte();
    player.shoeColor.B = binaryReader.ReadByte();
    Main.player[Main.myPlayer].shirtColor = player.shirtColor;
    Main.player[Main.myPlayer].pantsColor = player.pantsColor;
    Main.player[Main.myPlayer].hairColor = player.hairColor;
    if (num >= 38)
    {
    for (int i = 0; i < 11; i++)
    {
    player.armor[i].netDefaults(binaryReader.ReadInt32());
    player.armor[i].Prefix((int)binaryReader.ReadByte());
    }
    for (int j = 0; j < 48; j++)
    {
    player.inventory[j].netDefaults(binaryReader.ReadInt32());
    player.inventory[j].stack = binaryReader.ReadInt32();
    player.inventory[j].Prefix((int)binaryReader.ReadByte());
    }
    for (int k = 0; k < Chest.maxItems; k++)
    {
    player.bank[k].netDefaults(binaryReader.ReadInt32());
    player.bank[k].stack = binaryReader.ReadInt32();
    player.bank[k].Prefix((int)binaryReader.ReadByte());
    }
    for (int l = 0; l < Chest.maxItems; l++)
    {
    player.bank2[l].netDefaults(binaryReader.ReadInt32());
    player.bank2[l].stack = binaryReader.ReadInt32();
    player.bank2[l].Prefix((int)binaryReader.ReadByte());
    }
    }
    else
    {
    for (int m = 0; m < 8; m++)
    {
    player.armor[m].SetDefaults(Item.VersionName(binaryReader.ReadString(), num));
    if (num >= 36)
    {
    player.armor[m].Prefix((int)binaryReader.ReadByte());
    }
    }
    if (num >= 6)
    {
    for (int n = 8; n < 11; n++)
    {
    player.armor[n].SetDefaults(Item.VersionName(binaryReader.ReadString(), num));
    if (num >= 36)
    {
    player.armor[n].Prefix((int)binaryReader.ReadByte());
    }
    }
    }
    for (int num2 = 0; num2 < 44; num2++)
    {
    player.inventory[num2].SetDefaults(Item.VersionName(binaryReader.ReadString(), num));
    player.inventory[num2].stack = binaryReader.ReadInt32();
    if (num >= 36)
    {
    player.inventory[num2].Prefix((int)binaryReader.ReadByte());
    }
    }
    if (num >= 15)
    {
    for (int num3 = 44; num3 < 48; num3++)
    {
    player.inventory[num3].SetDefaults(Item.VersionName(binaryReader.ReadString(), num));
    player.inventory[num3].stack = binaryReader.ReadInt32();
    if (num >= 36)
    {
    player.inventory[num3].Prefix((int)binaryReader.ReadByte());
    }
    }
    }
    for (int num4 = 0; num4 < Chest.maxItems; num4++)
    {
    player.bank[num4].SetDefaults(Item.VersionName(binaryReader.ReadString(), num));
    player.bank[num4].stack = binaryReader.ReadInt32();
    if (num >= 36)
    {
    player.bank[num4].Prefix((int)binaryReader.ReadByte());
    }
    }
    if (num >= 20)
    {
    for (int num5 = 0; num5 < Chest.maxItems; num5++)
    {
    player.bank2[num5].SetDefaults(Item.VersionName(binaryReader.ReadString(), num));
    player.bank2[num5].stack = binaryReader.ReadInt32();
    if (num >= 36)
    {
    player.bank2[num5].Prefix((int)binaryReader.ReadByte());
    }
    }
    }
    }
    if (num >= 11)
    {
    for (int num6 = 0; num6 < 10; num6++)
    {
    player.buffType[num6] = binaryReader.ReadInt32();
    player.buffTime[num6] = binaryReader.ReadInt32();
    }
    }
    for (int num7 = 0; num7 < 200; num7++)
    {
    int num8 = binaryReader.ReadInt32();
    if (num8 == -1)
    {
    break;
    }
    player.spX[num7] = num8;
    player.spY[num7] = binaryReader.ReadInt32();
    player.spI[num7] = binaryReader.ReadInt32();
    player.spN[num7] = binaryReader.ReadString();
    }
    if (num >= 16)
    {
    player.hbLocked = binaryReader.ReadBoolean();
    }
    binaryReader.Close();
    }
    }
    player.PlayerFrame();
    File.Delete(text);
    Player result = player;
    return result;
    }
    }
    catch
    {
    flag = true;
    }
    if (flag)
    {
    try
    {
    string text2 = playerPath + ".bak";
    Player result;
    if (File.Exists(text2))
    {
    File.Delete(playerPath);
    File.Move(text2, playerPath);
    result = Player.LoadPlayer(playerPath);
    return result;
    }
    result = new Player();
    return result;
    }
    catch
    {
    Player result = new Player();
    return result;
    }
    }
    return new Player();
    }
    
    Info:
    • Current Life = 0x12 (Int32)
    • Max Life = 0x16 (Int32) - Max is 400 or 190 in Hex
    • Current Mana = 0x1A (Int32)
    • Max Mana = 0x1E (Int32) - Max is 200 or C8 in Hex

     
    Last edited: Mar 30, 2013
  2. Z61

    Z61 Some times our saints are sinners Retired

    Messages:
    5,514
    Ratings:
    3,057
    Nice job!
    I may have to try this at some point to keep my self from sucking at loading files.
     
  3. XBLToothPik

    XBLToothPik Contributor

    Messages:
    583
    Ratings:
    911
    Cool, might help even more when the Xbox version is released; if they don't change the encryption methods or the save layout, I'm sure it will be similar though.
     
  4. OP
    Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,340
    Well if it is an Indie game then it should be the same but if it is an arcade game then they will change it.
     
  5. Z61

    Z61 Some times our saints are sinners Retired

    Messages:
    5,514
    Ratings:
    3,057
    It's an arcade.
     
  6. OP
    Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,340
    Yeah, it will be different then :/

    EDIT: It isn't different XD
     
    Last edited: Mar 31, 2013
  7. fat pat 666

    fat pat 666 Lucifixture

    Messages:
    10,580
    Ratings:
    4,168
  8. Jbro129

    Jbro129 Newbie

    Messages:
    4
    Ratings:
    0
    Do you still have the tool? If so can you please send it to me? :smile:
     
  9. OP
    Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,340
    I do and I can.
     
  10. Jbro129

    Jbro129 Newbie

    Messages:
    4
    Ratings:
    0
    thank you :smile:
    so can you dm me a download?
     
  11. Jbro129

    Jbro129 Newbie

    Messages:
    4
    Ratings:
    0
    Can you please dm it to me?
     
  12. OP
    Visual Studio

    Visual Studio The Original Shiba Inu

    Messages:
    2,766
    Ratings:
    1,340
    I can, I forgot, I'll do it in a few minutes.
     
  13. Jbro129

    Jbro129 Newbie

    Messages:
    4
    Ratings:
    0
    ok thank you
     
  14. mimossss

    mimossss Newbie

    Messages:
    1
    Ratings:
    0
    hello,I have used your decryption code,but it does not work. Is your code open source? if so, can you please send it to me? my Terraira version is 1.3(pc)
     

Share This Page