Skip to content

Commit

Permalink
#104 adding documentation to core cryptonet
Browse files Browse the repository at this point in the history
  • Loading branch information
maythamfahmi committed Nov 2, 2024
1 parent ac26268 commit 0a2ef23
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 15 deletions.
71 changes: 69 additions & 2 deletions CryptoNet/CryptoNetAes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

namespace CryptoNet;

/// <summary>
/// Provides AES cryptographic functionalities, including key management, encryption, and decryption.
/// </summary>
public class CryptoNetAes : ICryptoNetAes
{
private Aes Aes { get; }
public CryptoNetInfo Info { get; }

/// <summary>
/// Initializes a new instance of the <see cref="CryptoNetAes"/> class and generates a new AES key and IV.
/// </summary>
public CryptoNetAes()
{
Aes = Aes.Create();
Expand All @@ -30,6 +36,10 @@ public CryptoNetAes()
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}

/// <summary>
/// Initializes a new instance of the <see cref="CryptoNetAes"/> class using a specified AES key in string format.
/// </summary>
/// <param name="key">The AES key as a string.</param>
public CryptoNetAes(string key)
{
Aes = Aes.Create();
Expand All @@ -40,6 +50,10 @@ public CryptoNetAes(string key)
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}

/// <summary>
/// Initializes a new instance of the <see cref="CryptoNetAes"/> class using an AES key loaded from a file.
/// </summary>
/// <param name="fileInfo">FileInfo object representing the file containing the AES key.</param>
public CryptoNetAes(FileInfo fileInfo)
{
Aes = Aes.Create();
Expand All @@ -50,6 +64,11 @@ public CryptoNetAes(FileInfo fileInfo)
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}

/// <summary>
/// Initializes a new instance of the <see cref="CryptoNetAes"/> class using specified AES key and IV.
/// </summary>
/// <param name="key">The AES key as a byte array.</param>
/// <param name="iv">The initialization vector (IV) as a byte array.</param>
public CryptoNetAes(byte[] key, byte[] iv)
{
Aes = Aes.Create();
Expand All @@ -59,6 +78,12 @@ public CryptoNetAes(byte[] key, byte[] iv)
Aes.IV = Info.AesDetail!.AesKeyValue.Iv;
}

/// <summary>
/// Creates and returns a new <see cref="CryptoNetInfo"/> object with AES key details.
/// </summary>
/// <param name="key">The AES key as a byte array.</param>
/// <param name="iv">The initialization vector (IV) as a byte array.</param>
/// <returns>A <see cref="CryptoNetInfo"/> instance containing AES details.</returns>
private CryptoNetInfo CreateInfo(byte[] key, byte[] iv)
{
return new CryptoNetInfo()
Expand All @@ -72,44 +97,81 @@ private CryptoNetInfo CreateInfo(byte[] key, byte[] iv)
};
}

/// <summary>
/// Retrieves the AES key as a string.
/// </summary>
/// <returns>The AES key as a string.</returns>
public string GetKey()
{
return CryptoNetUtils.ExportAndSaveAesKey(Aes);
}

/// <summary>
/// Saves the AES key to a specified file.
/// </summary>
/// <param name="fileInfo">FileInfo object representing the destination file.</param>
public void SaveKey(FileInfo fileInfo)
{
var key = CryptoNetUtils.ExportAndSaveAesKey(Aes);
CryptoNetUtils.SaveKey(fileInfo.FullName, key);
}

/// <summary>
/// Saves the AES key to a specified file.
/// </summary>
/// <param name="filename">The name of the file to save the key to.</param>
public void SaveKey(string filename)
{
SaveKey(new FileInfo(filename));
}

#region encryption logic

/// <summary>
/// Encrypts content from a string using AES.
/// </summary>
/// <param name="content">The content to encrypt.</param>
/// <returns>The encrypted content as a byte array.</returns>
public byte[] EncryptFromString(string content)
{
return EncryptContent(CryptoNetExtensions.StringToBytes(content));

}

/// <summary>
/// Encrypts content from a byte array using AES.
/// </summary>
/// <param name="bytes">The byte array to encrypt.</param>
/// <returns>The encrypted byte array.</returns>
public byte[] EncryptFromBytes(byte[] bytes)
{
return EncryptContent(bytes);
}

/// <summary>
/// Decrypts encrypted content to a string.
/// </summary>
/// <param name="bytes">The byte array to decrypt.</param>
/// <returns>The decrypted content as a string.</returns>
public string DecryptToString(byte[] bytes)
{
return CryptoNetExtensions.BytesToString(DecryptContent(bytes));
}

/// <summary>
/// Decrypts encrypted content to a byte array.
/// </summary>
/// <param name="bytes">The byte array to decrypt.</param>
/// <returns>The decrypted content as a byte array.</returns>
public byte[] DecryptToBytes(byte[] bytes)
{
return DecryptContent(bytes);
}

/// <summary>
/// Encrypts a byte array using AES.
/// </summary>
/// <param name="bytes">The byte array to encrypt.</param>
/// <returns>The encrypted byte array.</returns>
private byte[] EncryptContent(byte[] bytes)
{
if (bytes == null || bytes.Length <= 0)
Expand Down Expand Up @@ -152,6 +214,11 @@ private byte[] EncryptContent(byte[] bytes)
return result;
}

/// <summary>
/// Decrypts a byte array using AES.
/// </summary>
/// <param name="bytes">The byte array to decrypt.</param>
/// <returns>The decrypted byte array.</returns>
private byte[] DecryptContent(byte[] bytes)
{
if (bytes == null || bytes.Length <= 0)
Expand Down Expand Up @@ -195,4 +262,4 @@ private byte[] DecryptContent(byte[] bytes)
}

#endregion
}
}
Loading

0 comments on commit 0a2ef23

Please sign in to comment.