forked from sabresaurus/PlayerPrefsEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleEncryption.cs
More file actions
executable file
·141 lines (114 loc) · 4.6 KB
/
SimpleEncryption.cs
File metadata and controls
executable file
·141 lines (114 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
namespace Sabresaurus.PlayerPrefsExtensions
{
public static class SimpleEncryption
{
// IMPORTANT: Make sure to change this key for each project you use this encryption in to help secure your
// encrypted values. This key must be exactly 32 characters long (256 bit).
private static string key = ":{j%6j?E:t#}G10mM%9hp5S=%}2,Y26C";
// Cache the encryption provider
private static RijndaelManaged provider = null;
private static void SetupProvider()
{
// Create a new encryption provider
provider = new RijndaelManaged();
// Get the bytes from the supplied string key and use it as the provider's key
provider.Key = Encoding.ASCII.GetBytes(key);
// Ensure that the same data is always encrypted the same way when used with the same key
provider.Mode = CipherMode.ECB;
}
/// <summary>
/// Encrypts the specified string using the key stored in SimpleEncryption and returns the encrypted result
/// </summary>
public static string EncryptString(string sourceString)
{
if(provider == null)
{
// Encryption provider hasn't been set up yet, so set it up
SetupProvider();
}
// Create an encryptor to encrypt the bytes
ICryptoTransform encryptor = provider.CreateEncryptor();
// Convert the source string into bytes to be encrypted
byte[] sourceBytes = Encoding.UTF8.GetBytes(sourceString);
// Encrypt the bytes using the encryptor we just created
byte[] outputBytes = encryptor.TransformFinalBlock(sourceBytes, 0, sourceBytes.Length);
// Convert the encrypted bytes into a Base 64 string, so we can safely represent them as a string and return
// that string
return Convert.ToBase64String(outputBytes);
}
/// <summary>
/// Decrypts the specified string from its specified encrypted value into the returned decrypted value using the
/// key stored in SimpleEncryption
/// </summary>
public static string DecryptString(string sourceString)
{
if(provider == null)
{
// Encryption provider hasn't been set up yet, so set it up
SetupProvider();
}
// Create a decryptor to decrypt the encrypted bytes
ICryptoTransform decryptor = provider.CreateDecryptor();
// Convert the base 64 string representing the encrypted bytes back into an array of encrypted bytes
byte[] sourceBytes = Convert.FromBase64String(sourceString);
// Use the decryptor we just created to decrypt those bytes
byte[] outputBytes = decryptor.TransformFinalBlock(sourceBytes, 0, sourceBytes.Length);
// Turn the decrypted bytes back into the decrypted string and return it
return Encoding.UTF8.GetString(outputBytes);
}
/// <summary>
/// Encrypts the specified float value and returns an encrypted string
/// </summary>
public static string EncryptFloat(float value)
{
// Convert the float into its 4 bytes
byte[] bytes = BitConverter.GetBytes(value);
// Represent those bytes as a base 64 string
string base64 = Convert.ToBase64String(bytes);
// Return the encrypted version of that base 64 string
return SimpleEncryption.EncryptString(base64);
}
/// <summary>
/// Encrypts the specified int value and returns an encrypted string
/// </summary>
public static string EncryptInt(int value)
{
// Convert the int value into its 4 bytes
byte[] bytes = BitConverter.GetBytes(value);
// Represent those bytes as a base 64 string
string base64 = Convert.ToBase64String(bytes);
// Return the encrypted version of that base 64 string
return SimpleEncryption.EncryptString(base64);
}
/// <summary>
/// Decrypts the encrypted string representing a float into the decrypted float
/// </summary>
public static float DecryptFloat(string sourceString)
{
// Decrypt the encrypted string
string decryptedString = SimpleEncryption.DecryptString(sourceString);
// Convert the decrypted Base 64 representation back into bytes
byte[] bytes = Convert.FromBase64String(decryptedString);
// Turn the bytes back into a float and return it
return BitConverter.ToSingle(bytes, 0);
}
/// <summary>
/// Decrypts the encrypted string representing an int into the decrypted int
/// </summary>
public static int DecryptInt(string sourceString)
{
// Decrypt the encrypted string
string decryptedString = SimpleEncryption.DecryptString(sourceString);
// Convert the decrypted Base 64 representation back into bytes
byte[] bytes = Convert.FromBase64String(decryptedString);
// Turn the bytes back into a int and return it
return BitConverter.ToInt32(bytes, 0);
}
}
}