Encrypt/decrypt text or files with library

You may want to encrypt end user license files or any other data within your application. TLL brings a integrated features to do such task easily. You can use this example simply by copying into new class in your project and calling it’s easily understandable functions. Don’t forget to change initialization vector, salt and iteration count on per application basis.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeksLicensingLibrary;

namespace LicenseLibraryExample
{
    class Encryption
    {
        //IV must be 16 chars long
        protected string myInitializationVector = "CHANGE_TO_YOURS1";
        //salt can be string of any lenght
        protected string mySalt = "YOUR_SALT_FORENCRYPTION_OF_ANY_LENGHT";
        //iterations is number of encryption cycles, the more is secure and also slower
        protected int myIterations = 20000;

        public string EncryptText(string strPlain, string strPassword)
        {
            try
            {
                string strEncrypted = Rijndael256.EncryptString256Bit(strPlain, strPassword, mySalt, 256, myIterations, myInitializationVector);

                return strEncrypted;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        public string DecryptText(string strEncrypted, string strPassword)
        {
            try
            {
                string strDecrypted = Rijndael256.DecryptString256Bit(strEncrypted, strPassword, mySalt, 256, myIterations, myInitializationVector);

                return strDecrypted;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

        public string EncryptFile(string strPlainPath, string strResultPath, string strPassword)
        {
            try
            {
                byte[] bytPlain = System.IO.File.ReadAllBytes(strPlainPath);
                byte[] bytEncrypted = (byte[])Rijndael256.EncryptBytes256Bit(bytPlain, strPassword, mySalt, 256, myIterations, myInitializationVector, "SHA1", false);

                System.IO.File.WriteAllBytes(strResultPath, bytEncrypted);

                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DecryptFile(string strEncryptedPath, string strResultPath, string strPassword)
        {
            try
            {
                byte[] bytEncrypted = System.IO.File.ReadAllBytes(strEncryptedPath);
                byte[] bytDecrypted = Rijndael256.DecryptBytes256Bit(bytEncrypted, strPassword, mySalt, 256, myIterations, myInitializationVector, "SHA1");

                System.IO.File.WriteAllBytes(strResultPath, bytDecrypted);

                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

    }
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports TreeksLicensingLibrary

Namespace LicenseLibraryExample
    Class Encryption
        'IV must be 16 chars long
        Protected myInitializationVector As String = "CHANGE_TO_YOURS1"
        'salt can be string of any lenght
        Protected mySalt As String = "YOUR_SALT_FORENCRYPTION_OF_ANY_LENGHT"
        'iterations is number of encryption cycles, the more is secure and also slower
        Protected myIterations As Integer = 20000

        Public Function EncryptText(strPlain As String, strPassword As String) As String
            Try
                Dim strEncrypted As String = Rijndael256.EncryptString256Bit(strPlain, strPassword, mySalt, 256, myIterations, myInitializationVector)

                Return strEncrypted
            Catch ex As Exception
                Return ex.Message
            End Try

        End Function

        Public Function DecryptText(strEncrypted As String, strPassword As String) As String
            Try
                Dim strDecrypted As String = Rijndael256.DecryptString256Bit(strEncrypted, strPassword, mySalt, 256, myIterations, myInitializationVector)

                Return strDecrypted
            Catch ex As Exception
                Return ex.Message
            End Try

        End Function

        Public Function EncryptFile(strPlainPath As String, strResultPath As String, strPassword As String) As String
            Try
                Dim bytPlain As Byte() = System.IO.File.ReadAllBytes(strPlainPath)
                Dim bytEncrypted As Byte() = DirectCast(Rijndael256.EncryptBytes256Bit(bytPlain, strPassword, mySalt, 256, myIterations, myInitializationVector,
                "SHA1", False), Byte())

                System.IO.File.WriteAllBytes(strResultPath, bytEncrypted)

                Return "OK"
            Catch ex As Exception
                Return ex.Message
            End Try
        End Function

        Public Function DecryptFile(strEncryptedPath As String, strResultPath As String, strPassword As String) As String
            Try
                Dim bytEncrypted As Byte() = System.IO.File.ReadAllBytes(strEncryptedPath)
                Dim bytDecrypted As Byte() = Rijndael256.DecryptBytes256Bit(bytEncrypted, strPassword, mySalt, 256, myIterations, myInitializationVector,
                "SHA1")

                System.IO.File.WriteAllBytes(strResultPath, bytDecrypted)

                Return "OK"
            Catch ex As Exception
                Return ex.Message
            End Try
        End Function

    End Class
End Namespace