<< back to the Securely main page
BenCoding.Securely.FileCrypto
The Securely FileCrypto module is used to encrypt and decrypt files using the AES encryption algorithm.
* First you need to download and install the module as discussed [here.](https://github.com/benbahrenburg/Securely) * You can now use the module via the commonJS require method, example shown below.
var securely = require('bencoding.securely');
Requiring the module into your project
//Require the securely module into your project
var securely = require('bencoding.securely');
The following demonstrates how to create a new instance of the Securely FileCrypto component.
var fileCrypto = securely.createFileCrypto()
As the name implies this method uses the AES encryption algorithm to encrypt a file. The password is used as the AES key during the encryption process.
Parameters
The AESEncrypt method takes a dictionary with the following properties.
1. password - (required) The password used as the encryption seed.
2. from - (required) The file nativePath you wish to be encrypted
3. to - (required) The file nativePath in which the encrypted file should be generated to. If the file exists, it will be deleted before being created.
4. completed - (required) The callback method used to return the results of the encryption process.
Example
function onEncryptCompleted(e){
//Print full statement to the console
Ti.API.info(JSON.stringify(e));
if(e.success){
alert('Encrypted file created at: ' + e.to);
var test = Ti.Filesystem.getFile(e.to);
if(!test.exists()){
Ti.API.info("test failed, file missing");
}else{
Ti.API.info("Test file contents:\n" + (test.read()).text);
}
}else{
alert('failed due to: ' + e.message);
}
};
var plainTextFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'PlainText.txt'),
futureEncrypted = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'encryptedFile.txt');
fileCrypto.AESEncrypt({
password:"your password",
from:plainTextFile.nativePath,
to:futureEncrypted.nativePath,
completed:onEncryptCompleted
});
As the name implies this method uses the AES encryption algorithm to decrypt a file. The password is used as the AES key during the encryption process.
Parameters
The AESDecrypt method takes a dictionary with the following properties.
1. password - (required) The password used as the encryption seed.
2. from - (required) The file nativePath you wish to be decrypt
3. to - (required) The file nativePath in which the unencrypted file should be generated to. If the file exists, it will be deleted before being created.
4. completed - (required) The callback method used to return the results of the decryption process.
Example
function onDecryptCompleted(e){
//Print full statement to the console
Ti.API.info(JSON.stringify(e));
if(e.success){
alert('Decrypted file created at: ' + e.to);
var test = Ti.Filesystem.getFile(e.to);
if(!test.exists()){
Ti.API.info("test failed, file missing");
}else{
Ti.API.info("Test file contents:\n" + (test.read()).text);
}
}else{
alert('failed due to: ' + e.message);
}
};
var encryptedFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'encryptedFile.txt'),
futureDecrypted = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'decryptedFile.txt');
fileCrypto.AESDecrypt({
password:"your password",
from:encryptedFile.nativePath,
to:futureDecrypted.nativePath,
completed:onDecryptCompleted
});