-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptedString.cs
More file actions
177 lines (147 loc) · 4.98 KB
/
EncryptedString.cs
File metadata and controls
177 lines (147 loc) · 4.98 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using System.Diagnostics;
//using System.Text;
namespace Crypto
{
public sealed class EncryptedString : IEquatable<EncryptedString>
{
private char[] cryptoKey;
private char[] encrypted_char_data;
private static EncryptedString instance;
private EncryptedString(string input)
{
if (string.IsNullOrEmpty(input))
{
return;
}
cryptoKey = new char[input.ToCharArray().Length];
GetRandomKey(ref cryptoKey);
encrypted_char_data = Encrypt(input.ToCharArray(), cryptoKey);
instance = this;
}
private static char[] Encrypt(char[] value, char[] key)
{
if (value == null || value.Length == 0)
{
return value;
}
var result = new char[value.Length];
var transformedtext = new char[value.Length];
if (key.Length == 0) { Debug.Fail("The Encryption key cannot be empty or null."); return value; }
for (int i = 0; i < value.Length; i++)
{
transformedtext[i] = Convert.ToChar(Transforms.TransformByte(Convert.ToByte(value[i])));
}
for (int i = 0; i < value.Length; i++)
{
result[i] = (char) (value[i] ^ (key[i] ^ 0xfe));
}
return result;
}
private static void GetRandomKey(ref char[] outcharbytes)
{
Random rnd = new Random();
for (int i = 0; i < outcharbytes.Length; i++)
{
outcharbytes[i] = Convert.ToChar((Transforms.TransformByte(rnd.Next(256)))^ 0xfe);
}
}
private static char[] Decrypt(char[] value, char[] key)
{
if (value == null || value.Length == 0)
{
return value;
}
var result = new char[value.Length];
var transformedtext = new char[value.Length];
if (key.Length == 0) { Debug.Fail("The Encryption key cannot be empty or null."); return value; }
for (int i = 0; i < value.Length; i++)
{
transformedtext[i] = Convert.ToChar(Transforms.InvertByteTransform(Convert.ToByte(value[i])));
}
for (int i = 0; i < value.Length; i++)
{
result[i] = (char) (value[i] ^ (key[i] ^ 0xfe));
}
return result;
}
public int Length => cryptoKey.Length;
private string GetDecryptedValue
{
get
{
return new string(Decrypt(encrypted_char_data, cryptoKey));
}
}
private char[] GetDecryptedChars
{
get
{
return Decrypt(encrypted_char_data, cryptoKey);
}
}
public char this[int position]
{
get
{
if (position < 0 || position >= cryptoKey.Length)
{
throw new IndexOutOfRangeException();
}
return Decrypt(encrypted_char_data, cryptoKey)[position];
}
}
private static EncryptedString GetInstance()
{
//if (instance == null) { throw new NullReferenceException(); }
return instance;
}
public static implicit operator EncryptedString(string value)
{
return value == null ? null : new EncryptedString(value);
}
public static implicit operator string(EncryptedString value)
{
return value == null ? null : value.GetDecryptedValue;
}
public override int GetHashCode()
{
return GetDecryptedValue.GetHashCode();
}
public override string ToString()
{
return GetDecryptedValue;
}
public bool Equals(EncryptedString other)
{
if (other == null) return false;
if (cryptoKey == other.cryptoKey)
{
return ArraysEquals(encrypted_char_data, other.encrypted_char_data);
}
return ArraysEquals(GetDecryptedChars, other.GetDecryptedChars);
}
public string Substring(int startPosition)
{
return Substring(startPosition, Length - startPosition);
}
public string Substring(int startPosition, int length)
{
return GetDecryptedValue.Substring(startPosition, length);
}
private static bool ArraysEquals(char[] left, char[] right)
{
if (left == right) return true;
if (left == null || right == null) return false;
if (left.Length != right.Length) return false;
for (var i = 0; i < left.Length; i++)
{
if (left[i] != right[i])
{
return false;
}
}
return true;
}
}
}