Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BasicSymmetricCipherNCrypt.cs
Go to the documentation of this file.
1using System;
3using System.Text;
5
7
8internal sealed class BasicSymmetricCipherNCrypt : Internal.Cryptography.BasicSymmetricCipher
9{
10 private CngKey _cngKey;
11
12 private readonly bool _encrypting;
13
14 private static readonly CngProperty s_ECBMode = new CngProperty("Chaining Mode", Encoding.Unicode.GetBytes("ChainingModeECB\0"), CngPropertyOptions.None);
15
16 private static readonly CngProperty s_CBCMode = new CngProperty("Chaining Mode", Encoding.Unicode.GetBytes("ChainingModeCBC\0"), CngPropertyOptions.None);
17
18 private static readonly CngProperty s_CFBMode = new CngProperty("Chaining Mode", Encoding.Unicode.GetBytes("ChainingModeCFB\0"), CngPropertyOptions.None);
19
20 public BasicSymmetricCipherNCrypt(Func<CngKey> cngKeyFactory, CipherMode cipherMode, int blockSizeInBytes, byte[] iv, bool encrypting, int paddingSize)
21 : base(iv, blockSizeInBytes, paddingSize)
22 {
23 _encrypting = encrypting;
24 _cngKey = cngKeyFactory();
25 CngProperty property = cipherMode switch
26 {
27 CipherMode.ECB => s_ECBMode,
28 CipherMode.CBC => s_CBCMode,
29 CipherMode.CFB => s_CFBMode,
31 };
32 _cngKey.SetProperty(property);
33 Reset();
34 }
35
36 public unsafe sealed override int Transform(ReadOnlySpan<byte> input, Span<byte> output)
37 {
38 global::Interop.NCrypt.ErrorCode errorCode;
39 int pcbResult;
41 {
42 errorCode = (_encrypting ? global::Interop.NCrypt.NCryptEncrypt(hKey, input, input.Length, null, output, output.Length, out pcbResult, global::Interop.NCrypt.AsymmetricPaddingMode.None) : global::Interop.NCrypt.NCryptDecrypt(hKey, input, input.Length, null, output, output.Length, out pcbResult, global::Interop.NCrypt.AsymmetricPaddingMode.None));
43 }
44 if (errorCode != 0)
45 {
46 throw errorCode.ToCryptographicException();
47 }
48 if (pcbResult != input.Length)
49 {
51 }
52 return pcbResult;
53 }
54
55 public sealed override int TransformFinal(ReadOnlySpan<byte> input, Span<byte> output)
56 {
57 int result = 0;
58 if (input.Length != 0)
59 {
60 result = Transform(input, output);
61 }
62 Reset();
63 return result;
64 }
65
66 protected sealed override void Dispose(bool disposing)
67 {
68 if (disposing && _cngKey != null)
69 {
71 _cngKey = null;
72 }
73 base.Dispose(disposing);
74 }
75
76 private void Reset()
77 {
78 if (base.IV != null)
79 {
80 CngProperty property = new CngProperty("IV", base.IV, CngPropertyOptions.None);
81 _cngKey.SetProperty(property);
82 }
83 }
84}
override int TransformFinal(ReadOnlySpan< byte > input, Span< byte > output)
unsafe override int Transform(ReadOnlySpan< byte > input, Span< byte > output)
BasicSymmetricCipherNCrypt(Func< CngKey > cngKeyFactory, CipherMode cipherMode, int blockSizeInBytes, byte[] iv, bool encrypting, int paddingSize)
static string Cryptography_UnexpectedTransformTruncation
Definition SR.cs:158
static string Cryptography_InvalidCipherMode
Definition SR.cs:54
Definition SR.cs:7
unsafe void SetProperty(CngProperty property)
Definition CngKey.cs:261
SafeNCryptKeyHandle Handle
Definition CngKey.cs:51
static Encoding Unicode
Definition Encoding.cs:519
int Length
Definition Span.cs:70