Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Aes256.cs
1using System;
2using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
10
12{
17 {
21 protected Aes aes;
22
26 public Aes256()
27 {
28 this.aes = Aes.Create();
29 this.aes.BlockSize = 128;
30 this.aes.KeySize = 256;
31 this.aes.Mode = CipherMode.CBC;
32 this.aes.Padding = PaddingMode.None;
33 }
34
38 public override string LocalName => "aes";
39
45 public override bool Supported(XmlElement E2e)
46 {
47 return XML.Attribute(E2e, "aes", false);
48 }
49
54 public override IE2eSymmetricCipher CreteNew()
55 {
56 return new Aes256();
57 }
58
60 public override void Dispose()
61 {
62 base.Dispose();
63
64 this.aes?.Dispose();
65 this.aes = null;
66 }
67
78 public override byte[] GetIV(string Id, string Type, string From, string To, uint Counter)
79 {
80 byte[] IV = Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(Id + Type + From + To));
81 Array.Resize(ref IV, 16);
82
83 IV[12] = (byte)Counter;
84 Counter >>= 8;
85 IV[13] = (byte)Counter;
86 Counter >>= 8;
87 IV[14] = (byte)Counter;
88 Counter >>= 8;
89 IV[15] = (byte)Counter;
90
91 return IV;
92 }
93
99 protected override long GetEncryptedLength(long ContentLength)
100 {
101 return (ContentLength + 15L) & ~0xfL;
102 }
103
113 public override byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData,
114 E2eBufferFillAlgorithm FillAlgorithm)
115 {
116 byte[] Encrypted = base.Encrypt(Data, Key, IV, AssociatedData, FillAlgorithm);
117
118 lock (this.aes)
119 {
120 using ICryptoTransform Aes = this.aes.CreateEncryptor(Key, IV);
121
122 Encrypted = Aes.TransformFinalBlock(Encrypted, 0, Encrypted.Length);
123 }
124
125 return Encrypted;
126 }
127
136 public override byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
137 {
138 lock (this.aes)
139 {
140 using ICryptoTransform Aes = this.aes.CreateDecryptor(Key, IV);
141
142 Data = Aes.TransformFinalBlock(Data, 0, Data.Length);
143 }
144
145 return base.Decrypt(Data, Key, IV, AssociatedData);
146 }
147
156 public override async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
157 {
158 TemporaryStream PreEncrypt = new TemporaryStream();
159 ICryptoTransform Aes = null;
160
161 try
162 {
163 await base.Encrypt(Data, PreEncrypt, Key, IV, AssociatedData);
164 PreEncrypt.Position = 0;
165
166 lock (this.aes)
167 {
168 Aes = this.aes.CreateEncryptor(Key, IV);
169 }
170
171 await Crypto.CryptoTransform(Aes, PreEncrypt, Encrypted);
172 }
173 finally
174 {
175 PreEncrypt.Dispose();
176 Aes?.Dispose();
177 }
178 }
179
188 public override async Task<Stream> Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
189 {
190 ICryptoTransform Aes = null;
191 TemporaryStream Decrypted = new TemporaryStream();
192
193 try
194 {
195 lock (this.aes)
196 {
197 Aes = this.aes.CreateDecryptor(Key, IV);
198 }
199
200 await Crypto.CryptoTransform(Aes, Data, Decrypted);
201
202 Decrypted.Position = 0;
203 return await base.Decrypt(Decrypted, Key, IV, AssociatedData);
204 }
205 finally
206 {
207 Decrypted.Dispose();
208 Aes?.Dispose();
209 }
210 }
211
216 public override byte[] GenerateKey()
217 {
218 lock (this.aes)
219 {
220 this.aes.GenerateKey();
221 return this.aes.Key;
222 }
223 }
224
225 }
226}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Implements support for the AES-256 cipher in hybrid End-to-End encryption schemes.
Definition: Aes256.cs:17
override void Dispose()
IDisposable.Dispose
Definition: Aes256.cs:60
override bool Supported(XmlElement E2e)
If the symmetric cipher is supported by a remote endpoint.
Definition: Aes256.cs:45
override byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
Decrypts binary data
Definition: Aes256.cs:136
override byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData, E2eBufferFillAlgorithm FillAlgorithm)
Encrypts binary data
Definition: Aes256.cs:113
override long GetEncryptedLength(long ContentLength)
Calculates the minimum size of encrypted data, given the size of the content.
Definition: Aes256.cs:99
Aes256()
Implements support for the AES-256 cipher in hybrid End-to-End encryption schemes.
Definition: Aes256.cs:26
override byte[] GenerateKey()
Generates a new key. Used when the asymmetric cipher cannot calculate a shared secret.
Definition: Aes256.cs:216
override byte[] GetIV(string Id, string Type, string From, string To, uint Counter)
Gets an Initiation Vector from stanza attributes.
Definition: Aes256.cs:78
override async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
Encrypts binary data
Definition: Aes256.cs:156
override async Task< Stream > Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
Decrypts binary data
Definition: Aes256.cs:188
override string LocalName
Local name of the E2E symmetric cipher
Definition: Aes256.cs:38
override IE2eSymmetricCipher CreteNew()
Creates a new symmetric cipher object with the same settings as the current object.
Definition: Aes256.cs:54
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
Helper methods for encrypting and decrypting streams of data.
Definition: Crypto.cs:14
static Task CryptoTransform(ICryptoTransform Transform, Stream Source, Stream Destination)
Transforms a stream of data.
Definition: Crypto.cs:31
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:469
Interface for symmetric ciphers.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.