Maison  >  Article  >  interface Web  >  Explication détaillée de la méthode de cryptage cryptographique de nodeJS

Explication détaillée de la méthode de cryptage cryptographique de nodeJS

零下一度
零下一度original
2017-06-24 14:49:314875parcourir

Mots précédents

Le module de chiffrement fournit une méthode d'encapsulation des informations de sécurité lors des connexions HTTP ou HTTPS. L'encapsulation des méthodes de hachage, hmac, de chiffrement (chiffrement), de déchiffrement (déchiffrement), de signature (signe) et de vérification (vérification) d'OpenSSL est également fournie. Cet article présentera en détail crypto

crypto

[crypto.setEngine(engine[, flags])]

pour certaines/toutes les fonctions OpenSSL Chargez et réglez le moteur (selon le paramètre flags).

Le moteur peut être un identifiant ou un chemin vers une bibliothèque partagée de moteur.

Flags est un paramètre facultatif, la valeur par défaut est ENGINE_METHOD_ALL, qui peut être une combinaison d'un ou plusieurs des paramètres suivants (définis dans des constantes)

ENGINE_METHOD_RSA
ENGINE_METHOD_DSA
ENGINE_METHOD_DH
ENGINE_METHOD_RAND
ENGINE_METHOD_ECDH
ENGINE_METHOD_ECDSA
ENGINE_METHOD_CIPHERS
ENGINE_METHOD_DIGESTS
ENGINE_METHOD_STORE
ENGINE_METHOD_PKEY_METH
ENGINE_METHOD_PKEY_ASN1_METH
ENGINE_METHOD_ALL
ENGINE_METHOD_NONE

[crypto .getCiphers()】

Renvoie un tableau de noms d'algorithmes de chiffrement pris en charge

 crypto = require('crypto'

【crypto.getCiphers()】

Renvoie ceux pris en charge. Tableau de noms de hachage.

var crypto = require('crypto');
console.log(crypto.getHashes());//[ 'DSA',  'DSA-SHA',  'DSA-SHA1',  'DSA-SHA1-old',  'RSA-MD4',  'RSA-MD5',  'RSA-MDC2',  'RSA-RIPEMD160',  'RSA-SHA',  'RSA-SHA1',  'RSA-SHA1-2',  'RSA-SHA224',  'RSA-SHA256',  'RSA-SHA384',  'RSA-SHA512',  'dsaEncryption',  'dsaWithSHA',  'dsaWithSHA1',  'dss1',  'ecdsa-with-SHA1',  'md4',  'md4WithRSAEncryption',  'md5',  'md5WithRSAEncryption',  'mdc2',  'mdc2WithRSA',  'ripemd',  'ripemd160',  'ripemd160WithRSA',  'rmd160',  'sha',  'sha1',  'sha1WithRSAEncryption',  'sha224',  'sha224WithRSAEncryption',  'sha256',  'sha256WithRSAEncryption',  'sha384',  'sha384WithRSAEncryption',  'sha512',  'sha512WithRSAEncryption',  'shaWithRSAEncryption',  'ssl2-md5',  'ssl3-md5',  'ssl3-sha1',  'whirlpool' ]

[crypto.getCurves()]

Renvoie un tableau de noms de courbes elliptiques pris en charge.

var crypto = require('crypto');
console.log(crypto.getCurves());//[ 'Oakley-EC2N-3',  'Oakley-EC2N-4',  'brainpoolP160r1',  'brainpoolP160t1',  'brainpoolP192r1',  'brainpoolP192t1',  'brainpoolP224r1',  'brainpoolP224t1',  'brainpoolP256r1',  'brainpoolP256t1',  'brainpoolP320r1',  'brainpoolP320t1',  'brainpoolP384r1',  'brainpoolP384t1',  'brainpoolP512r1',  'brainpoolP512t1',  'c2pnb163v1',  'c2pnb163v2',  'c2pnb163v3',  'c2pnb176v1',  'c2pnb208w1',  'c2pnb272w1',  'c2pnb304w1',  'c2pnb368w1',  'c2tnb191v1',  'c2tnb191v2',  'c2tnb191v3',  'c2tnb239v1',  'c2tnb239v2',  'c2tnb239v3',  'c2tnb359v1',  'c2tnb431r1',  'prime192v1',  'prime192v2',  'prime192v3',  'prime239v1',  'prime239v2',  'prime239v3',  'prime256v1',  'secp112r1',  'secp112r2',  'secp128r1',  'secp128r2',  'secp160k1',  'secp160r1',  'secp160r2',  'secp192k1',  'secp224k1',  'secp224r1',  'secp256k1',  'secp384r1',  'secp521r1',  'sect113r1',  'sect113r2',  'sect131r1',  'sect131r2',  'sect163k1',  'sect163r1',  'sect163r2',  'sect193r1',  'sect193r2',  'sect233k1',  'sect233r1',  'sect239k1',  'sect283k1',  'sect283r1',  'sect409k1',  'sect409r1',  'sect571k1',  'sect571r1',  'wap-wsg-idm-ecid-wtls1',  'wap-wsg-idm-ecid-wtls10',  'wap-wsg-idm-ecid-wtls11',  'wap-wsg-idm-ecid-wtls12',  'wap-wsg-idm-ecid-wtls3',  'wap-wsg-idm-ecid-wtls4',  'wap-wsg-idm-ecid-wtls5',  'wap-wsg-idm-ecid-wtls6',  'wap-wsg-idm-ecid-wtls7',  'wap-wsg-idm-ecid-wtls8',  'wap-wsg-idm-ecid-wtls9' ]

MD5

MD5 est un algorithme de hachage couramment utilisé pour donner une "signature" à n'importe quelle donnée. Cette signature est généralement représentée par une chaîne hexadécimale :

[crypto.createHash(algorithm)]

Crée et renvoie un objet de hachage, en utilisant l'algorithme spécifié pour générer le résumé de hachage.

 L'algorithme des paramètres dépend de l'algorithme supporté par la version OpenSSL sur la plateforme. Par exemple, 'sha1', 'md5', 'sha256', 'sha512' et ainsi de suite

【hash.update(data[, input_encoding])】

 Mettre à jour le contenu de hachage en fonction pour data , la méthode de codage est déterminée en fonction de input_encoding, y compris 'utf8', 'ascii' ou 'binary'. Si aucune valeur n'est transmise, le codage par défaut est « utf8 ». Si data est un Buffer, input_encoding sera ignoré.

Comme il s'agit de données en streaming, il peut être appelé plusieurs fois avec des données différentes.

[hash.digest([encoding])]

Calculez le résumé de hachage des données entrantes. l'encodage peut être 'hex', 'binary' ou 'base64', si l'encodage n'est pas spécifié, le tampon sera renvoyé.

 [Note] L'objet de hachage ne peut pas être utilisé après avoir appelé digest().

var crypto = require('crypto');var hash = crypto.createHash('md5');// 可任意多次调用update():hash.update('Hello, world!');
hash.update('Hello, nodejs!');

console.log(hash.digest('hex')); // 7e1977739c748beac0c0fd14fd26a544

Hmac

L'algorithme Hmac est également un algorithme de hachage, qui peut utiliser des algorithmes de hachage tels que MD5 ou SHA1. La différence est que Hmac nécessite également une clé :

[crypto.createHmac(algorithm, key)]

Créer et renvoyer un objet hmac, et générer une carte hmac en utilisant l'algorithme et le secret spécifiés clé .

C'est un flux lisible et inscriptible. Les données écrites sont utilisées pour calculer hmac. Une fois l'écriture dans le flux terminée, utilisez la méthode read() pour obtenir la valeur calculée. Les anciennes méthodes de mise à jour et de résumé sont également prises en charge.

 L'algorithme du paramètre dépend de l'algorithme supporté par la version OpenSSL sur la plateforme, voir createHash ci-dessus. La clé est la clé utilisée dans l'algorithme hmac

[hmac.update(data)]

Mettre à jour l'objet hmac en fonction des données. Puisqu'il s'agit de données en continu, il peut être appelé plusieurs fois avec de nouvelles données.

[hmac.digest([encoding])]

Calculez la valeur hmac des données entrantes. l'encodage peut être 'hex', 'binary' ou 'base64', si l'encodage n'est pas spécifié, le tampon sera renvoyé.

[Note] L'objet hmac ne peut pas être utilisé après avoir appelé digest()

var crypto = require('crypto');var hmac = crypto.createHmac('sha256', 'match');

hmac.update('Hello, world!');
hmac.update('Hello, nodejs!');//e82a58066cae2fae4f44e58be1d589b66a5d102c2e8846d796607f02a88c1649console.log(hmac.digest('hex'));

 

AES

  AES是一种常用的对称加密算法,加解密都用同一个密钥。crypto模块提供了AES支持,但是需要自己封装好函数,便于使用:

【crypto.createCipher(algorithm, password)】

  使用传入的算法和秘钥来生成并返回加密对象。

  algorithm 取决于 OpenSSL,例如'aes192'等。password 用来派生 key 和 IV,它必须是一个'binary' 编码的字符串或者一个buffer。

  它是可读写的流 stream 。写入的数据来用计算 hmac。当写入流结束后,使用 read() 方法来获取计算后的值。也支持老的update 和 digest 方法。

【cipher.update(data[, input_encoding][, output_encoding])】

  根据 data 来更新哈希内容,编码方式根据 input_encoding 来定,有 'utf8', 'ascii' or 'binary'。如果没有传入值,默认编码方式是'binary'。如果data 是 Buffer,input_encoding 将会被忽略。

  output_encoding 指定了输出的加密数据的编码格式,它可用是 'binary', 'base64' 或 'hex'。如果没有提供编码,将返回 buffer 。

  返回加密后的内容,因为它是流式数据,所以可以使用不同的数据调用很多次。

【cipher.final([output_encoding])】

  返回加密后的内容,编码方式是由 output_encoding 指定,可以是 'binary', 'base64' 或 'hex'。如果没有传入值,将返回 buffer。

  [注意]cipher 对象不能在 final() 方法之后调用。

var crypto = require('crypto');function aesEncrypt(data, key) {
    const cipher = crypto.createCipher('aes192', key);var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');return crypted;
}var data = 'Hello, this is a secret message!';var key = 'Password!';var encrypted = aesEncrypt(data, key);//8a944d97bdabc157a5b7a40cb180e713f901d2eb454220d6aaa1984831e17231f87799ef334e3825123658c80e0e5d0cconsole.log(encrypted);

【crypto.createDecipher(algorithm, password)】

  根据传入的算法和密钥,创建并返回一个解密对象。这是 createCipher() 的镜像

【decipher.update(data[, input_encoding][, output_encoding])】

  使用参数 data 更新需要解密的内容,其编码方式是 'binary','base64' 或 'hex'。如果没有指定编码方式,则把 data 当成 buffer 对象。

  如果 data 是 Buffer,则忽略 input_encoding 参数。

  参数 output_decoding 指定返回文本的格式,是 'binary', 'ascii' 或 'utf8' 之一。如果没有提供编码格式,则返回 buffer。

【decipher.final([output_encoding])】

  返回剩余的解密过的内容,参数 output_encoding 是 'binary', 'ascii' 或 'utf8',如果没有指定编码方式,返回 buffer。

  [注意]decipher对象不能在 final() 方法之后使用。

var crypto = require('crypto');function aesDecrypt(encrypted, key) {
    const decipher = crypto.createDecipher('aes192', key);var decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');return decrypted;
}var data = 'Hello, this is a secret message!';var key = 'Password!';var encrypted = '8a944d97bdabc157a5b7a40cb180e713f901d2eb454220d6aaa1984831e17231f87799ef334e3825123658c80e0e5d0c';var decrypted = aesDecrypt(encrypted, key);
console.log(decrypted);//Hello, this is a secret message!

  可以看出,加密后的字符串通过解密又得到了原始内容。

  注意到AES有很多不同的算法,如aes192aes-128-ecbaes-256-cbc等,AES除了密钥外还可以指定IV(Initial Vector),不同的系统只要IV不同,用相同的密钥加密相同的数据得到的加密结果也是不同的。加密结果通常有两种表示方法:hex和base64,这些功能Nodejs全部都支持,但是在应用中要注意,如果加解密双方一方用Nodejs,另一方用Java、PHP等其它语言,需要仔细测试。如果无法正确解密,要确认双方是否遵循同样的AES算法,字符串密钥和IV是否相同,加密后的数据是否统一为hex或base64格式

【crypto.createCipheriv(algorithm, key, iv)】

  创建并返回一个加密对象,用指定的算法,key 和 iv。

  algorithm 参数和 createCipher() 一致。key 在算法中用到.iv 是一个initialization vector.

  key 和 iv 必须是 'binary' 的编码字符串或buffers.

【crypto.createDecipheriv(algorithm, key, iv)】

  根据传入的算法,密钥和 iv,创建并返回一个解密对象。这是 createCipheriv() 的镜像。

const crypto = require('crypto');function aesEncryptiv(data, key,iv) {
    const cipher = crypto.createCipher('aes192', key, iv);var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');return crypted;
}function aesDecryptiv(encrypted, key,iv) {
    const decipher = crypto.createDecipher('aes192', key, iv);var decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');return decrypted;
}var data = 'Hello, this is a secret message!';var key = 'Password!';var iv = 'match';var encrypted = aesEncryptiv(data, key, iv);var decrypted = aesDecryptiv(encrypted, key, iv);//Hello, this is a secret message!console.log(data);//8a944d97bdabc157a5b7a40cb180e713f901d2eb454220d6aaa1984831e17231f87799ef334e3825123658c80e0e5d0cconsole.log(encrypted);//Hello, this is a secret message!console.log(decrypted);

 

Diffie-Hellman

【crypto.createDiffieHellman(prime[, prime_encoding][, generator][, generator_encoding])】

  使用传入的 prime 和 generator 创建 Diffie-Hellman 秘钥交互对象。

  generator 可以是数字,字符串或Buffer。如果没有指定 generator,使用 2

  prime_encoding 和 generator_encoding 可以是 'binary', 'hex', 或 'base64'。

  如果没有指定 prime_encoding, 则 Buffer 为 prime。如果没有指定 generator_encoding ,则 Buffer 为 generator。

【diffieHellman.generateKeys([encoding])】

  生成秘钥和公钥,并返回指定格式的公钥。这个值必须传给其他部分。编码方式: 'binary', 'hex', 或 'base64'。如果没有指定编码方式,将返回 buffer。

【diffieHellman.getPrime([encoding])】

  用参数 encoding 指明的编码方式返回 Diffie-Hellman 质数,编码方式为: 'binary', 'hex', 或 'base64'。 如果没有指定编码方式,将返回 buffer。

【diffieHellman.getGenerator([encoding])】

  用参数 encoding 指明的编码方式返回 Diffie-Hellman 生成器,编码方式为: 'binary', 'hex', 或 'base64'. 如果没有指定编码方式 ,将返回 buffer。

【diffieHellman.computeSecret(other_public_key[, input_encoding][, output_encoding])】

  使用 other_public_key 作为第三方公钥来计算并返回共享秘密(shared secret)。秘钥用input_encoding 编码。编码方式为:'binary', 'hex', 或 'base64'。如果没有指定编码方式 ,默认为 buffer。

  如果没有指定返回编码方式,将返回 buffer。

DH算法

  DH算法是一种密钥交换协议,它可以让双方在不泄漏密钥的情况下协商出一个密钥来。DH算法基于数学原理,比如小明和小红想要协商一个密钥,可以这么做:

  1、小明先选一个素数和一个底数,例如,素数p=23,底数g=5(底数可以任选),再选择一个秘密整数a=6,计算A=g^a mod p=8,然后大声告诉小红:p=23,g=5,A=8;

  2、小红收到小明发来的p,g,A后,也选一个秘密整数b=15,然后计算B=g^b mod p=19,并大声告诉小明:B=19;

  3、小明自己计算出s=B^a mod p=2,小红也自己计算出s=A^b mod p=2,因此,最终协商的密钥s为2。

  在这个过程中,密钥2并不是小明告诉小红的,也不是小红告诉小明的,而是双方协商计算出来的。第三方只能知道p=23,g=5,A=8,B=19,由于不知道双方选的秘密整数a=6和b=15,因此无法计算出密钥2。

  用crypto模块实现DH算法如下:

var crypto = require('crypto');// xiaoming's keys:var ming = crypto.createDiffieHellman(512);var ming_keys = ming.generateKeys();var prime = ming.getPrime();var generator = ming.getGenerator();//Prime: 8df777257625c66821af697652f28e93af05b9f779af919111b89816faa11c36fcf9df04c76811471a6099800213c4fe8e3fbec8d2f90bd00795e4b7fd241603console.log('Prime: ' + prime.toString('hex'));//Generator: 02console.log('Generator: ' + generator.toString('hex'));// xiaohong's keys:var hong = crypto.createDiffieHellman(prime, generator);var hong_keys = hong.generateKeys();// exchange and generate secret:var ming_secret = ming.computeSecret(hong_keys);var hong_secret = hong.computeSecret(ming_keys);//Secret of Xiao Ming: 4237157ab4c9211f78ffdb67d127d749cec91780d594b81a7e75f1fb591fecb84f33ae6591e1edda4bc9685b503010fe8f9928c6ed69e4ff9fdb44adb9ba1539console.log('Secret of Xiao Ming: ' + ming_secret.toString('hex'));//Secret of Xiao Hong: 4237157ab4c9211f78ffdb67d127d749cec91780d594b81a7e75f1fb591fecb84f33ae6591e1edda4bc9685b503010fe8f9928c6ed69e4ff9fdb44adb9ba1539console.log('Secret of Xiao Hong: ' + hong_secret.toString('hex'))

   [注意]每次输出都不一样,因为素数的选择是随机的。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn