SoftCircuits.EasyEncryption 2.0.1

Prefix Reserved
dotnet add package SoftCircuits.EasyEncryption --version 2.0.1                
NuGet\Install-Package SoftCircuits.EasyEncryption -Version 2.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SoftCircuits.EasyEncryption" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SoftCircuits.EasyEncryption --version 2.0.1                
#r "nuget: SoftCircuits.EasyEncryption, 2.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SoftCircuits.EasyEncryption as a Cake Addin
#addin nuget:?package=SoftCircuits.EasyEncryption&version=2.0.1

// Install SoftCircuits.EasyEncryption as a Cake Tool
#tool nuget:?package=SoftCircuits.EasyEncryption&version=2.0.1                

EasyEncryption

NuGet version (SoftCircuits.EasyEncryption)

Install-Package SoftCircuits.EasyEncryption

The .NET Framework provides a number of encryption routines. However, these routines generally require a bit of work to set up correctly. Use EasyEncryption to make these encryption routines more easily accessible.

Encrypting a String

The Encrypt() method can be used to encrypt a string. Use DecryptString() to decrypt the string back to the original.

Encryption encrypt = new Encryption("Password123", EncryptionAlgorithm.TripleDes);

string original = "This is my message";
string cipher = encrypt.Encrypt(original);
string result = encrypt.DecryptString(cipher);

Debug.Assert(result == message);

Encrypting other Types

The Encrypt() method is overloaded to encrypt many different data types. When decrypting, you must use the decryption method specific to the data type you are decrypting. This example encrypts an int and double value.

Encryption encrypt = new Encryption("Password123", EncryptionAlgorithm.TripleDes);

int originalInt = 55;
double originalDouble = 123.45;
string cipherInt = encrypt.Encrypt(originalInt);
string cipherDouble = encrypt.Encrypt(originalDouble);
int resultInt = encrypt.DecryptInt32(cipherInt);
double resultDouble = encrypt.DecryptDouble(cipherDouble);

Debug.Assert(resultInt == originalInt);
Debug.Assert(resultDouble == originalDouble);

Streams

EasyEncryption also provides the streaming classes EncryptionWriter and EncryptionReader. These classes work well when encrypting to (or decrypting from) files.

The following example uses the CreateStreamWriter() to encrypt a number of integer values to a file.

Encryption encrypt = new Encryption("Password123", EncryptionAlgorithm.TripleDes);
int[] intValues = { 123, 88, 902, 27, 16, 4, 478, 54 };

using (EncryptionWriter writer = encrypt.CreateStreamWriter(path))
{
    for (int i = 0; i < intValues.Length; i++)
        writer.Write(intValues[i]);
}

Use the CreateStreamReader() method to decrypt those integer values from the file.

Encryption encrypt = new Encryption("Password123", EncryptionAlgorithm.TripleDes);
int[] intValues = new int[8];

using (EncryptionReader reader = encrypt.CreateStreamReader(path))
{
    for (int i = 0; i < intValues.Length; i++)
        intValues[i] = reader.ReadInt32();
}

Also, the CreateStreamWriter() and CreateStreamReader() methods are overloaded to accept a stream argument, allowing you to use custom streams. For example, you could use a MemoryStream to encrypt data to memory. This is demonstrated in the following example. It also uses the static method EncodeBytesToString() method to convert the results to a string. (Note that there is also a corresponding static DecodeBytesFromString() method.)

Encryption encrypt = new Encryption("Password123", EncryptionAlgorithm.TripleDes);

using (MemoryStream stream = new MemoryStream())
using (EncryptionWriter writer = encrypt.CreateStreamWriter(stream))
{
    writer.Write("ABC");
    writer.Write(123);
    writer.Write(123.45);
    string s = Encryption.EncodeBytesToString(stream.ToArray());
}

Note that the streaming classes are actually the most efficient way to encrypt and decrypt data. In fact the Encrypt() and decryption methods create an instance of EncryptionWriter internally (using a MemoryStream), even when only encrypting or decrypting a single byte.

In addition, it should be pointed out that the encrypted results produced by these routines include embedded meta data, making the encrypted data slightly larger than it would otherwise be. However, when encrypting to a stream, this data would only be stored once regardless of the number of values added to the stream. The takeaway is that you can use the Encrypt() method for a simple encryption, but should use the streaming classes for anything more complex.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SoftCircuits.EasyEncryption:

Package Downloads
SoftCircuits.WinSettings

.NET class library that makes it easy to save your application settings in Windows. Just create your settings class and have it derive from RegistrySettings (saves settings to the system registry), XmlSettings (saves settings to an XML file) or IniSettings (saves settings to an INI file). Just call the Save() method to save the class properties. Use the Load() method to load them. Use the [EncryptedSetting] attribute to specify a property should be stored. Use the [ExcludeSetting] to specify that a property does not represent an application setting and should not be saved.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 4,618 4/28/2022
2.0.0 1,133 2/22/2021
1.0.3 722 3/6/2020
1.0.2 514 3/4/2020
1.0.1 744 7/7/2019
1.0.0 541 7/3/2019

Code clean up; Added support for .NET 6.0; WARNING: Rijndael encryption was deprecated and now removed from library.