Security Tutorial

Applies to: Rebex Total Pack, Rebex Security

Namespaces and assemblies 

To use the features of Rebex Security for .NET described here, you have to reference the Rebex.Security.dll and Rebex.Common.dll assembly in your project. It contains the FileEncryption and other classes in Rebex.Security namespace.

In your source files, import the following namespace:

C#

using Rebex.Security;

VB.NET

Imports Rebex.Security

Security basics 

There are two typical uses of crypting

  • encryption
  • decryption

For encryption one must use any password as encryption key. For succesful decryption of encrypted file one must know the password used for encryption of the file. There are several crypting algorythms, the supported ones are enumerated in FileEncryptionAlgorithm enum.

First of all there is a need of construction FileEncryption class and setting password for encryption.

C#

using Rebex.Security;
// ...

// create encryption class
FileEncryption cryptor = new FileEncryption();

// set password for encryption
cryptor.SetPassword("password");

// use information provided above for encrypting and decrypting
// ...

VB.NET

Imports Rebex.Security;
' ...

' create encryption class
Dim cryptor As FileEncryption = New FileEncryption()

' set password for encryption
cryptor.SetPassword("password")

' use information provided above for encrypting and decrypting
' ...

After this you are prepared for encryption and decryption of files or streams.

Encryption files 

As soon as you have instance of FileEncryption class and have set password you can encrypt file like in following sample.

C#

// create encryption class
FileEncryption cryptor = new FileEncryption();

// set password for encryption
cryptor.SetPassword("password");

// encrypt file from fileToEncryptPath to encryptedFilePath
cryptor.Encrypt(fileToEncryptPath, encryptedFilePath);

VB.NET

' create encryption class
Dim cryptor As FileEncryption = New FileEncryption()

' set password for encryption
cryptor.SetPassword("password")

' encrypt file from fileToEncryptPath to encryptedFilePath
cryptor.Encrypt(fileToEncryptPath, encryptedFilePath)

Encryption streams 

As soon as you have instance of FileEncryption class and have set password you can encrypt stream like in following sample.

C#

// create encryption class
FileEncryption cryptor = new FileEncryption();

// set password for encryption
cryptor.SetPassword("password");

// encrypt stream from streamToEncrypt to encryptedStream stream
cryptor.Encrypt(streamToEncrypt, encryptedStream);

VB.NET

' create encryption class
Dim cryptor As FileEncryption = New FileEncryption()

' set password for encryption
cryptor.SetPassword("password")

' encrypt stream from streamToEncrypt to encryptedStream stream
cryptor.Encrypt(streamToEncrypt, encryptedStream)

Decryption files 

As soon as you have instance of FileEncryption class and have set password you can decrypt file like in following sample.

C#

// create encryption class
FileEncryption cryptor = new FileEncryption();

// set password for decryption
cryptor.SetPassword("password");

// decrypt file from encryptedFilePath to decryptedFilePath
cryptor.Decrypt(encryptedFilePath, decryptedFilePath);

VB.NET

' create encryption class
Dim cryptor As FileEncryption = New FileEncryption()

' set password for decryption
cryptor.SetPassword("password")

' decrypt file from encryptedFilePath to decryptedFilePath
cryptor.Decrypt(encryptedFilePath, decryptedFilePath)

Decryption streams 

As soon as you have instance of FileEncryption class and have set password you can decrypt stream like in following sample.

C#

// create encryption class
FileEncryption cryptor = new FileEncryption();

// set password for encryption
cryptor.SetPassword("password");

// decrypt stream from encryptedStream to decryptedStream stream
cryptor.Encrypt(encryptedStream, decryptedStream);

VB.NET

' create encryption class
Dim cryptor As FileEncryption = New FileEncryption()

' set password for decryption
cryptor.SetPassword("password")

' decrypt stream from encryptedStream to decryptedStream stream
cryptor.Encrypt(encryptedStream, decryptedStream)

Back to tutorial list...