EDIT: I just had to replace .close with .dispose to fix my problem ;)
To prevent users hacking my game I am attempting to encrypt the save file. The issue is that any attempt to encrypt a file after I have previously decrypted it causes the following error to pop up and prevents me from modifying the file.
![Name: encryptederror.png
Views: 230
Size: 81.5 KB]()
Basically you start the game causing it to load. It decrypts the save file "SKernal.sft" and puts the decrypted text in HDD.dri. Then all the boolean values I have are set to those specified in HDD.dri (this works perfectly!).
You then close the game causing it to save. It sets the boolean values/ saves to "HDD.dri" then encrypts the info and finally saves to "SKernal.sft".
Here is the File_Crypt Class Code:
Here is snippets of code where I call the File_Crypt Class excluding most of the save boolean values:
Please help me work out how to fix this! I've only got 24 hours left before release and after this is worked out I still need to work on one more thing which I won't need help with.
Note: This is just for the purpose of minor protection against average users going to the save location to modify values in notepad. That's why I have just set the key as "Password". I actually tried setting it as "ocean" but it didn't work as said invalid key or something.
Thanks in advance for your help!
To prevent users hacking my game I am attempting to encrypt the save file. The issue is that any attempt to encrypt a file after I have previously decrypted it causes the following error to pop up and prevents me from modifying the file.
Basically you start the game causing it to load. It decrypts the save file "SKernal.sft" and puts the decrypted text in HDD.dri. Then all the boolean values I have are set to those specified in HDD.dri (this works perfectly!).
You then close the game causing it to save. It sets the boolean values/ saves to "HDD.dri" then encrypts the info and finally saves to "SKernal.sft".
Here is the File_Crypt Class Code:
Code:
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Public Class File_Crypt
Public Const sSecretKey As String = "Password"
Public Shared Sub EncryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Dim fsInput As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider()
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
Dim cryptostream As New CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write)
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
fsEncrypted.Close()
cryptostream.Close()
End Sub
Public Shared Sub DecryptFile(ByVal sInputFilename As String, ByVal sOutputFilename As String, ByVal sKey As String)
Dim DES As New DESCryptoServiceProvider()
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
End ClassCode:
Private Sub savegame()
If boughttitlebar = True Then savelines(0) = 11 Else savelines(0) = 10
'code continues but not included here
IO.File.WriteAllLines("C:\ShiftOS\Shiftum42\Drivers\HDD.dri", savelines)
File_Crypt.EncryptFile("C:\ShiftOS\Shiftum42\Drivers\HDD.dri", "C:/ShiftOS/Shiftum42/SKernal.sft", File_Crypt.sSecretKey)
End Sub
Private Sub loadgame()
File_Crypt.DecryptFile("C:/ShiftOS/Shiftum42/SKernal.sft", "C:\ShiftOS\Shiftum42\Drivers\HDD.dri", File_Crypt.sSecretKey)
loadlines = IO.File.ReadAllLines("C:\ShiftOS\Shiftum42\Drivers\HDD.dri")
If loadlines(0) = 11 Then boughttitlebar = True Else boughttitlebar = False
'code continues but not included here
End SubNote: This is just for the purpose of minor protection against average users going to the save location to modify values in notepad. That's why I have just set the key as "Password". I actually tried setting it as "ocean" but it didn't work as said invalid key or something.
Thanks in advance for your help!