Results 1 to 6 of 6

Thread: [2005] Saving important data

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2005] Saving important data

    I am familiar with saving data such as strings and ints to XML files and such but what if the data is important? Such as an int representing what level the character is on in your game? You cannot simply put that in an XML file. Encryption comes to mind but really, whatever you come up with someone will hack and exploit. Also, you must deal with decryption and is really a waste IMO. Surely there must be a better way?

    I was thinking about storing the values IN the exe. Would such a thing be fine for this sort of thing? Also, decompiling DLL's seems to be almost impossible. Should I just write and read to and from a DLL?

    (I'm not going to have a server so please don't suggest it)

    I'd really like to hear some nice, clean suggestions on performing such a task as I don't really want 10 seperate classes just for writing and reading what level a character is on.

    I'm sure other people have encountered this problem before? What did you do?

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Saving important data

    Millions of software applications and computer users the world over use encryption all the time. Maybe you just don't understand it properly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] Saving important data

    I have good and simple codes, comes with encryption, and decryption...

    all you do is

    Public Wrapper As New Simple3Des("PasswordForUniqueness")

    Code:
    Dim EncryptText As String = Wrapper.Encrypt("mytext")
    Dim DecryptText As String = Wrapper.Decrypt(EncryptText)
    (and if you want to make it even simpler, you can create a function to read and write to a stream/file, so that you don't have to keep calling Wrapper.Encrypt etc..
    It's just
    Code:
    WriteToStream(FileStream, Text)
    The code is this:

    (Note I have made a modification to the Encrypt/Decrypt function to make sure the length of the value provided is greater than 0)

    vb.net Code:
    1. #Region " [Cryptography] "
    2.     Public NotInheritable Class Simple3Des
    3.         Private TripleDes As New TripleDESCryptoServiceProvider
    4.  
    5.         Sub New(ByVal key As String)
    6.             ' Initialize the crypto provider.
    7.             TripleDes.Key = TruncateHash(key, TripleDes.KeySize \ 8)
    8.             TripleDes.IV = TruncateHash("", TripleDes.BlockSize \ 8)
    9.         End Sub
    10.  
    11.         Private Function TruncateHash( _
    12. ByVal key As String, _
    13. ByVal length As Integer) _
    14. As Byte()
    15.  
    16.             Dim sha1 As New SHA1CryptoServiceProvider
    17.  
    18.             ' Hash the key.
    19.             Dim keyBytes() As Byte = _
    20.                 System.Text.Encoding.Unicode.GetBytes(key)
    21.             Dim hash() As Byte = sha1.ComputeHash(keyBytes)
    22.  
    23.             ' Truncate or pad the hash.
    24.             ReDim Preserve hash(length - 1)
    25.             Return hash
    26.         End Function
    27.  
    28.         Public Function EncryptData( _
    29.         ByVal plaintext As String) _
    30.         As String
    31.             If plaintext.Length > 0 Then
    32.  
    33.                 ' Convert the plaintext string to a byte array.
    34.                 Dim plaintextBytes() As Byte = _
    35.                     System.Text.Encoding.Unicode.GetBytes(plaintext)
    36.  
    37.                 ' Create the stream.
    38.                 Dim ms As New System.IO.MemoryStream
    39.                 ' Create the encoder to write to the stream.
    40.                 Dim encStream As New CryptoStream(ms, _
    41.                     TripleDes.CreateEncryptor(), _
    42.                     System.Security.Cryptography.CryptoStreamMode.Write)
    43.  
    44.                 ' Use the crypto stream to write the byte array to the stream.
    45.                 encStream.Write(plaintextBytes, 0, plaintextBytes.Length)
    46.                 encStream.FlushFinalBlock()
    47.  
    48.                 ' Convert the encrypted stream to a printable string.
    49.                 Return Convert.ToBase64String(ms.ToArray)
    50.             Else
    51.                 Return ""
    52.             End If
    53.         End Function
    54.  
    55.         Public Function DecryptData( _
    56.         ByVal encryptedtext As String) _
    57.         As String
    58.             If encryptedtext.Length > 0 Then
    59.  
    60.                 ' Convert the encrypted text string to a byte array.
    61.                 Dim encryptedBytes() As Byte = Convert.FromBase64String(encryptedtext)
    62.  
    63.                 ' Create the stream.
    64.                 Dim ms As New System.IO.MemoryStream
    65.                 ' Create the decoder to write to the stream.
    66.                 Dim decStream As New CryptoStream(ms, _
    67.                     TripleDes.CreateDecryptor(), _
    68.                     System.Security.Cryptography.CryptoStreamMode.Write)
    69.  
    70.                 ' Use the crypto stream to write the byte array to the stream.
    71.                 decStream.Write(encryptedBytes, 0, encryptedBytes.Length)
    72.                 decStream.FlushFinalBlock()
    73.  
    74.                 ' Convert the plaintext stream to a string.
    75.                 Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
    76.             Else
    77.                 Return ""
    78.             End If
    79.         End Function
    80.     End Class
    81. #End Region

    Just import these:
    Code:
    Imports System
    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Char
    Imports System.IO
    Imports System.Security
    Imports System.Security.Cryptography
    Imports System.Text
    (Some are not necessary, but the Cryptography one is the important one I think)

    Cheers

  4. #4
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [2005] Saving important data

    If it's important encrypt it. There are encryption methods out there now that take years for programs to decrypt without the correct method.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  5. #5
    Addicted Member
    Join Date
    Sep 2006
    Location
    Surabaya, Indonesia
    Posts
    163

    Re: [2005] Saving important data

    If you want the easiest but non secure way, just use XML files, but rename it to something unfamiliar (ex: data.wll, xyz.abc). Therefore, they wouldn't know what file is about unless they curious and open it in a text editor.

    If you want a secure way, you should use encryption. .NET provide encryption technology in security namespace.
    Check my Blog at VB Corner,Component Crafts

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2005] Saving important data

    Alright, I suppose I will use encryption. Thanks everyone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width