forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAesCipher.cs
More file actions
95 lines (85 loc) · 3.37 KB
/
AesCipher.cs
File metadata and controls
95 lines (85 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Security.Cryptography;
using Org.BouncyCastle.Crypto.Paddings;
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
namespace Renci.SshNet.Security.Cryptography.Ciphers
{
/// <summary>
/// AES cipher implementation.
/// </summary>
public sealed partial class AesCipher : BlockCipher, IDisposable
{
private readonly BlockCipher _impl;
/// <summary>
/// Initializes a new instance of the <see cref="AesCipher"/> class.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="iv">The IV.</param>
/// <param name="mode">The mode.</param>
/// <param name="pkcs7Padding">Enable PKCS7 padding.</param>
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
public AesCipher(byte[] key, byte[] iv, AesCipherMode mode, bool pkcs7Padding = false)
: base(key, 16, mode: null, padding: null)
{
if (mode == AesCipherMode.OFB)
{
// OFB is not supported on modern .NET
_impl = new BlockImpl(key, new OfbCipherMode(iv), pkcs7Padding ? new Pkcs7Padding() : null);
}
#if !NET
else if (mode == AesCipherMode.CFB)
{
// CFB not supported on NetStandard 2.1
_impl = new BlockImpl(key, new CfbCipherMode(iv), pkcs7Padding ? new Pkcs7Padding() : null);
}
#endif
else if (mode == AesCipherMode.CTR)
{
// CTR not supported by the BCL, use an optimized implementation
_impl = new CtrImpl(key, iv);
}
else
{
_impl = new BclImpl(
key,
iv,
(System.Security.Cryptography.CipherMode)mode,
pkcs7Padding ? PaddingMode.PKCS7 : PaddingMode.None);
}
}
/// <inheritdoc/>
public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
return _impl.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
}
/// <inheritdoc/>
public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
return _impl.EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
}
/// <inheritdoc/>
public override byte[] Encrypt(byte[] input, int offset, int length)
{
return _impl.Encrypt(input, offset, length);
}
/// <inheritdoc/>
public override byte[] Decrypt(byte[] input, int offset, int length)
{
return _impl.Decrypt(input, offset, length);
}
/// <inheritdoc/>
public override int Decrypt(byte[] input, int offset, int length, byte[] output, int outputOffset)
{
return _impl.Decrypt(input, offset, length, output, outputOffset);
}
/// <inheritdoc/>
public void Dispose()
{
if (_impl is IDisposable disposableImpl)
{
disposableImpl.Dispose();
}
}
}
}