So I wrote a little handy script which takes in values from players in a encrypted form, and returns a value. Also, it encrypts save data back into the file using a specific key. The script works perfectly in the editor. However, for some reason, when it is used on the android build, it throws these two errors:
- FormatException: Input string was not in the correct format.
- UnauthorizedAccessException: Access to the path "/.config" is denied.
There are no line numbers,sources or anything.
I saw another person had the same problem as me. He added a few extra workspace declarations and it fixed the issue for him. However, it did not work for me.
I am pretty new to crypto so am I making a silly mistake somewhere?
If anyone has any idea, help is greatly appreciated!
Crypt Class:
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.Collections.ObjectModel;
using System.Security;
using System;
public class Crypt : MonoBehaviour {
private string key = "key";
private string val;
// Use this for initialization
void Start(){
}
//prepares string for encryption.
public void toByte (string str, string prefval){
byte[] valBytes = System.Text.Encoding.UTF8.GetBytes (str);
if (true && (!prefval.Equals(""))){
this.Encrypt(valBytes, prefval);
}
}
//Gets saved string ready for decryption.
public string toArray(string prefval){
string temp = PlayerPrefs.GetString (prefval);
string[] test = temp.Split (';');
byte[] bytes = new byte[test.Length];
for (int i = 0; i < test.Length; i++) {
bytes[i] = Byte.Parse(test[i]);
}
return this.Decrypt (bytes);
}
private void Encrypt (byte[] vb, string prefval) {
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = key;
var provider = new RSACryptoServiceProvider(cspParams);
byte[]encryptedBytes = provider.Encrypt(
vb, true);
bool first = true;
val = "";
foreach (byte encryptedByte in encryptedBytes)
{
if(first){
val += encryptedByte;
first = false;
} else {
val += ";" + encryptedByte;
val.Trim();
}
}
PlayerPrefs.SetString (prefval, val);
PlayerPrefs.Save();
Debug.Log (PlayerPrefs.GetString(prefval));
}
public string Decrypt(byte[] ba){
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = key;
var provider = new RSACryptoServiceProvider(cspParams);
string decrypted = System.Text.Encoding.UTF8.GetString(
provider.Decrypt(ba, true));
return decrypted;
}
}
How it is being called:
int sval = int.Parse (c.toArray ("player_high_score"));
if (score >= sval) {
c.toByte (score.ToString(), "player_high_score");
}
text.text = score + " : " + sval;
Thank you for the help in advance!
↧