Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Aes256Encrypt.cs
1using System;
7
9{
14 {
25 : base(new ScriptNode[] { Content, Key, IV }, argumentTypes3Normal, Start, Length, Expression)
26 {
27 }
28
40 : base(new ScriptNode[] { Content, Key, IV, CipherMode }, argumentTypes4Normal, Start, Length, Expression)
41 {
42 }
43
55 public Aes256Encrypt(ScriptNode Content, ScriptNode Key, ScriptNode IV, ScriptNode CipherMode, ScriptNode PaddingMode, int Start, int Length, Expression Expression)
56 : base(new ScriptNode[] { Content, Key, IV, CipherMode, PaddingMode }, argumentTypes5Normal, Start, Length, Expression)
57 {
58 }
59
63 public override string FunctionName => nameof(Aes256Encrypt);
64
68 public override string[] DefaultArgumentNames => new string[] { "Content", "Key", "IV", "CipherMode", "PaddingMode" };
69
77 {
78 if (!(Arguments[0].AssociatedObjectValue is byte[] Data))
79 throw new ScriptRuntimeException("Data to encrypt must be binary (i.e. an array of bytes).", this);
80
81 if (!(Arguments[1].AssociatedObjectValue is byte[] Key))
82 throw new ScriptRuntimeException("Key to use for encryption must be binary (i.e. an array of bytes).", this);
83
84 if (!(Arguments[2].AssociatedObjectValue is byte[] IV))
85 throw new ScriptRuntimeException("Initiation Vector to use for encryption must be binary (i.e. an array of bytes).", this);
86
87 int c = Arguments.Length;
88 CipherMode CipherMode = c <= 3 ? CipherMode.CBC : this.ToEnum<CipherMode>(Arguments[3]);
89 PaddingMode PaddingMode = c <= 4 ? PaddingMode.PKCS7 : this.ToEnum<PaddingMode>(Arguments[4]);
90
91 return new ObjectValue(Encrypt(Data, Key, IV, CipherMode, PaddingMode));
92 }
93
103 public static byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, CipherMode CipherMode = CipherMode.CBC, PaddingMode PaddingMode = PaddingMode.PKCS7)
104 {
105 if (Data is null)
106 throw new ArgumentNullException(nameof(Data));
107
108 if (Key is null)
109 throw new ArgumentNullException(nameof(Key));
110
111 if (IV is null)
112 throw new ArgumentNullException(nameof(IV));
113
114 using (Aes Aes = Aes.Create())
115 {
116 Aes.BlockSize = 128;
117 Aes.KeySize = 256;
118 Aes.Mode = CipherMode;
119 Aes.Padding = PaddingMode;
120
121 using (ICryptoTransform Transform = Aes.CreateEncryptor(Key, IV))
122 {
123 return Transform.TransformFinalBlock(Data, 0, Data.Length);
124 }
125 }
126 }
127
128 }
129}
Aes256Encrypt(ScriptNode Content, ScriptNode Key, ScriptNode IV, ScriptNode CipherMode, ScriptNode PaddingMode, int Start, int Length, Expression Expression)
AES Encryption
override string[] DefaultArgumentNames
Default Argument names
static byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, CipherMode CipherMode=CipherMode.CBC, PaddingMode PaddingMode=PaddingMode.PKCS7)
Performs AES encryption of data.
Aes256Encrypt(ScriptNode Content, ScriptNode Key, ScriptNode IV, int Start, int Length, Expression Expression)
AES Encryption
Aes256Encrypt(ScriptNode Content, ScriptNode Key, ScriptNode IV, ScriptNode CipherMode, int Start, int Length, Expression Expression)
AES Encryption
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Class managing a script expression.
Definition: Expression.cs:41
Base class for multivariate funcions.
static readonly ArgumentType[] argumentTypes3Normal
Three normal parameters.
static readonly ArgumentType[] argumentTypes4Normal
Four normal parameters.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes5Normal
Five normal parameters.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21