Skip to content

Commit

Permalink
Add ability to specify KeySize and BlockSize
Browse files Browse the repository at this point in the history
  • Loading branch information
sjh37 committed Oct 18, 2019
1 parent 0946df6 commit 860b932
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 99 deletions.
134 changes: 112 additions & 22 deletions Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ namespace Effortless.Net.Encryption
{
public static class Bytes
{
public static int BufferLen = 4096;
private static readonly RNGCryptoServiceProvider Rng = new RNGCryptoServiceProvider();
private static PaddingMode _paddingMode = PaddingMode.ISO10126;
private static CipherMode _cipherMode = CipherMode.CBC;

public enum BlockSize
{
Default = 256,
Size128 = 128,
Size192 = 192,
Size256 = 256
}

public enum KeySize
{
Default = 256,
Size128 = 128,
Size192 = 192,
Size256 = 256
Expand Down Expand Up @@ -67,14 +77,14 @@ public static bool SetPaddingAndCipherModes(PaddingMode paddingMode, CipherMode
return true;
}

private static RijndaelManaged GetRijndaelManaged(byte[] key, byte[] iv)
private static RijndaelManaged GetRijndaelManaged(byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize)
{
var rm = new RijndaelManaged
{
KeySize = 256,
BlockSize = 256,
Padding = _paddingMode,
Mode = _cipherMode
KeySize = (int) keySize,
BlockSize = (int) blockSize,
Padding = _paddingMode,
Mode = _cipherMode
};

if (key != null)
Expand All @@ -91,7 +101,15 @@ private static RijndaelManaged GetRijndaelManaged(byte[] key, byte[] iv)
/// </summary>
public static byte[] GenerateKey()
{
using (var rm = GetRijndaelManaged(null, null))
return GenerateKey(KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Returns an encryption key to be used with the Rijndael algorithm
/// </summary>
public static byte[] GenerateKey(KeySize keySize, BlockSize blockSize)
{
using (var rm = GetRijndaelManaged(null, null, keySize, blockSize))
{
rm.GenerateKey();
return rm.Key;
Expand Down Expand Up @@ -123,7 +141,15 @@ public static byte[] GenerateKey(string password, string salt, KeySize keySize,
/// </summary>
public static byte[] GenerateIV()
{
using (var rm = GetRijndaelManaged(null, null))
return GenerateIV(KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Returns the encryption IV to be used with the Rijndael algorithm
/// </summary>
public static byte[] GenerateIV(KeySize keySize, BlockSize blockSize)
{
using (var rm = GetRijndaelManaged(null, null, keySize, blockSize))
{
rm.GenerateIV();
return rm.IV;
Expand All @@ -134,6 +160,14 @@ public static byte[] GenerateIV()
/// Encrypt a byte array into a byte array using the given Key and an IV
/// </summary>
public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv)
{
return Encrypt(clearData, key, iv, KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Encrypt a byte array into a byte array using the given Key and an IV
/// </summary>
public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize)
{
if (clearData == null || clearData.Length <= 0) throw new ArgumentNullException(nameof(clearData));
if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key));
Expand All @@ -146,7 +180,7 @@ public static byte[] Encrypt(byte[] clearData, byte[] key, byte[] iv)
// We are going to use Rijndael because it is strong and available on all platforms.
// You can use other algorithms, to do so substitute the next line with something like
// TripleDES alg = TripleDES.Create();
using (var alg = GetRijndaelManaged(key, iv))
using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize))
{
// Create a CryptoStream through which we are going to be pumping our data.
// CryptoStreamMode.Write means that we are going to be writing data to the stream and the
Expand Down Expand Up @@ -189,13 +223,12 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, Rijnda
{
// Now will will initialize a buffer and will be processing the input file in chunks.
// This is done to avoid reading the whole file (which can be huge) into memory.
const int bufferLen = 4096;
var buffer = new byte[bufferLen];
var buffer = new byte[BufferLen];
int bytesRead;

do
{
bytesRead = clearStreamIn.Read(buffer, 0, bufferLen); // Read a chunk of data from the input file
bytesRead = clearStreamIn.Read(buffer, 0, BufferLen); // Read a chunk of data from the input file
if (bytesRead > 0)
cs.Write(buffer, 0, bytesRead); // Encrypt it
} while (bytesRead != 0);
Expand All @@ -212,14 +245,22 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, Rijnda
/// Encrypt a file into another file
/// </summary>
public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] key, byte[] iv)
{
Encrypt(clearFileIn, encryptedFileOut, key, iv, KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Encrypt a file into another file
/// </summary>
public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize)
{
if (string.IsNullOrEmpty(clearFileIn)) throw new ArgumentNullException(nameof(clearFileIn));
if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut));

if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key));
if (iv == null || iv.Length <= 0) throw new ArgumentNullException(nameof(iv));

using (var alg = GetRijndaelManaged(key, iv))
using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize))
{
using (var fsIn = new FileStream(clearFileIn, FileMode.Open, FileAccess.Read))
{
Expand All @@ -232,13 +273,21 @@ public static void Encrypt(string clearFileIn, string encryptedFileOut, byte[] k
/// Encrypt a stream into a file
/// </summary>
public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, byte[] key, byte[] iv)
{
Encrypt(clearStreamIn, encryptedFileOut, key, iv, KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Encrypt a stream into a file
/// </summary>
public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize)
{
if (clearStreamIn == null) throw new ArgumentNullException(nameof(clearStreamIn));
if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut));
if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key));
if (iv == null || iv.Length <= 0) throw new ArgumentNullException(nameof(iv));

using (var alg = GetRijndaelManaged(key, iv))
using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize))
{
Encrypt(clearStreamIn, encryptedFileOut, alg);
}
Expand All @@ -249,11 +298,20 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, byte[]
/// The Key and an IV are automatically generated. These will be required when Decrypting the data.
/// </summary>
public static void Encrypt(string clearFileIn, string encryptedFileOut, out string key, out string iv)
{
Encrypt(clearFileIn, encryptedFileOut, KeySize.Default, BlockSize.Default, out key, out iv);
}

/// <summary>
/// Encrypt a file into another file.
/// The Key and an IV are automatically generated. These will be required when Decrypting the data.
/// </summary>
public static void Encrypt(string clearFileIn, string encryptedFileOut, KeySize keySize, BlockSize blockSize, out string key, out string iv)
{
if (string.IsNullOrEmpty(clearFileIn)) throw new ArgumentNullException(nameof(clearFileIn));
if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut));

using (var alg = GetRijndaelManaged(null, null))
using (var alg = GetRijndaelManaged(null, null, keySize, blockSize))
{
alg.GenerateIV();
alg.GenerateKey();
Expand All @@ -273,11 +331,20 @@ public static void Encrypt(string clearFileIn, string encryptedFileOut, out stri
/// The Key and an IV are automatically generated. These will be required when Decrypting the data.
/// </summary>
public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, out string key, out string iv)
{
Encrypt(clearStreamIn, encryptedFileOut, KeySize.Default, BlockSize.Default, out key, out iv);
}

/// <summary>
/// Encrypt a stream into a file.
/// The Key and an IV are automatically generated. These will be required when Decrypting the data.
/// </summary>
public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, KeySize keySize, BlockSize blockSize, out string key, out string iv)
{
if (clearStreamIn == null) throw new ArgumentNullException(nameof(clearStreamIn));
if (string.IsNullOrEmpty(encryptedFileOut)) throw new ArgumentNullException(nameof(encryptedFileOut));

using (var alg = GetRijndaelManaged(null, null))
using (var alg = GetRijndaelManaged(null, null, keySize, blockSize))
{
alg.GenerateIV();
alg.GenerateKey();
Expand All @@ -293,6 +360,14 @@ public static void Encrypt(Stream clearStreamIn, string encryptedFileOut, out st
/// Decrypt a byte array into a byte array using a Key and an IV
/// </summary>
public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv)
{
return Decrypt(cipherData, key, iv, KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Decrypt a byte array into a byte array using a Key and an IV
/// </summary>
public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize)
{
if (cipherData == null) throw new ArgumentNullException(nameof(cipherData));
if (key == null || key.Length <= 0) throw new ArgumentNullException(nameof(key));
Expand All @@ -307,7 +382,7 @@ public static byte[] Decrypt(byte[] cipherData, byte[] key, byte[] iv)
// We are going to use Rijndael because it is strong and available on all platforms.
// You can use other algorithms, to do so substitute the next line with something like
// TripleDES alg = TripleDES.Create();
using (var alg = GetRijndaelManaged(key, iv))
using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize))
{
// Create a CryptoStream through which we are going to be pumping our data.
// CryptoStreamMode.Write means that we are going to be writing data to the stream
Expand Down Expand Up @@ -347,13 +422,12 @@ public static void Decrypt(Stream encryptedStreamIn, Stream clearStreamOut, Rijn

// Now will will initialize a buffer and will be processing the input file in chunks.
// This is done to avoid reading the whole file (which can be huge) into memory.
const int bufferLen = 4096;
var buffer = new byte[bufferLen];
var buffer = new byte[BufferLen];
int bytesRead;

do
{
bytesRead = encryptedStreamIn.Read(buffer, 0, bufferLen); // Read a chunk of data from the input file
bytesRead = encryptedStreamIn.Read(buffer, 0, BufferLen); // Read a chunk of data from the input file
if (bytesRead > 0)
cs.Write(buffer, 0, bytesRead); // Decrypt it
} while (bytesRead != 0);
Expand All @@ -368,6 +442,14 @@ public static void Decrypt(Stream encryptedStreamIn, Stream clearStreamOut, Rijn
/// Decrypt a file into another file
/// </summary>
public static void Decrypt(string encryptedFileIn, string clearFileOut, byte[] key, byte[] iv)
{
Decrypt(encryptedFileIn, clearFileOut, key, iv, KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Decrypt a file into another file
/// </summary>
public static void Decrypt(string encryptedFileIn, string clearFileOut, byte[] key, byte[] iv, KeySize keySize, BlockSize blockSize)
{
if (string.IsNullOrEmpty(encryptedFileIn)) throw new ArgumentNullException(nameof(encryptedFileIn));
if (string.IsNullOrEmpty(clearFileOut)) throw new ArgumentNullException(nameof(clearFileOut));
Expand All @@ -379,7 +461,7 @@ public static void Decrypt(string encryptedFileIn, string clearFileOut, byte[] k
{
using (var fsOut = new FileStream(clearFileOut, FileMode.OpenOrCreate, FileAccess.Write))
{
using (var alg = GetRijndaelManaged(key, iv))
using (var alg = GetRijndaelManaged(key, iv, keySize, blockSize))
{
Decrypt(fsIn, fsOut, alg);
}
Expand All @@ -404,6 +486,14 @@ public static void Decrypt(string encryptedFileIn, string clearFileOut, string k
/// Decrypt a file into another file using a Key and an IV
/// </summary>
public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string key, string iv)
{
Decrypt(encryptedFileIn, clearStreamOut, key, iv, KeySize.Default, BlockSize.Default);
}

/// <summary>
/// Decrypt a file into another file using a Key and an IV
/// </summary>
public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string key, string iv, KeySize keySize, BlockSize blockSize)
{
if (encryptedFileIn == null) throw new ArgumentNullException(nameof(encryptedFileIn));
if (clearStreamOut == null) throw new ArgumentNullException(nameof(clearStreamOut));
Expand All @@ -412,15 +502,15 @@ public static void Decrypt(string encryptedFileIn, Stream clearStreamOut, string

using (var fsIn = new FileStream(encryptedFileIn, FileMode.Open, FileAccess.Read))
{
using (var alg = GetRijndaelManaged(Convert.FromBase64String(key), Convert.FromBase64String(iv)))
using (var alg = GetRijndaelManaged(Convert.FromBase64String(key), Convert.FromBase64String(iv), keySize, blockSize))
{
Decrypt(fsIn, clearStreamOut, alg);
}
}
}

/// <summary>
/// Converts HEX string to btye array.
/// Converts HEX string to byte array.
/// Opposite of ByteArrayToHex.
/// </summary>
public static byte[] HexToByteArray(string hexString)
Expand Down
3 changes: 1 addition & 2 deletions Digest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ public static Digest CreateFromString(string hashedData, string sharedKey)

var hashType = (HashType) int.Parse(hashedData.Substring(0, 2));

int hashLength;
int.TryParse(hashedData.Substring(2, 3), out hashLength);
int.TryParse(hashedData.Substring(2, 3), out var hashLength);
if (hashLength < 0)
return null;

Expand Down
Loading

0 comments on commit 860b932

Please sign in to comment.