1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Separated cipher processing into more subfunctions

This commit is contained in:
Uncled1023 2017-02-08 16:38:22 -08:00
parent 8d561dd5b8
commit c68e1d2ec6

View File

@ -14,6 +14,7 @@ using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Utilities.IO; using Org.BouncyCastle.Utilities.IO;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
namespace Teknik.Utilities namespace Teknik.Utilities
{ {
@ -182,11 +183,10 @@ namespace Teknik.Utilities
} }
} }
public static byte[] ProcessCipher(bool encrypt, Stream input, int blockSize, byte[] key, byte[] iv, string mode, string padding) public static byte[] ProcessCipher(bool encrypt, Stream input, int chunkSize, byte[] key, byte[] iv, string mode, string padding)
{ {
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding); // Create the cipher we are going to use
IBufferedCipher cipher = CreateCipher(encrypt, key, iv, mode, padding);
cipher.Init(encrypt, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
// Make sure the input stream is at the beginning // Make sure the input stream is at the beginning
input.Seek(0, SeekOrigin.Begin); input.Seek(0, SeekOrigin.Begin);
@ -199,18 +199,18 @@ namespace Teknik.Utilities
// Process the stream and save the bytes to the output // Process the stream and save the bytes to the output
do do
{ {
processedBytes = ProcessCipherBlock(cipher, input, blockSize, output, cipherOffset); processedBytes = ProcessCipherBlock(cipher, input, chunkSize, output, cipherOffset);
cipherOffset += processedBytes; cipherOffset += processedBytes;
} }
while (processedBytes > 0); while (processedBytes > 0);
// Finalize processing of the cipher // Finalize processing of the cipher
cipher.DoFinal(output, cipherOffset); FinalizeCipherBlock(cipher, output, cipherOffset);
return output; return output;
} }
public static void EncryptToFile(string filePath, Stream input, int blockSize, byte[] key, byte[] iv, string mode, string padding) public static void EncryptToFile(string filePath, Stream input, int chunkSize, byte[] key, byte[] iv, string mode, string padding)
{ {
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding); IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
@ -222,26 +222,26 @@ namespace Teknik.Utilities
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{ {
int processedBytes = 0; int processedBytes = 0;
byte[] buffer = new byte[blockSize]; byte[] buffer = new byte[chunkSize];
do do
{ {
processedBytes = ProcessCipherBlock(cipher, input, blockSize, buffer, 0); processedBytes = ProcessCipherBlock(cipher, input, chunkSize, buffer, 0);
if (processedBytes > 0) if (processedBytes > 0)
{ {
// We have bytes, lets write them to the file // We have bytes, lets write them to the file
fileStream.Write(buffer, 0, processedBytes); fileStream.Write(buffer, 0, processedBytes);
// Clear the buffer // Clear the buffer
Array.Clear(buffer, 0, blockSize); Array.Clear(buffer, 0, chunkSize);
} }
} }
while (processedBytes > 0); while (processedBytes > 0);
// Clear the buffer // Clear the buffer
Array.Clear(buffer, 0, blockSize); Array.Clear(buffer, 0, chunkSize);
// Do the final output // Finalize processing of the cipher
processedBytes = cipher.DoFinal(buffer, 0); processedBytes = FinalizeCipherBlock(cipher, buffer, 0);
if (processedBytes > 0) if (processedBytes > 0)
{ {
// We have bytes, lets write them to the file // We have bytes, lets write them to the file
@ -250,13 +250,22 @@ namespace Teknik.Utilities
} }
} }
public static int ProcessCipherBlock(IBufferedCipher cipher, Stream input, int blockSize, byte[] output, int outputOffset) public static IBufferedCipher CreateCipher(bool encrypt, byte[] key, byte[] iv, string mode, string padding)
{
IBufferedCipher cipher = CipherUtilities.GetCipher("AES/" + mode + "/" + padding);
cipher.Init(encrypt, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));
return cipher;
}
public static int ProcessCipherBlock(IBufferedCipher cipher, Stream input, int chunkSize, byte[] output, int outputOffset)
{ {
// Initialize buffer // Initialize buffer
byte[] buffer = new byte[blockSize]; byte[] buffer = new byte[chunkSize];
// Read the next block of data // Read the next block of data
int bytesRead = input.Read(buffer, 0, blockSize); int bytesRead = input.Read(buffer, 0, chunkSize);
if (bytesRead > 0) if (bytesRead > 0)
{ {
// process the cipher for the read block and add it to the output // process the cipher for the read block and add it to the output
@ -266,6 +275,12 @@ namespace Teknik.Utilities
return 0; return 0;
} }
public static int FinalizeCipherBlock(IBufferedCipher cipher, byte[] output, int outputOffset)
{
// perform final action on cipher
return cipher.DoFinal(output, outputOffset);
}
public static byte[] CreateKey(string password, string iv, int keySize = 256) public static byte[] CreateKey(string password, string iv, int keySize = 256)
{ {
byte[] ivBytes = Encoding.UTF8.GetBytes(iv); byte[] ivBytes = Encoding.UTF8.GetBytes(iv);