<< back to the Securely main page
BenCoding.Securely.StringCrypto
The Securely StringCrypto module is used to encrypt and decrypt strings using a variety of methods.
* 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 StringCrypto component.
var stringCrypto = securely.createStringCrypto();
As the name implies this method uses the AES encryption algorithm to encrypt a string. This method returns an AES encrypted string of the plain text provided. The password is used as the AES key during the encryption process.
Arguments
The AESEncrypt method takes the following arguments ( order is important ).
1. Password - (required) The password used as the encryption seed.
2. text - (required) The text you wish to encrypt.
3. useHex - (optional default of true) A boolean flag if you want the value to be converted to hex on the return. If false, you must provide the same parameter into the decrypt method.
Return value string ( encrypted )
Example
var plainTextString = "this is a clear text example string";
var usingGUID = securely.generateDerivedKey(Ti.Platform.createUUID());
Ti.API.info("Derived key using GUID = " + usingGUID);
var aesEncryptedString = stringCrypto.AESEncrypt(usingGUID,plainTextString);
Ti.API.info("aesEncryptedString =" + aesEncryptedString);
As the name implies this method uses the AES encryption algorithm to decrypt an AES encrypted string. This method decrypts the provided encrypted text using the password provided. After decryption is completed, a plain text string is returned.
Arguments
The AESDecrypt method takes the following arguments ( order is important ).
1. Password - (required) The password used as the encryption seed.
2. text - (required) The encrypted text you wish to decrypt.
3. useHex - (optional default of true) A boolean flag if the values should be converted to hex during the decryption process. Is configuration must make that used in the AESEncrypt method call used to encrypt the string.
Return value string ( plain text )
Example
Ti.API.info("Demonstrate using AES Decryption");
var aesDecryptedString = stringCrypto.AESDecrypt(usingGUID,aesEncryptedString);
Ti.API.info('aesDecryptedString=' + aesDecryptedString);
As the name implies this method uses the DES encryption algorithm to encrypt a string. This method returns an DES encrypted string of the plain text provided. The password is used as the DES key during the encryption process.
Arguments
The DESEncrypt method takes the following arguments ( order is important ).
1. Password - (required) The password used as the encryption seed.
2. text - (required) The text you wish to encrypt.
3. useHex - (optional default of true) A boolean flag if you want the value to be converted to hex on the return. If false, you must provide the same parameter into the decrypt method.
Return value string ( encrypted )
Example
var plainTextString = "this is a clear text example string";
var usingGUID = securely.generateDerivedKey(Ti.Platform.createUUID());
Ti.API.info("Demonstrate using DES Encryption");
var desEncryptedString = stringCrypto.DESEncrypt(usingGUID,plainTextString);
Ti.API.info("desEncryptedString =" + desEncryptedString);
As the name implies this method uses the DES encryption algorithm to decrypt an DES encrypted string. This method decrypts the provided encrypted text using the password provided. After decryption is completed, a plain text string is returned.
Arguments
The DESDecrypt method takes the following arguments ( order is important ).
1. Password - (required) The password used as the encryption seed.
2. text - (required) The encrypted text you wish to decrypt.
3. useHex - (optional default of true) A boolean flag if the values should be converted to hex during the decryption process. Is configuration must make that used in the DESEncrypt method call used to encrypt the string.
Return value string ( plain text )
Example
Ti.API.info("Demonstrate using DES Decryption");
var desDecryptedString = stringCrypto.DESDecrypt(usingGUID,desEncryptedString);
Ti.API.info('desDecryptedString=' + desDecryptedString);
This method takes a string value and returns a sha256 hash of the results.
Arguments
The sha256 method takes the following argument.
text - (required) The text you wish to hash.
Return value string
This method takes a string value and returns a sha512 hash of the results.
Arguments
The sha512 method takes the following argument.
text - (required) The text you wish to hash.
Return value string
This method takes a string value and returns a hex value of the results.
Arguments
The toHex method takes the following argument.
text - (required) The text you wish to hash.
Return value string
This method takes a hex value and returns a string
Arguments
The fromHex method takes the following argument.
text - (required) The hash code you wish converted to a string
Return value string