Back to feature list...
File encryption/decryption
On this page:
Easy-to-use API
FileEncryption object is very simple - just set a password and it's ready to encrypt:
// create an instance of encryption/decryption object
var encryption = new FileEncryption();
// specify password
encryption.SetPassword("secret password");
// encrypt a file
encryption.Encrypt("dragon.jpg", "dragon.jpg.enc");
' create an instance of encryption/decryption object
Dim encryption = New FileEncryption()
' specify password
encryption.SetPassword("secret password")
' encrypt a file
encryption.Encrypt("dragon.jpg", "dragon.jpg.enc")
AES encryption algorithm in XTS mode is used by default.
This is the same mode used by the more powerful XtsStream object.
Several encryption algorithms
Rebex FileEncryption
supports several encryption algorithms:
| Algorithm | Description |
|---|---|
| AesXts |
XTS-AES, a standard algorithm for protection of stored data defined by IEEE P1619. Compatible with XtsStream class. |
| AesCbc | AES in CBC mode. |
| TripleDesCbc | 3DES in CBC mode. |
| TwofishCbc | Twofish in CBC mode. |
To specify which one to use, set the
EncryptionAlgorithm property.
It's also recommended to specify an SHA-2 algorithm for PBKDF2 key derivation
(PasswordHashingAlgorithm property):
// create an instance of encryption/decryption object
var encryption = new FileEncryption();
// specify encryption algorithm
encryption.EncryptionAlgorithm = FileEncryptionAlgorithm.TripleDesCbc;
// specify hash algorithm for PBKDF2 key derivation
encryption.PasswordHashingAlgorithm = HashingAlgorithmId.SHA256;
// specify password
encryption.SetPassword("secret password");
// encrypt a file
encryption.Encrypt("dragon.jpg", "dragon.jpg.enc");
' create an instance of encryption/decryption object
Dim encryption = New FileEncryption()
' specify encryption algorithm
encryption.EncryptionAlgorithm = FileEncryptionAlgorithm.TripleDesCbc
' specify hash algorithm for PBKDF2 key derivation
encryption.PasswordHashingAlgorithm = HashingAlgorithmId.SHA256
' specify password
encryption.SetPassword("secret password")
' encrypt a file
encryption.Encrypt("dragon.jpg", "dragon.jpg.enc")
Encrypting/decrypting a file
Use
Encrypt method to encrypt files and
Decrypt method to decrypt them. A
OverwriteExistingFile option can be useful as well.
// create an instance of encryption/decryption object
var encryption = new FileEncryption();
// overwrite target file if it already exists
encryption.OverwriteExistingFile = true;
// specify password
encryption.SetPassword("secret password");
// encrypt a file
encryption.Encrypt("dragon.jpg", "dragon.jpg.enc");
// decrypt a file
encryption.Decrypt("dragon.jpg.enc", "dragon.jpg");
' create an instance of encryption/decryption object
Dim encryption = New FileEncryption()
' overwrite target file if it already exists
encryption.OverwriteExistingFile = True
' specify password
encryption.SetPassword("secret password")
' encrypt a file
encryption.Encrypt("dragon.jpg", "dragon.jpg.enc")
' decrypt a file
encryption.Decrypt("dragon.jpg.enc", "dragon.jpg")
Encrypting/decrypting a stream
FileEncryption works with streams as well and it's just as simple as with files.
// create an instance of encryption/decryption object
var encryption = new FileEncryption();
// specify password
encryption.SetPassword("secret password");
// encrypt a stream
// (reads data from input and encrypts it)
encryption.Encrypt(input, encryptedOutput);
// decrypt a stream
// (reads data from encryptedInput and decrypts it)
encryption.Decrypt(encryptedInput, output);
' create an instance of encryption/decryption object
Dim encryption = New FileEncryption()
' specify password
encryption.SetPassword("secret password")
' encrypt a stream
' (reads data from input and encrypts it)
encryption.Encrypt(input, encryptedOutput)
' decrypt a stream
' (reads data from encryptedInput and decrypts it)
encryption.Decrypt(encryptedInput, output)
Back to feature list...