Пример #1
0
        internal static char[] GetCryptoKey(string dynamicSuffix = null)
        {
            if (generatedCryptoKey == null)
            {
                var savedKey = PlayerPrefs.GetString(PrefsKey);
                if (!string.IsNullOrEmpty(savedKey))
                {
                    generatedCryptoKey = Base64Utils.FromBase64ToChars(savedKey);
                }
                else
                {
                    generatedCryptoKey = ObscuredString.GenerateKey();
                    var b64 = Base64Utils.ToBase64(generatedCryptoKey);
                    PlayerPrefs.SetString(PrefsKey, b64);
                    PlayerPrefs.Save();
                }
            }

            if (string.IsNullOrEmpty(dynamicSuffix))
            {
                return(generatedCryptoKey);
            }

            var suffixChars = dynamicSuffix.ToCharArray();
            var result      = new char[generatedCryptoKey.Length + suffixChars.Length];

            Buffer.BlockCopy(generatedCryptoKey, 0, result, 0, generatedCryptoKey.Length);
            Buffer.BlockCopy(suffixChars, 0, result, generatedCryptoKey.Length + 1, suffixChars.Length);

            return(result);
        }
Пример #2
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);
        }
Пример #3
0
        public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
        {
            MigrateUtils.MigrateObscuredStringIfNecessary(prop);

            var hiddenChars = prop.FindPropertyRelative("hiddenChars");

            var cryptoKey       = prop.FindPropertyRelative("cryptoKey");
            var inited          = prop.FindPropertyRelative("inited");
            var fakeValue       = prop.FindPropertyRelative("fakeValue");
            var fakeValueActive = prop.FindPropertyRelative("fakeValueActive");

            var currentCryptoKey = GetChars(cryptoKey);

            var val = string.Empty;

            if (!inited.boolValue)
            {
                if (currentCryptoKey.Length == 0)
                {
                    currentCryptoKey = ObscuredString.GenerateKey();
                    SetChars(cryptoKey, currentCryptoKey);
                }
                inited.boolValue = true;
                EncryptAndSetChars(val.ToCharArray(), hiddenChars, currentCryptoKey);
                fakeValue.stringValue = val;
            }
            else
            {
                var size      = hiddenChars.FindPropertyRelative("Array.size");
                var showMixed = size.hasMultipleDifferentValues;

                if (!showMixed)
                {
                    for (var i = 0; i < hiddenChars.arraySize; i++)
                    {
                        showMixed |= hiddenChars.GetArrayElementAtIndex(i).hasMultipleDifferentValues;
                        if (showMixed)
                        {
                            break;
                        }
                    }
                }

                if (!showMixed)
                {
                    var chars = new char[hiddenChars.arraySize];
                    for (var i = 0; i < hiddenChars.arraySize; i++)
                    {
                        chars[i] = (char)hiddenChars.GetArrayElementAtIndex(i).intValue;
                    }

                    val = ObscuredString.Decrypt(chars, currentCryptoKey);
                }
                else
                {
                    EditorGUI.showMixedValue = true;
                }
            }

            var dataIndex = prop.propertyPath.IndexOf("Array.data[", StringComparison.Ordinal);

            if (dataIndex >= 0)
            {
                dataIndex += 11;

                var index = "Element " + prop.propertyPath.Substring(dataIndex, prop.propertyPath.IndexOf("]", dataIndex, StringComparison.Ordinal) - dataIndex);
                label.text = index;
            }

            label = EditorGUI.BeginProperty(position, label, prop);

            EditorGUI.BeginChangeCheck();
            val = EditorGUI.TextField(position, label, val);
            if (EditorGUI.EndChangeCheck())
            {
                EncryptAndSetChars(val.ToCharArray(), hiddenChars, currentCryptoKey);
                fakeValue.stringValue     = val;
                fakeValueActive.boolValue = true;
            }

            EditorGUI.showMixedValue = false;
            EditorGUI.EndProperty();
        }