Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Onufriyev · Nov 15, 2015 at 10:17 AM · c#androiderror message

Encryption and Playerprefs (RSA)

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bhatiarohan156 · Dec 16, 2015 at 07:05 AM 0
Share

I am also facing the same problem looks like .net support problems

avatar image saschandroid · Dec 16, 2015 at 08:44 AM 1
Share

Have you tried to convert the byte array to a base64 string and save this ins$$anonymous$$d of adding the bytes one by one to a string? like: string val = System.Convert.ToBase64String(encryptedBytes);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Dec 16, 2015 at 09:34 AM

Well, it's pointless to use such a strong encryption since the key is stored in the code, Everyone that actually manages to get access to the playerpref value can easily lookup your key in the code. You can't get any clientside safety against people who actually know what they're doing. Normal users don't have access to the playerprefs. Also they are stored in a binary format so it's already "hard" to read for "normal" people.

Simply using Random with a constant "secret" seed and xor / shift / add to each byte will be as safe as using RSA but doesn't require a bloated security and crypto library.

On topic: Well you most likely have to include the correct assemblies into your project if you haven't yet.

The FormatException comes either from your int.Parse or Byte.Parse line. You don't really check if the playerpref actually exist so an empty string can't be split and can't be parsed into a number.

It's also a quite inefficient way to convert a byte array into a string. It creates a lot of garbage. A StringBuilder would help a bit. Furthermore the val.Trim(); line is pointless. The Trim function returns the trimmed string. A string behaves like an immutable type. You can't call a function on a string and directly change that string. You always have to create a new string.

 val = val.Trim();

That would be correct, however not really necessary since you don't add any whitespace which could be trimmed.

Like @saschandroid said converting to base64 is much easier and the resulting string is even shorter.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Private Field is assigned but its value is never used, but i use it 1 Answer

Touch not changing variables, Keypress does 0 Answers

How to detect if the Device can run the shader ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges