Reduce unnecessary methods

This commit is contained in:
Matt Nadareski
2024-11-13 21:15:44 -05:00
parent bc31cb0f6a
commit 3fbaedd7d5
2 changed files with 9 additions and 18 deletions

View File

@@ -19,7 +19,10 @@ namespace NDecrypt.Core
/// <returns>Initialized AES cipher</returns>
public static IBufferedCipher CreateAESDecryptionCipher(byte[] key, byte[] iv)
{
var keyParam = new KeyParameter(TakeSixteen(key));
if (key.Length != 16)
throw new ArgumentOutOfRangeException(nameof(key));
var keyParam = new KeyParameter(key);
var cipher = CipherUtilities.GetCipher("AES/CTR");
cipher.Init(forEncryption: false, new ParametersWithIV(keyParam, iv));
return cipher;
@@ -33,7 +36,10 @@ namespace NDecrypt.Core
/// <returns>Initialized AES cipher</returns>
public static IBufferedCipher CreateAESEncryptionCipher(byte[] key, byte[] iv)
{
var keyParam = new KeyParameter(TakeSixteen(key));
if (key.Length != 16)
throw new ArgumentOutOfRangeException(nameof(key));
var keyParam = new KeyParameter(key);
var cipher = CipherUtilities.GetCipher("AES/CTR");
cipher.Init(forEncryption: true, new ParametersWithIV(keyParam, iv));
return cipher;
@@ -118,18 +124,6 @@ namespace NDecrypt.Core
progress($"{blockCount + 1} / {blockCount + 1} MB... Done!\r\n");
}
/// <summary>
/// Pad an array to 16 bytes
/// </summary>
/// <param name="input">Byte array value to pad</param>
/// <returns>16-byte padded array</returns>
internal static byte[] TakeSixteen(byte[] input)
{
var arr = new byte[16];
Array.Copy(input, arr, Math.Min(input.Length, 16));
return arr;
}
#endregion
#region Byte Arrays

View File

@@ -55,10 +55,7 @@ namespace NDecrypt.Core
KeyX2C = development ? args.DevKeyX0x2C : args.KeyX0x2C;
// Backup headers can't have a KeyY value set
if (signature != null)
KeyY = TakeSixteen(signature);
else
KeyY = TakeSixteen(new byte[16]);
KeyY = signature != null && signature.Length == 16 ? signature : new byte[16];
// Set the standard normal key values
NormalKey = new byte[16];