Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Node.js) RSAES-OAEP Encrypt/Decrypt Binary Data with AES-128 and SHA56Demonstrates the use of the new EncryptBd and DecryptBd methods introduced in Chilkat v9.5.0.67 to create a PKCS7/CMS (Cryptographic Message Syntax) message using RSAES-OAEP with AES-128 and SHA256. Note: This example requires Chilkat v9.5.0.67 or greater.
var os = require('os'); if (os.platform() == 'win32') { if (os.arch() == 'ia32') { var chilkat = require('@chilkat/ck-node21-win-ia32'); } else { var chilkat = require('@chilkat/ck-node21-win64'); } } else if (os.platform() == 'linux') { if (os.arch() == 'arm') { var chilkat = require('@chilkat/ck-node21-arm'); } else if (os.arch() == 'x86') { var chilkat = require('@chilkat/ck-node21-linux32'); } else { var chilkat = require('@chilkat/ck-node21-linux64'); } } else if (os.platform() == 'darwin') { if (os.arch() == 'arm64') { var chilkat = require('@chilkat/ck-node21-mac-m1'); } else { var chilkat = require('@chilkat/ck-node21-macosx'); } } function chilkatExample() { // This example requires the Chilkat Crypt API to have been previously unlocked. // See Unlock Chilkat Crypt for sample code. // Load a small JPG file to be encrypted/decrypted. var jpgBytes = new chilkat.BinData(); var success = jpgBytes.LoadFile("qa_data/jpg/starfish20.jpg"); if (success !== true) { console.log("Failed to load JPG file."); return; } // Show the unencrypted JPG bytes in Base64 format. // (The "base64_mime" encoding was added in Chilkat v9.5.0.67. // The "base64" encoding emits a single line of base64, whereas // "base64_mime" will emit multi-line base64 as it would appear // in MIME.) console.log(jpgBytes.GetEncoded("base64_mime")); // Sample base64_mime JPG data: // /9j/4AAQSkZJRgABAQEASABIAAD//gAmRmlsZSB3cml0dGVuIGJ5IEFkb2JlIFBob3Rvc2hvcD8g // NC4w/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgxIyUdKDozPTw5Mzg3QEhcTkBEV0U3OFBtUVdf // YmdoZz5NcXlwZHhcZWdj/9sAQwEREhIYFRgvGhovY0I4QmNjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj // Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj/8IAEQgAFAAUAwERAAIRAQMRAf/EABcAAAMBAAAA // AAAAAAAAAAAAAAIDBAX/xAAYAQADAQEAAAAAAAAAAAAAAAABAgMEAP/aAAwDAQACEAMQAAAB2kZY // NNEijWKddfTmLgALWH//xAAbEAACAgMBAAAAAAAAAAAAAAABAgMRAAQSE//aAAgBAQABBQL0XqN+ // pM2aqJGMiqFFCyg7z//EABwRAAICAgMAAAAAAAAAAAAAAAERAAIQIQMSUf/aAAgBAwEBPwHqU5aq // Axx+y1tMQl4elj//xAAcEQEAAQUBAQAAAAAAAAAAAAABEQACEBIhA1H/2gAIAQIBAT8B3Bhqy7Zc // enyiwmGgDhiOzj//xAAdEAABAwUBAAAAAAAAAAAAAAABAAIREBIhIkFR/9oACAEBAAY/ArZyn+Cg // xtxWuJaoCnqDuin/xAAcEAABBAMBAAAAAAAAAAAAAAABABEhYRAxQVH/2gAIAQEAAT8hkEwPUUR9 // DYfE4nxtRpIkBTsayuALIiuY/9oADAMBAAIAAwAAABDWPTsf/8QAGhEAAwADAQAAAAAAAAAAAAAA // AAEREDFBIf/aAAgBAwEBPxC0DVPcWm+Ce4OesrkE6bjH/8QAGBEBAQEBAQAAAAAAAAAAAAAAAREA // QRD/2gAIAQIBAT8QahMiOc8YgSrnTY3ELclHXn//xAAcEAEBAAIDAQEAAAAAAAAAAAABEQAhMUFx // EFH/2gAIAQEAAT8Qn3igmSZSj+c4N4zapMy9IjFV98wncN2iuLFsCEbDGxQkI6RO/n//2Q== var crypt = new chilkat.Crypt2(); // Specify the encryption to be used. // "pki" indicates "Public Key Infrastructure" and will create a PKCS7/CMS message. crypt.CryptAlgorithm = "pki"; crypt.Pkcs7CryptAlg = "aes"; crypt.KeyLength = 128; crypt.OaepHash = "sha256"; crypt.OaepPadding = true; // A certificate is needed as the encryption key.. var cert = new chilkat.Cert(); success = cert.LoadFromFile("qa_data/rsaes-oaep/cert.pem"); if (success !== true) { console.log(cert.LastErrorText); return; } // Tell the crypt object to use the certificate. crypt.SetEncryptCert(cert); // Do the in-place RSAES-OAEP encryption. // The contents of jpgBytes are replaced with the CMS message. success = crypt.EncryptBd(jpgBytes); if (success !== true) { console.log(crypt.LastErrorText); return; } // Examine the JPG bytes again. The bytes should be different because they are encrypted: console.log(jpgBytes.GetEncoded("base64_mime")); // Sample CMS message: // This CMS message can be copy-and-pasted into the online web form // at https://lapo.it/asn1js/ to verify the algorithms used. // MIIFDAYJKoZIhvcNAQcDoIIE/TCCBPkCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw // CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ // dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP // oA0wCwYJYIZIAWUDBAIBBIIBAHyWLgkJfIvoA3cYEAR/uHfA7uoi4eQXHl2woQAd6W5BbUNVBcYD // zuCTTOTWo1e7Uh0j0AhMZvVQf3+cngTiimzKwIZ2LNuNAgYOhrO/7coHyB22ImVetncUpCsAv/u7 // 2tYp1dO36T56K+2hMELwcQXTj6v5ODO6a3emdCjITCjHhlYePvq2l0HyU2ALG5RFB6ldk9imhKzn // 6gfcijfH65S+KfMRlFBCXFu5nCBKmi8Ywo8Ue0rFssUDKLCjCeQNY52symiDbN/d06K/luOUwVhY // 1KQffdIKmTrZUugw+FaoQRq0xGj39T/sYu8qCinNZu/vPdUmxcXszSaSVJ/LGwcwggNOBgkqhkiG // 9w0BBwEwHQYJ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r // qu/ftXWypfcfvATxme+cN2EBsCDhq4VcMgB6JY3ykv6P8PK/QpMTbu4Y // To decrypt, we'll need the cert with private key. // (The certificate alone contains only the public key.) // Provide the required cert + private key from a PFX (.pfx/.p12) file. success = crypt.AddPfxSourceFile("qa_data/rsaes-oaep/cert_plus_privatekey.pfx","PFX_PASSWORD"); if (success !== true) { console.log(crypt.LastErrorText); return; } // Decrypt to restore back to the original: success = crypt.DecryptBd(jpgBytes); if (success !== true) { console.log(crypt.LastErrorText); return; } console.log(jpgBytes.GetEncoded("base64_mime")); } chilkatExample(); |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.