Node.jsのcrypto.createCipheriv()メソッド
crypto.createCipheriv()メソッドは、最初に暗号オブジェクトを作成してから、指定されたキーと認証係数(iv)に渡されたアルゴリズムに従って暗号オブジェクトを返します。
構文
crypto.createCipheriv(algorithm, key, iv, options)
パラメータ
上記のパラメータは以下のように記述されます-
-
アルゴリズム –暗号の作成に使用されるアルゴリズムの入力を受け取ります。可能な値には、aes192、aes256などがあります。
-
キー –アルゴリズムとivで使用される生のキーの入力を受け取ります。可能な値のタイプは、string、buffer、TypedArray、またはDataViewです。オプションで、シークレットタイプのタイプオブジェクトにすることができます。
-
iv –初期化ベクトルとも呼ばれます。このパラメーターは、暗号を不確実で一意にするivの入力を受け取ります。秘密である必要はありません。可能な値のタイプは、string、buffer、TypedArray、DataViewです。暗号で必要とされない場合、これはnullになる可能性があります。
-
オプション –これは、ストリームの動作を制御するためのオプションのパラメーターです。暗号がCCMまたはOCBモードで使用される場合(「aes-256-ccm」のように)、これはオプションではありません
例
createCipheriv.jsという名前のファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すようにこのコードを実行します-
node createCipheriv.js
createCipheriv.js
// A node demo program for creating the ECDH
// Importing the crypto module
const crypto = require('crypto');
// Initializing the algorithm
const algorithm = 'aes-256-cbc';
// Initializing the key
const key = crypto.randomBytes(32);
// Initializing the iv vector
const iv = crypto.randomBytes(16);
// Creating the function to encrypt data
function encrypt(text) {
// Creating the cipher with the above defined parameters
let cipher = crypto.createCipheriv(
'aes-256-cbc', Buffer.from(key), iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
// Returning iv and the encrypted data
return { iv: iv.toString('hex'),
encryptedData: encrypted.toString('hex') };
}
// Printing public & private curve keys...
var output = encrypt("TutorialsPoint");
console.log(output); 出力
C:\home\node>> node createCipheriv.js
{ iv: '3dd899aa441c00d4d8d2ff95abb2e684',
encryptedData: 'b4985053bc1507fc25a4d99823dc8b03' } 例
もう1つの例を見てみましょう。
// A node demo program for creating the ECDH
// Importing the crypto module
const crypto = require('crypto');
// Initializing the algorithm
const algorithm = 'aes-192-cbc';
// Defining and initializing the password
const password = '123456789'
// Initializing the key
const key = crypto.scryptSync(password, 'TutorialsPoint', 24);
// Initializing the iv vector
const iv = Buffer.alloc(16, 0);
// Creating the cipher with the above defined parameters
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = '';
// Reading and encrypting the data
cipher.on('readable', () => {
let chunk;
while (null !== (chunk = cipher.read())) {
encrypted += chunk.toString('base64');
}
});
//Handling the closing/end event
cipher.on('end', () => {
console.log(encrypted);
});
// Printing public & private curve keys...
cipher.write('TutorialsPoint');
cipher.end();
console.log("Completed... !"); 出力
C:\home\node>> node createCipheriv.js Completed... ! uqeQEkXy5dpJjQv+JDvMHw==
-
Node.jsのcrypto.privateDecrypt()メソッド
crypto.privateDecrypt()は、crypto.publicEncrypt()メソッドで対応する公開鍵を使用して以前に暗号化されたパラメーターで渡された秘密鍵を使用して、指定されたデータコンテンツを復号化するために使用されます。 構文 crypto.privateDecrypt(privateKey、buffer) パラメータ 上記のパラメータは以下のように記述されます- キー –オブジェクト、文字列、バッファ、またはKeyObjectの5種類のデータを含めることができます。 oaepHash –このフィールドには、OAEPパディングとMGF1に使用されるハ
-
Node.jsのcrypto.getHashes()メソッド
crypto.getHashes()メソッドは、サポートされているすべてのハッシュアルゴリズムの名前を含む配列を返します。暗号パッケージには、使用できるハッシュアルゴリズムの膨大なリストがあります。ただし、最も使用されている暗号化アルゴリズムは「MD5 –メッセージダイジェストアルゴリズム5」です。 構文 crypto.getHashes() パラメータ すべてのハッシュアルゴリズムのリストを返すためです。入力する必要はありません。 例 getHashes.jsという名前のファイルを作成し、以下のコードスニペットをコピーします。ファイルを作成したら、次のコマンドを使用して、以下の例に示すよ