Overview
In Etlworks it is possible to programmatically encrypt/decrypt files and messages using the PGP algorithm. It is also possible to create a pair of private/public keys.
Key Management
PGP requires a pair of keys: private and public. Use the Public Key
to encrypt the message and use the Private Key
to decrypt it. Private Key
can be protected by the passphrase.
Upload keys
If you already have a pair of keys, simply upload them to the secure key storage.
Step 1. Create a new Server storage Connection or select one of the existing connections.
Step 2. Upload keys.
Generate keys using the designated flow
Read how to generate a pair of PGP keys using flow.
Generate keys using JavaScript
To generate a pair of keys in the secure key storage, use the following JavaScript code:
// the name, the passphrase, the key length
var keys = com.toolsverse.util.encryption.pgp.PgpKeyPairGenerator.
generateKeyPair("name", "passphrase", 2048);
var alias = new com.toolsverse.etl.common.Alias();
alias.setUrl(com.toolsverse.config.SystemConfig.instance().getKeysFolder() + '*.*');
com.toolsverse.etl.core.task.common.
FileManagerTask.write(alias, 'public.key', keys.getKey());
com.toolsverse.etl.core.task.common.
FileManagerTask.write(alias, 'private.key', keys.getValue());
Encrypt and Decrypt message using keys in key storage
To encrypt/decrypt the message (any string) using key files stored in the secure key storage, use the following JavaScript code:
var publicKeyFileName = com.toolsverse.config.SystemConfig.instance().getKeysFolder() +
"public.key";
var privateKeyFileName = com.toolsverse.config.SystemConfig.instance().getKeysFolder() +
"private.key";
var encryped = com.toolsverse.util.encryption.pgp.
PgpUtils.encryptStringWithPublicKyeFileName(originalString, publicKeyFileName);
var decrypted = com.toolsverse.util.encryption.pgp.PgpUtils.
decryptStringWithPrivateKeyFileName(encryped, privateKeyFileName, "passphrase");
Encrypt and Decrypt file using keys in key storage
To encrypt/decrypt the file in the server storage using key files stored in the secure key storage, use the following JavaScript code:
var publicKeyFileName = com.toolsverse.config.SystemConfig.instance().getKeysFolder() +
"public.key";
var privateKeyFileName = com.toolsverse.config.SystemConfig.instance().getKeysFolder() +
"private.key";
// encrypt file
var keyStream = null;
var publicKey = null;
try {
keyStream = new java.io.FileInputStream(publicKeyFileName);
publicKey = com.toolsverse.util.encryption.pgp.PgpUtils.readPublicKey(keyStream);
} finally {
if (keyStream != null) {
keyStream.close();
}
}
var fileToEncrypt = 'full file name to encrypt';
var tempFileName = com.toolsverse.util.FileUtils.
getTempFileNameInDataFolder(fileToEncrypt);
var out = null;
try {
out = new java.io.FileOutputStream(tempFileName);
com.toolsverse.util.encryption.pgp.PgpUtils.encryptFile(out, fileName,
publicKey, true, true);
out.close();
out = null;
java.nio.file.Files.move(java.nio.file.Paths.get(tempFileName),
java.nio.file.Paths.get(fileName),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} finally {
if (out != null) {
out.close();
}
}
// decrypt file
fileToDecrypt = 'full file name to decrypt';
keyStream = null;
var dectyptedStream = null;
var ecryptedStream = null;
try {
keyStream = new java.io.FileInputStream(privateKeyFileName);
ecryptedStream = new java.io.FileInputStream(fileToDecrypt);
dectyptedStream = new java.io.FileOutputStream(fileToDecrypt);
com.toolsverse.util.encryption.pgp.PgpUtils.decryptStream(ecryptedStream,
dectyptedStream, keyStream, 'passphrase'.toCharArray());
} finally {
if (keyStream != null) {
keyStream.close();
}
if (dectyptedStream != null) {
dectyptedStream.close();
}
if (ecryptedStream != null) {
ecryptedStream.close();
}
}
Encrypt and Decrypt message using generated keys
To encrypt/decrypt the message using generated keys, use the following JavaScript code:
// the name, the passphrase, the key length
var keys = com.toolsverse.util.encryption.pgp.PgpKeyPairGenerator.
generateKeyPair("name", "passphrase", 2048);
var encryped = com.toolsverse.util.encryption.pgp.
PgpUtils.encryptStringWithPublicKey(originalString, keys.getKey());
var decrypted = com.toolsverse.util.encryption.pgp.
PgpUtils.decryptStringWithPrivateKey(encryped, keys.getValue(), "passphrase");
Comments
0 comments
Please sign in to leave a comment.