Skip to content

Commit

Permalink
Cleanup and refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Beger committed May 23, 2018
1 parent b9b5c6e commit c31655c
Show file tree
Hide file tree
Showing 153 changed files with 5,208 additions and 5,282 deletions.
91 changes: 46 additions & 45 deletions nUpdate.Administration/Core/AesManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Author: Dominic Beger (Trade/ProgTrade) 2016
// Copyright © Dominic Beger 2018

using System;
using System.IO;
Expand All @@ -10,16 +10,16 @@ namespace nUpdate.Administration.Core
public class AesManager
{
/// <summary>
/// Encrypts a string with the given key and initializing vector.
/// Decrypts a string with the given key and initializing vector.
/// </summary>
/// <param name="plainText">The text to encrypt.</param>
/// <param name="keyPassword">The password which the key should be derived from.</param>
/// <param name="ivPassword">The password which the initializing vector should be drived from.</param>
/// <returns>Returns the encrypted string as a byte-array.</returns>
public static byte[] Encrypt(string plainText, string keyPassword, string ivPassword)
/// <param name="cipherText">The byte-array of the encrypted string.</param>
/// <param name="keyPassword">The key to use.</param>
/// <param name="ivPassword">The initializing vector to use.</param>
/// <returns>Returns the plain string as SecureString.</returns>
public static SecureString Decrypt(byte[] cipherText, string keyPassword, string ivPassword)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException(nameof(plainText));
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException(nameof(cipherText));
if (keyPassword == null || keyPassword.Length <= 0)
throw new ArgumentNullException(nameof(keyPassword));
if (ivPassword == null || ivPassword.Length <= 0)
Expand All @@ -29,47 +29,49 @@ public static byte[] Encrypt(string plainText, string keyPassword, string ivPass
new byte[] {0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84});
var ivPasswordDeriveBytes = new Rfc2898DeriveBytes(ivPassword,
new byte[] {0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84});
byte[] encrypted;

string plaintext;
using (var aesAlg = new AesManaged())
{
aesAlg.KeySize = 256;
aesAlg.Key = keyPasswordDeriveBytes.GetBytes(aesAlg.KeySize/8);
aesAlg.IV = ivPasswordDeriveBytes.GetBytes(aesAlg.BlockSize/8);
aesAlg.Key = keyPasswordDeriveBytes.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = ivPasswordDeriveBytes.GetBytes(aesAlg.BlockSize / 8);

// Create a decrytor to perform the stream transform.
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
using (var srDecrypt = new StreamReader(csDecrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
encrypted = msEncrypt.ToArray();
}
}
}

// Return the encrypted bytes from the memory stream.
return encrypted;
var securedPlainText = new SecureString();
foreach (var c in plaintext)
securedPlainText.AppendChar(c);
return securedPlainText;
}

/// <summary>
/// Decrypts a string with the given key and initializing vector.
/// Encrypts a string with the given key and initializing vector.
/// </summary>
/// <param name="cipherText">The byte-array of the encrypted string.</param>
/// <param name="keyPassword">The key to use.</param>
/// <param name="ivPassword">The initializing vector to use.</param>
/// <returns>Returns the plain string as SecureString.</returns>
public static SecureString Decrypt(byte[] cipherText, string keyPassword, string ivPassword)
/// <param name="plainText">The text to encrypt.</param>
/// <param name="keyPassword">The password which the key should be derived from.</param>
/// <param name="ivPassword">The password which the initializing vector should be drived from.</param>
/// <returns>Returns the encrypted string as a byte-array.</returns>
public static byte[] Encrypt(string plainText, string keyPassword, string ivPassword)
{
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException(nameof(cipherText));
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException(nameof(plainText));
if (keyPassword == null || keyPassword.Length <= 0)
throw new ArgumentNullException(nameof(keyPassword));
if (ivPassword == null || ivPassword.Length <= 0)
Expand All @@ -79,36 +81,35 @@ public static SecureString Decrypt(byte[] cipherText, string keyPassword, string
new byte[] {0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84});
var ivPasswordDeriveBytes = new Rfc2898DeriveBytes(ivPassword,
new byte[] {0x43, 0x87, 0x23, 0x72, 0x45, 0x56, 0x68, 0x14, 0x62, 0x84});
byte[] encrypted;

string plaintext;
using (var aesAlg = new AesManaged())
{
aesAlg.KeySize = 256;
aesAlg.Key = keyPasswordDeriveBytes.GetBytes(aesAlg.KeySize/8);
aesAlg.IV = ivPasswordDeriveBytes.GetBytes(aesAlg.BlockSize/8);
aesAlg.Key = keyPasswordDeriveBytes.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = ivPasswordDeriveBytes.GetBytes(aesAlg.BlockSize / 8);

// Create a decrytor to perform the stream transform.
var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream(cipherText))
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var srDecrypt = new StreamReader(csDecrypt))
using (var swEncrypt = new StreamWriter(csEncrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
//Write all data to the stream.
swEncrypt.Write(plainText);
}

encrypted = msEncrypt.ToArray();
}
}
}

var securedPlainText = new SecureString();
foreach (var c in plaintext)
securedPlainText.AppendChar(c);
return securedPlainText;
// Return the encrypted bytes from the memory stream.
return encrypted;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Author: Dominic Beger (Trade/ProgTrade) 2016
// Copyright © Dominic Beger 2018

using System.Collections.Generic;

Expand All @@ -9,27 +9,6 @@ namespace nUpdate.Administration.Core.Application.Extension
/// </summary>
public class AssociationManager
{
/// <summary>
/// Determines of the list of extensions are associated with the specified program id.
/// </summary>
/// <param name="progId">Program id to check against.</param>
/// <param name="extensions">String array of extensions to check against the program id.</param>
/// <returns>String array of extensions that were not associated with the program id.</returns>
public string[] CheckAssociation(string progId, params string[] extensions)
{
var notAssociated = new List<string>();

foreach (var s in extensions)
{
var fai = new FileAssociationInfo(s);

if (!fai.Exists || fai.ProgId != progId)
notAssociated.Add(s);
}

return notAssociated.ToArray();
}

/// <summary>
/// Associates a single executable with a list of extensions.
/// </summary>
Expand Down Expand Up @@ -78,5 +57,26 @@ public void Associate(string progId, params string[] extensions)
fai.ProgId = progId;
}
}

/// <summary>
/// Determines of the list of extensions are associated with the specified program id.
/// </summary>
/// <param name="progId">Program id to check against.</param>
/// <param name="extensions">String array of extensions to check against the program id.</param>
/// <returns>String array of extensions that were not associated with the program id.</returns>
public string[] CheckAssociation(string progId, params string[] extensions)
{
var notAssociated = new List<string>();

foreach (var s in extensions)
{
var fai = new FileAssociationInfo(s);

if (!fai.Exists || fai.ProgId != progId)
notAssociated.Add(s);
}

return notAssociated.ToArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Author: Dominic Beger (Trade/ProgTrade) 2016
// Copyright © Dominic Beger 2018

using System;
using System.Linq;
Expand All @@ -7,7 +7,6 @@

namespace nUpdate.Administration.Core.Application.Extension
{

#region Public Enums

/// <summary>
Expand Down Expand Up @@ -81,8 +80,8 @@ public FileAssociationInfo(string extension)
/// </summary>
public string ContentType
{
get { return GetContentType(this); }
set { SetContentType(this, value); }
get => GetContentType(this);
set => SetContentType(this, value);
}

/// <summary>
Expand Down Expand Up @@ -122,17 +121,17 @@ public bool Exists
/// <example>notepad.exe, wordpad.exe, othertexteditor.exe</example>
public string[] OpenWithList
{
get { return GetOpenWithList(this); }
set { SetOpenWithList(this, value); }
get => GetOpenWithList(this);
set => SetOpenWithList(this, value);
}

/// <summary>
/// Gets or sets a value that determines the <see cref="PerceivedType" /> of the file.
/// </summary>
public PerceivedTypes PerceivedType
{
get { return GetPerceivedType(this); }
set { SetPerceivedType(this, value); }
get => GetPerceivedType(this);
set => SetPerceivedType(this, value);
}

/// <summary>
Expand All @@ -141,8 +140,8 @@ public PerceivedTypes PerceivedType
/// </summary>
public Guid PersistentHandler
{
get { return GetPersistentHandler(this); }
set { SetPersistentHandler(this, value); }
get => GetPersistentHandler(this);
set => SetPersistentHandler(this, value);
}

/// <summary>
Expand All @@ -152,19 +151,8 @@ public Guid PersistentHandler
[XmlAttribute]
public string ProgId
{
get { return GetProgId(this); }
set { SetProgId(this, value); }
}

/// <summary>
/// Gets array containing known file extensions from HKEY_CLASSES_ROOT.
/// </summary>
/// <returns>String array containing extensions.</returns>
public static string[] GetExtensions()
{
var root = Registry.ClassesRoot;
var subKeys = root.GetSubKeyNames();
return subKeys.Where(subKey => subKey.StartsWith(".")).ToArray();
get => GetProgId(this);
set => SetProgId(this, value);
}

/// <summary>
Expand All @@ -175,30 +163,6 @@ public void Create()
Create(this);
}

/// <summary>
/// Deletes the extension key.
/// </summary>
public void Delete()
{
Delete(this);
}

/// <summary>
/// Verifies that given extension exists and is associated with given program id
/// </summary>
/// <param name="extension">Extension to be checked for.</param>
/// <param name="progId">progId to be checked for.</param>
/// <returns>True if association exists, false if it does not.</returns>
public bool IsValid(string extension, string progId)
{
var fai = new FileAssociationInfo(extension);

if (!fai.Exists)
return false;

return progId == fai.ProgId;
}

/// <summary>
/// Creates actual file extension entry in registry.
/// </summary>
Expand All @@ -216,6 +180,14 @@ protected void Create(FileAssociationInfo file)
RegistryValueKind.String);
}

/// <summary>
/// Deletes the extension key.
/// </summary>
public void Delete()
{
Delete(this);
}

/// <summary>
/// Deletes actual file extension entry in registry.
/// </summary>
Expand All @@ -229,6 +201,33 @@ protected void Delete(FileAssociationInfo file)
root.DeleteSubKeyTree(file.Extension);
}

/// <summary>
/// Gets array containing known file extensions from HKEY_CLASSES_ROOT.
/// </summary>
/// <returns>String array containing extensions.</returns>
public static string[] GetExtensions()
{
var root = Registry.ClassesRoot;
var subKeys = root.GetSubKeyNames();
return subKeys.Where(subKey => subKey.StartsWith(".")).ToArray();
}

/// <summary>
/// Verifies that given extension exists and is associated with given program id
/// </summary>
/// <param name="extension">Extension to be checked for.</param>
/// <param name="progId">progId to be checked for.</param>
/// <returns>True if association exists, false if it does not.</returns>
public bool IsValid(string extension, string progId)
{
var fai = new FileAssociationInfo(extension);

if (!fai.Exists)
return false;

return progId == fai.ProgId;
}

#region Public Functions - Creators

/// <summary>
Expand Down Expand Up @@ -346,10 +345,8 @@ protected void SetOpenWithList(FileAssociationInfo file, string[] programList)

key = key.CreateSubKey("OpenWithList");
foreach (var s in programList)
{
if (key != null)
key.CreateSubKey(s);
}
}

ShellNotification.NotifyOfChange();
Expand All @@ -373,7 +370,7 @@ protected PerceivedTypes GetPerceivedType(FileAssociationInfo file)

try
{
actualType = (PerceivedTypes) Enum.Parse(typeof (PerceivedTypes), val.ToString(), true);
actualType = (PerceivedTypes) Enum.Parse(typeof(PerceivedTypes), val.ToString(), true);
}
catch (Exception ex)
{
Expand Down
Loading

0 comments on commit c31655c

Please sign in to comment.