-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
423 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace CurrencySystem | ||
{ | ||
public interface IVault | ||
{ | ||
double Amount { get; set; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace CurrencySystem | ||
{ | ||
[Serializable] | ||
public class Vault : IVault | ||
{ | ||
private byte[] _amount; | ||
private readonly string _currencyCode; | ||
[NonSerialized] | ||
private byte[] _key; | ||
|
||
public Vault(double amount, string currencyCode) | ||
{ | ||
_currencyCode = currencyCode; | ||
InitKeys(); | ||
Amount = amount; | ||
} | ||
|
||
public double Amount | ||
{ | ||
get | ||
{ | ||
return Decrypt(_amount, _key); | ||
} | ||
set | ||
{ | ||
_amount = Encrypt(value, _key); | ||
} | ||
} | ||
|
||
internal void InitKeys() | ||
{ | ||
_key = GenerateValidKeys(_currencyCode); | ||
} | ||
|
||
private byte[] GenerateValidKeys(string seed) | ||
{ | ||
byte[] result = new byte[0]; | ||
do | ||
{ | ||
result = result.Concat(Encoding.ASCII.GetBytes(seed)).ToArray(); | ||
} while (result.Length < 16); | ||
Array.Resize(ref result, 16); | ||
return result; | ||
} | ||
|
||
private double Decrypt(byte[] encryptedData, byte[] key) | ||
{ | ||
byte[] bytesIv = new byte[16]; | ||
Array.Copy(encryptedData, 0, bytesIv, 0, 16); | ||
byte[] data = new byte[encryptedData.Length - 16]; | ||
Array.Copy(encryptedData, 16, data, 0, data.Length); | ||
|
||
double result; | ||
|
||
using (Aes aes = Aes.Create()) | ||
{ | ||
aes.Key = key; | ||
aes.IV = bytesIv; | ||
|
||
ICryptoTransform crypt = aes.CreateDecryptor(aes.Key, aes.IV); | ||
using (MemoryStream ms = new MemoryStream(data)) | ||
{ | ||
using (CryptoStream cs = new CryptoStream(ms, crypt, CryptoStreamMode.Read)) | ||
{ | ||
using (BinaryReader sr = new BinaryReader(cs)) | ||
{ | ||
result = sr.ReadDouble(); | ||
} | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private byte[] Encrypt(double data, byte[] key) | ||
{ | ||
byte[] encrypted; | ||
using (Aes aes = Aes.Create()) | ||
{ | ||
aes.Key = key; | ||
|
||
ICryptoTransform crypt = aes.CreateEncryptor(aes.Key, aes.IV); | ||
using (MemoryStream ms = new MemoryStream()) | ||
{ | ||
using (CryptoStream cs = new CryptoStream(ms, crypt, CryptoStreamMode.Write)) | ||
{ | ||
using (BinaryWriter sw = new BinaryWriter(cs)) | ||
{ | ||
sw.Write(data); | ||
//sw.Write(BitConverter.GetBytes(data)); | ||
} | ||
} | ||
encrypted = ms.ToArray(); | ||
} | ||
encrypted = aes.IV.Concat(encrypted).ToArray(); | ||
} | ||
return encrypted; | ||
} | ||
|
||
[OnDeserialized] | ||
private void OnDeserialized(StreamingContext context) | ||
{ | ||
InitKeys(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.