infinity.aes256¶
Provides AES-256 encryption and decryption functionality for strings, files and streams.
All functions use AES-256 in CBC mode with PKCS#7 padding. For every encryption, a random 8-byte salt is generated; key and IV are derived from the password (encoded as UTF-8) and the salt with PBKDF2-HMAC-SHA512 (10,000 iterations). The encrypted data starts with the 8 ASCII characters SALTED__, followed by the salt and the ciphertext. Encrypting the same data twice therefore gives different results.
The ciphertext is not authenticated (no MAC). A wrong password usually causes an error during decryption, but this is not guaranteed; tampered data is not detected reliably.
Module: infinity.crypto
Example:
infinity.loadModule('infinity.crypto');
let encryptedData = infinity.aes256.encrypt('dataToEncrypt', 'secretPW123');
console.debug(encryptedData);
//Base64 string starting with "U0FMVEVEX1" (differs on every call)
let decryptedData = infinity.aes256.decrypt(encryptedData, 'secretPW123');
console.debug(decryptedData);
//dataToEncrypt
decrypt()¶
Decrypts the given Base64 string (as returned by encrypt()) with AES-256 and returns the decrypted data, decoded as UTF-8. Returns an empty string if value or password is empty. Throws an exception if the data has an invalid format or cannot be decrypted (e.g. because of a wrong password).
Signature:
decrypt( value: string, password: string ): string
Parameters:
-
value:
stringThe Base64-encoded encrypted data.
-
password:
stringThe password used previously during encryption.
Return type: string
Example:
infinity.loadModule('infinity.crypto');
let decryptedData = infinity.aes256.decrypt(encryptedData, 'secretPW123');
decryptFile()¶
Reads out the contents of the given file (created by encryptFile() or encryptStream()), decrypts them with AES-256 and writes the results to a file at the specified location. The output file is created or overwritten. Nothing is written if the input file is empty or password is empty. Throws an exception if the data has an invalid format or cannot be decrypted.
Signature:
decryptFile( inFileName: string, outFileName: string, password: string ): void
Parameters:
-
inFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the encrypted file.
-
outFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should contain the decrypted output.
-
password:
stringThe password used previously during encryption.
Example:
infinity.loadModule('infinity.crypto');
infinity.aes256.decryptFile('encryptedData.txt', 'decryptedData.txt', 'secretPW123');
decryptStream()¶
Reads the contents of the given stream from its current position to the end, decrypts them with AES-256 and writes the results to another given stream at its current position. The positions are not reset before or after the operation. Nothing is written if there is no data to read or password is empty. Throws an exception if the data has an invalid format or cannot be decrypted.
Signature:
decryptStream( inStream: infinity.stream, outStream: infinity.stream, password: string ): void
Parameters:
-
inStream:
infinity.streamThe stream object to decrypt data from. See infinity.stream.
-
outStream:
infinity.streamThe stream object to write to. See infinity.stream.
-
password:
stringThe password used previously during encryption.
Example:
infinity.loadModule('infinity.crypto');
infinity.aes256.decryptStream(inStream, outStream, 'secretPW123');
encrypt()¶
Encrypts the given string (encoded as UTF-8) with AES-256 and returns the encrypted data as a Base64 string (no line breaks). Returns an empty string if value or password is empty.
Signature:
encrypt( value: string, password: string ): string
Parameters:
-
value:
stringThe string to encrypt.
-
password:
stringThe password to be used later for decryption.
Return type: string
Example:
infinity.loadModule('infinity.crypto');
let encryptedData = infinity.aes256.encrypt(dataToEncrypt, 'secretPW123');
encryptFile()¶
Reads out the contents of the given file, encrypts them with AES-256 and writes the results (binary, not Base64) to a file at the specified location. The output file is created or overwritten. Nothing is written if the input file is empty or password is empty.
Signature:
encryptFile( inFileName: string, outFileName: string, password: string ): void
Parameters:
-
inFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which has to be encrypted.
-
outFileName:
stringFilename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which should contain the encrypted output.
-
password:
stringThe password to be used later for decryption.
Example:
infinity.loadModule('infinity.crypto');
infinity.aes256.encryptFile('dataToEncrypt.txt', 'encryptedData.txt', 'secretPW123');
encryptStream()¶
Reads the contents of the given stream from its current position to the end, encrypts them with AES-256 and writes the results (binary, not Base64) to another given stream at its current position. The positions are not reset before or after the operation. Nothing is written if there is no data to read or password is empty.
Signature:
encryptStream( inStream: infinity.stream, outStream: infinity.stream, password: string ): void
Parameters:
-
inStream:
infinity.streamThe stream object to encrypt. See infinity.stream.
-
outStream:
infinity.streamThe stream object to write to. See infinity.stream.
-
password:
stringThe password to be used later for decryption.
Example:
infinity.loadModule('infinity.crypto');
infinity.aes256.encryptStream(inStream, outStream, 'secretPW123');