Search Unity

Trying to encrypt a save file crashes system

Discussion in 'Editor & General Support' started by knobby67, Mar 29, 2019.

  1. knobby67

    knobby67

    Joined:
    Aug 30, 2015
    Posts:
    389
    Hi All,
    I',m trying to use System.Security.Cryptography to read write a binaryformatted file. However when I try to read it crashes game ( no error appears in console ). How I read the file is
    Code (csharp):
    1.  
    2.         CryptoStream enc = new CryptoStream( file, unencryptor, CryptoStreamMode.Read );
    3.  
    4.         //T x = ( T )this.bf.Deserialize( file );
    5.  
    6.         T x = ( T )this.bf.Deserialize( enc );
    7.  
    8.         return( x );
    9.     }
    10.  
    however when I Serialize the output this."bf.Serialize( enc, testObject );" it crahses the game.
    Can anyone advise please, I've searched google without luck.
    The whole code is below

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.IO;
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using System.Security.Cryptography;
    8. public class FileControl
    9. {
    10.     BinaryFormatter bf;
    11.     RijndaelManaged rm;
    12.  
    13.     ICryptoTransform encryptor,unencrytor;
    14.     FileStream file;
    15.     string filepath;
    16.     byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    17.                 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14,
    18.                 0x15, 0x16};
    19.     byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    20.                 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14,
    21.                 0x15, 0x16};
    22.     public void setSavePath( string game_path )
    23.     {
    24.         this.filepath = Application.persistentDataPath + "/" + game_path;
    25.     }
    26.     public bool openFile( string game_path )
    27.     {
    28.         this.setSavePath( game_path );
    29.         this.bf = new BinaryFormatter( );
    30.         rm = new RijndaelManaged();
    31.      
    32.         encryptor = rm.CreateEncryptor(Key, IV);
    33.         unencrytor = rm.CreateDecryptor(Key, IV);
    34.  
    35.         if( !File.Exists( filepath ) )
    36.         {
    37.             this.file = File.Create( filepath );
    38.             this.file.Flush( );
    39.             return( false );
    40.         }
    41.         else file = File.Open( filepath, FileMode.Open );
    42.         this.file.Flush( );
    43.         return( true );
    44.     }
    45.     public void closeFile( )
    46.     {
    47.         if( this.file != null )this.file.Close( );
    48.     }
    49.     public void writeFile< T >( T testObject )
    50.     {
    51.         file.Seek(0, SeekOrigin.Begin);
    52.         CryptoStream enc = new CryptoStream(file,  encryptor, CryptoStreamMode.Write );
    53.         //this.bf.Serialize( file, testObject );
    54.         this.bf.Serialize( enc, testObject );
    55.         this.file.Flush( );
    56.         //this.file.Close( );
    57.     }
    58.     public T readFile< T >(  )
    59.     {
    60.         file.Seek(0, SeekOrigin.Begin);
    61.         CryptoStream enc = new CryptoStream( file, unencryptor, CryptoStreamMode.Read );
    62.         //T x = ( T )this.bf.Deserialize( file );
    63.         T x = ( T )this.bf.Deserialize( enc );
    64.         return( x );
    65.     }
    66. }
    67.  
     
    Last edited: Mar 29, 2019