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;
3using System.Security.Cryptography;
4using System.Text;
5using System.Threading.Tasks;
8
10{
15 {
19 protected Aes aes;
20
24 public Aes256()
25 {
26 this.aes = Aes.Create();
27 this.aes.BlockSize = 128;
28 this.aes.KeySize = 256;
29 this.aes.Mode = CipherMode.CBC;
30 this.aes.Padding = PaddingMode.None;
31 }
32
36 public override string LocalName => "aes";
37
42 public override IE2eSymmetricCipher CreteNew()
43 {
44 return new Aes256();
45 }
46
48 public override void Dispose()
49 {
50 base.Dispose();
51
52 this.aes?.Dispose();
53 this.aes = null;
54 }
55
66 public override byte[] GetIV(string Id, string Type, string From, string To, uint Counter)
67 {
68 byte[] IV = Hashes.ComputeSHA256Hash(Encoding.UTF8.GetBytes(Id + Type + From + To));
69 Array.Resize(ref IV, 16);
70
71 IV[12] = (byte)Counter;
72 Counter >>= 8;
73 IV[13] = (byte)Counter;
74 Counter >>= 8;
75 IV[14] = (byte)Counter;
76 Counter >>= 8;
77 IV[15] = (byte)Counter;
78
79 return IV;
80 }
81
87 protected override long GetEncryptedLength(long ContentLength)
88 {
89 return (ContentLength + 15L) & ~0xfL;
90 }
91
101 public override byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData,
102 E2eBufferFillAlgorithm FillAlgorithm)
103 {
104 byte[] Encrypted = base.Encrypt(Data, Key, IV, AssociatedData, FillAlgorithm);
105
106 lock (this.aes)
107 {
108 using (ICryptoTransform Aes = this.aes.CreateEncryptor(Key, IV))
109 {
110 Encrypted = Aes.TransformFinalBlock(Encrypted, 0, Encrypted.Length);
111 }
112 }
113
114 return Encrypted;
115 }
116
125 public override byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
126 {
127 lock (this.aes)
128 {
129 using (ICryptoTransform Aes = this.aes.CreateDecryptor(Key, IV))
130 {
131 Data = Aes.TransformFinalBlock(Data, 0, Data.Length);
132 }
133 }
134
135 return base.Decrypt(Data, Key, IV, AssociatedData);
136 }
137
146 public override async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
147 {
148 TemporaryStream PreEncrypt = new TemporaryStream();
149 ICryptoTransform Aes = null;
150
151 try
152 {
153 await base.Encrypt(Data, PreEncrypt, Key, IV, AssociatedData);
154 PreEncrypt.Position = 0;
155
156 lock (this.aes)
157 {
158 Aes = this.aes.CreateEncryptor(Key, IV);
159 }
160
161 await Crypto.CryptoTransform(Aes, PreEncrypt, Encrypted);
162 }
163 finally
164 {
165 PreEncrypt.Dispose();
166 Aes?.Dispose();
167 }
168 }
169
178 public override async Task<Stream> Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
179 {
180 ICryptoTransform Aes = null;
181 TemporaryStream Decrypted = new TemporaryStream();
182
183 try
184 {
185 lock (this.aes)
186 {
187 Aes = this.aes.CreateDecryptor(Key, IV);
188 }
189
190 await Crypto.CryptoTransform(Aes, Data, Decrypted);
191
192 Decrypted.Position = 0;
193 return await base.Decrypt(Decrypted, Key, IV, AssociatedData);
194 }
195 finally
196 {
197 Decrypted.Dispose();
198 Aes?.Dispose();
199 }
200 }
201
206 public override byte[] GenerateKey()
207 {
208 lock (this.aes)
209 {
210 this.aes.GenerateKey();
211 return this.aes.Key;
212 }
213 }
214
215 }
216}
Implements support for the AES-256 cipher in hybrid End-to-End encryption schemes.
Definition: Aes256.cs:15
override void Dispose()
IDisposable.Dispose
Definition: Aes256.cs:48
override byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
Decrypts binary data
Definition: Aes256.cs:125
override byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData, E2eBufferFillAlgorithm FillAlgorithm)
Encrypts binary data
Definition: Aes256.cs:101
override long GetEncryptedLength(long ContentLength)
Calculates the minimum size of encrypted data, given the size of the content.
Definition: Aes256.cs:87
Aes256()
Implements support for the AES-256 cipher in hybrid End-to-End encryption schemes.
Definition: Aes256.cs:24
override byte[] GenerateKey()
Generates a new key. Used when the asymmetric cipher cannot calculate a shared secret.
Definition: Aes256.cs:206
override byte[] GetIV(string Id, string Type, string From, string To, uint Counter)
Gets an Initiation Vector from stanza attributes.
Definition: Aes256.cs:66
override async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
Encrypts binary data
Definition: Aes256.cs:146
override async Task< Stream > Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
Decrypts binary data
Definition: Aes256.cs:178
override string LocalName
Local name of the E2E symmetric cipher
Definition: Aes256.cs:36
override IE2eSymmetricCipher CreteNew()
Creates a new symmetric cipher object with the same settings as the current object.
Definition: Aes256.cs:42
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:12
static Task CryptoTransform(ICryptoTransform Transform, Stream Source, Stream Destination)
Transforms a stream of data.
Definition: Crypto.cs:19
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.
E2eBufferFillAlgorithm
How buffers are filler before E2E Encryption is performed.