Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ParameterEncryptionAlgorithm.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
7
9{
15 {
16 private readonly ContractsClient client;
17 private readonly SymmetricCipherAlgorithms algorithm;
18 private readonly IE2eSymmetricCipher instance;
19 private readonly byte[] key;
20
22 IE2eSymmetricCipher Instance, byte[] Key, ContractsClient Client)
23 {
24 this.algorithm = Algorithm;
25 this.instance = Instance;
26 this.key = Key;
27 this.client = Client;
28 }
29
33 public byte[] Key => this.key;
34
38 public SymmetricCipherAlgorithms Algorithm => this.algorithm;
39
46 public static Task<ParameterEncryptionAlgorithm> Create(SymmetricCipherAlgorithms Algorithm, ContractsClient Client)
47 {
48 return Create(null, Algorithm, Client);
49 }
50
58 public static async Task<ParameterEncryptionAlgorithm> Create(string ContractId, SymmetricCipherAlgorithms DefaultAlgorithm,
59 ContractsClient Client)
60 {
61 string CreatorJid = Client.Client.BareJID;
62 byte[] Key = null;
63
64 if (!string.IsNullOrEmpty(ContractId))
65 {
66 Tuple<SymmetricCipherAlgorithms, string, byte[]> T = await Client.TryLoadContractSharedSecret(ContractId);
67
68 if (!(T.Item3 is null))
69 {
70 DefaultAlgorithm = T.Item1;
71 CreatorJid = T.Item2;
72 Key = T.Item3;
73 }
74 }
75
76 return await Create(ContractId, DefaultAlgorithm, Client, CreatorJid, Key);
77 }
78
88 public static async Task<ParameterEncryptionAlgorithm> Create(string ContractId, SymmetricCipherAlgorithms Algorithm,
89 ContractsClient Client, string CreatorJid, byte[] Key)
90 {
92
93 if (Key is null)
94 {
95 Key = Instance.GenerateKey();
96
97 if (!string.IsNullOrEmpty(ContractId))
98 await Client.SaveContractSharedSecret(ContractId, CreatorJid, Key, Algorithm, false);
99 }
100
101 return new ParameterEncryptionAlgorithm(Algorithm, Instance, Key, Client);
102 }
103
114 public byte[] Encrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce,
115 string ClearText)
116 {
117 byte[] Data;
118
119 Data = new byte[this.key.Length + ContractNonce.Length + 4];
120 int c = this.key.Length;
121 int d = ContractNonce.Length;
122
123 Array.Copy(this.key, 0, Data, 0, c);
124 Array.Copy(ContractNonce, 0, Data, c, d);
125 Array.Copy(BitConverter.GetBytes(ParameterIndex), 0, Data, c + d, 4);
126
127 Data = Hashes.ComputeSHA256Hash(Data);
128
129 int SuffixLength = Data[0];
130
131 if (ClearText is null)
132 Data = new byte[SuffixLength];
133 else
134 {
135 int Prefix = -1;
136 int i, j;
137
138 for (i = 1, c = Data.Length; i < c; i++)
139 {
140 if ((j = Data[i]) != 0)
141 {
142 Prefix = j;
143 break;
144 }
145 }
146
147 if (Prefix < 0)
148 Prefix = 1;
149
150 byte[] Data2 = Encoding.UTF8.GetBytes(ClearText);
151 c = Data2.Length;
152
153 Data = new byte[1 + c + SuffixLength];
154
155 Data[0] = (byte)Prefix;
156 Array.Copy(Data2, 0, Data, 1, c);
157 }
158
159 byte[] IV = this.instance.GetIV(ParameterName, ParameterType, CreatorJid,
160 Convert.ToBase64String(ContractNonce), ParameterIndex);
161 byte[] AssociatedData = Encoding.UTF8.GetBytes(ParameterName);
162
163 byte[] Result = this.instance.Encrypt(Data, this.key, IV, AssociatedData, E2eBufferFillAlgorithm.Zeroes);
164
165 return Result;
166 }
167
178 public string Decrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce,
179 byte[] CipherText)
180 {
181 byte[] IV = this.instance.GetIV(ParameterName, ParameterType, CreatorJid, Convert.ToBase64String(ContractNonce), ParameterIndex);
182 byte[] AssociatedData = Encoding.UTF8.GetBytes(ParameterName);
183 byte[] Data = this.instance.Decrypt(CipherText, this.key, IV, AssociatedData);
184
185 if (Data.Length == 0 || Data[0] == 0)
186 return null;
187
188 int c = Data.Length - 1;
189
190 while (c >= 0 && Data[c] == 0)
191 c--;
192
193 return Encoding.UTF8.GetString(Data, 1, c);
194 }
195 }
196}
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Implements parameter encryption using symmetric ciphers avaialble through IE2eSymmetricCipher in the ...
static Task< ParameterEncryptionAlgorithm > Create(SymmetricCipherAlgorithms Algorithm, ContractsClient Client)
Implements parameter encryption using symmetric ciphers avaialble through IE2eSymmetricCipher in the ...
SymmetricCipherAlgorithms Algorithm
Symmetric Cipher Algorithm used to encrypt parameters.
static async Task< ParameterEncryptionAlgorithm > Create(string ContractId, SymmetricCipherAlgorithms DefaultAlgorithm, ContractsClient Client)
Creates a object that implements parameter encryption using symmetric ciphers avaialble through IE2eS...
static async Task< ParameterEncryptionAlgorithm > Create(string ContractId, SymmetricCipherAlgorithms Algorithm, ContractsClient Client, string CreatorJid, byte[] Key)
Creates a object that implements parameter encryption using symmetric ciphers avaialble through IE2eS...
string Decrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce, byte[] CipherText)
Decrypts an encrypted parameter value.
byte[] Encrypt(string ParameterName, string ParameterType, uint ParameterIndex, string CreatorJid, byte[] ContractNonce, string ClearText)
Encrypts a parameter value.
static IE2eSymmetricCipher Create(SymmetricCipherAlgorithms Algorithm)
Creates an instance of a symmetric cipher algorithm.
XmppClient Client
XMPP Client.
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:348
Interface for symmetric ciphers.
byte[] GenerateKey()
Generates a new key. Used when the asymmetric cipher cannot calculate a shared secret.
SymmetricCipherAlgorithms
Enumeration of symmetric cipher algorithms available in the library.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.