Example #1
0
        internal static bool MigrateObscuredStringIfNecessary(SerializedProperty sp)
        {
            var hiddenValueProperty = sp.FindPropertyRelative("hiddenValue");

            if (hiddenValueProperty == null || hiddenValueProperty.arraySize == 0)
            {
                return(false);
            }

            var currentCryptoKeyOldProperty = sp.FindPropertyRelative("currentCryptoKey");

            if (currentCryptoKeyOldProperty == null)
            {
                return(false);
            }

            var currentCryptoKeyOld = currentCryptoKeyOldProperty.stringValue;

            if (string.IsNullOrEmpty(currentCryptoKeyOld))
            {
                return(false);
            }

            var hiddenCharsProperty = sp.FindPropertyRelative("hiddenChars");

            if (hiddenCharsProperty == null || hiddenCharsProperty.arraySize == 0)
            {
                return(false);
            }

            var hiddenValue = ObscuredStringDrawer.GetBytesObsolete(hiddenValueProperty);

            var decrypted = ObscuredString.EncryptDecryptObsolete(ObscuredString.GetStringObsolete(hiddenValue), currentCryptoKeyOld);

            var currentCryptoKey = ObscuredString.GenerateKey();
            var hiddenChars      = ObscuredString.InternalEncryptDecrypt(decrypted.ToCharArray(), currentCryptoKey);


            ObscuredStringDrawer.SetChars(hiddenCharsProperty, hiddenChars);
            var currentCryptoKeyProperty = sp.FindPropertyRelative("cryptoKey");

            ObscuredStringDrawer.SetChars(currentCryptoKeyProperty, currentCryptoKey);

            hiddenValueProperty.arraySize           = 0;
            currentCryptoKeyOldProperty.stringValue = null;

            return(true);
        }
Example #2
0
        private static bool MigrateFromACTkV1Internal(string key, string cryptoKey)
        {
            var oldPrefsKey = Base64Utils.ToBase64(ObscuredString.EncryptDecryptObsolete(key, cryptoKey));

            if (!PlayerPrefs.HasKey(oldPrefsKey))
            {
                return(false);
            }

            SetMigrationMode(true);
            cryptoKeyObsoleteForMigration = cryptoKey;

            var encrypted = PlayerPrefs.GetString(oldPrefsKey);
            var type      = GetRawValueType(encrypted);

            switch (type)
            {
            case DataType.Int:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0, encrypted);
                SetMigrationMode(false);
                SetInt(key, decrypted);
                break;
            }

            case DataType.UInt:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0u, encrypted);
                SetMigrationMode(false);
                SetUInt(key, decrypted);
                break;
            }

            case DataType.String:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, string.Empty, encrypted);
                SetMigrationMode(false);
                SetString(key, decrypted);
                break;
            }

            case DataType.Float:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0f, encrypted);
                SetMigrationMode(false);
                SetFloat(key, decrypted);
                break;
            }

            case DataType.Double:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0d, encrypted);
                SetMigrationMode(false);
                SetDouble(key, decrypted);
                break;
            }

            case DataType.Decimal:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0m, encrypted);
                SetMigrationMode(false);
                SetDecimal(key, decrypted);
                break;
            }

            case DataType.Long:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0L, encrypted);
                SetMigrationMode(false);
                SetLong(key, decrypted);
                break;
            }

            case DataType.ULong:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, 0ul, encrypted);
                SetMigrationMode(false);
                SetULong(key, decrypted);
                break;
            }

            case DataType.Bool:
            {
                var decrypted = DecryptValue(key, oldPrefsKey, false, encrypted);
                SetMigrationMode(false);
                SetBool(key, decrypted);
                break;
            }

            case DataType.ByteArray:
            {
                var decrypted = DecryptByteArrayValue(key, encrypted, 0, 0);
                SetMigrationMode(false);
                SetByteArray(key, decrypted);
                break;
            }

            case DataType.Vector2:
            {
                var decrypted = DecryptVector2Value(key, encrypted, Vector2.zero);
                SetMigrationMode(false);
                SetVector2(key, decrypted);
                break;
            }

            case DataType.Vector3:
            {
                var decrypted = DecryptVector3Value(key, encrypted, Vector3.zero);
                SetMigrationMode(false);
                SetVector3(key, decrypted);
                break;
            }

            case DataType.Quaternion:
            {
                var decrypted = DecryptQuaternionValue(key, encrypted, Quaternion.identity);
                SetMigrationMode(false);
                SetQuaternion(key, decrypted);
                break;
            }

            case DataType.Color:
            {
                var encodedColor = DecryptValue(key, oldPrefsKey, 16777216u, encrypted);
                var decrypted    = DecodeColor(encodedColor);
                SetMigrationMode(false);
                SetColor(key, decrypted);
                break;
            }

            case DataType.Rect:
            {
                var decrypted = DecryptRectValue(key, encrypted, new Rect(0, 0, 0, 0));
                SetMigrationMode(false);
                SetRect(key, decrypted);
                break;
            }

            default:
                Debug.LogWarning(FinalLogPrefix + "Couldn't migrate " + key + " key from ACTk v1 prefs since its type is unknown!");
                return(false);
            }

            Debug.Log(FinalLogPrefix + "Obscured pref " + key + " successfully migrated to the newer format.");

            cryptoKeyObsoleteForMigration = null;
            PlayerPrefs.DeleteKey(oldPrefsKey);
            return(true);
        }
Example #3
0
 /// <summary>
 /// Use to encrypt ACTkv1's value key for later use with SetRawValue to let it migrate.
 /// </summary>
 /// <param name="key">Prefs key.</param>
 /// <param name="cryptoKey">Crypto key you used with ACTk v1, if any.</param>
 /// <returns>Prefs key, encrypted with old ACTk v1 encryption.</returns>
 public static string EncryptKeyWithACTkV1Algorithm(string key, string cryptoKey = "e806f6")
 {
     return(Base64Utils.ToBase64(ObscuredString.EncryptDecryptObsolete(key, cryptoKey)));
 }