Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicPythonRubySQL ServerSwiftTclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcel

Node.js Examples


ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
Firebase
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)

Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Node.js) Generate Encryption Key

Discusses symmetric encryption key generation techniques for block encryption algorithms such as AES, Blowfish, and Twofish, or for other algorithms such as ChaCha20.

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOSX, and ARM

var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node10_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node10_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node10_linux32');
    } else {
        var chilkat = require('chilkat_node10_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node10_macosx');
}

function chilkatExample() {

    //  Symmetric encryption algorithms are such that the encryptor and decryptor
    //  share a pre-known secret key.  This could be a "single-use" key that is
    //  derived from a secure key exchange algorithm using RSA, ECC, or Diffie-Hellman,
    //  or it could be a password known to both sides, or
    //  it could simply be the binary bytes of the secret key known in advance on both
    //  sides.

    //  A secret key has no structure.  It's nothing more than N bytes of data.
    //  It should typically be random data, or bytes that resemble random data such
    //  as the hash of a password.

    //  The number of bytes in the secret key defines the bit-strength of an encryption
    //  algorithm.  For example, AES with a 32-byte key is 256-bit AES.  Most algorithms
    //  define restrictions on key sizes.  For example, AES has 3 choices: 128-bit, 192-bit,
    //  or 256-bit.  In the ChaCha20 algorithm, the key size must always be 256-bits (32-bytes).

    //  Both sides (encryptor and decryptor) must be in possession of the same secret key
    //  in order to communicate.   Whichever side generates the key, it must somehow
    //  deliver the key to the other side beforehand.  Key exchange algorithms, such as RSA, ECC,
    //  and Diffie-Hellman define secure ways of exchanging symmetric encryption keys.
    //  They do so using asymmetric encryption algorithms (public/private keys).  It is not
    //  required to use a key exchange algorithm to achieve the goal of having both sides
    //  in possession of the same secret key.  A long-living secret key could be exchanged
    //  via any secure out-of-band means.  For example, exchanging the information over a secure
    //  TLS (HTTPS) or SSH connection...

    //  This example assumes the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    var success;

    var crypt = new chilkat.Crypt2();
    crypt.CryptAlgorithm = "aes";
    crypt.KeyLength = 256;

    //  Generate a 32-byte random secret key,
    //  and use it in the crypt object.
    var prng = new chilkat.Prng();
    var secretKeyHex = prng.GenRandom(32,"hex");
    //  It is important that the number of bytes in the secret key
    //  matches the value specified in the KeyLength property (above).
    crypt.SetEncodedKey(secretKeyHex,"hex");
    console.log("randomly generated key: " + secretKeyHex);

    //  Alternatively, a password could be hashed using a hash algorithm
    //  the results in the desired key length.  Our desired key length
    //  in this case is 32 bytes, so we wouldn't want MD5 (16 bytes),
    //  nor would we want to use SHA-1 (20 bytes).  SHA256 would be the
    //  hash of choice because it results in 32-bytes of random-looking
    //  key material.
    crypt.HashAlgorithm = "SHA256";
    crypt.EncodingMode = "hex";
    secretKeyHex = crypt.HashStringENC("mypassword");
    crypt.SetEncodedKey(secretKeyHex,"hex");
    console.log("password-based key: " + secretKeyHex);

}

chilkatExample();

 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.