yaofly2012 / note

Personal blog
https://github.com/yaofly2012/note/issues
44 stars 5 forks source link

了解crypto #245

Open yaofly2012 opened 2 years ago

yaofly2012 commented 2 years ago

加密入门

最近刚好要使用crypto模块,发现好些概念不懂,就整理了下。 本文并不能算是算法入门介绍,就是混个脸熟,了解一些相关概念。至于算法本身不属于本文内容(个人能力之外哈)。

基本概念

  1. 加密算法
  2. 密钥

分类

  1. 对称加密
  2. 非对称加密

对称加密

加解密使用相同的密钥。

非对称加密

  1. 私钥
  2. 公钥

两者关系:

用公钥加密的数据必须用私钥才能解开,同样私钥加密的数据只有公钥能解开。但是私钥加密的数据是不安全的,因为公钥是大家都知道的,都可以解密用私钥加密的数据。

那私钥加密数据有什么用呢?

  1. 私钥用于签名、公钥用于验签 私钥具有私有性适合用在签名用途上,防止数据被篡改。

  2. 公钥用于加密、私钥用于解密,这才能起到加密作用

参考

  1. 彻底搞懂HTTPS的加密机制
  2. rsa公钥和私钥到底哪个才是用来加密,哪个用来解密?
yaofly2012 commented 2 years ago

AES对称加密算法

全称:Advanced Encyption Standard,即:高级加密标准 作为DES的替代品。

根据算法密钥的长度(bit),AES有3种不同方案用以满足不同的场景需求,分别是:

  1. AES-128(即16个字节)
  2. AES-192(即24个字节)
  3. AES-256(即32个字节)

秘钥长度递增64bit(8个字节)。

cipher mode ?

  1. CBC
  2. ECB

AES Key ?

参考

  1. 知乎 密码学基础:AES加密算法
  2. How to use the crypto module
  3. AES Encryption and Decryption Online
yaofly2012 commented 2 years ago

Pretty Good Privacy (PGP)

Pretty Good Privacy (PGP) is an encryption system used for both sending encrypted emails and encrypting sensitive files.

image

openpgp使用

passphrase在加密/解密过程中作用

passphrase用来对秘钥进行再次加密,这样在使用私钥时需要先解密私钥。

const openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via window.openpgp

(async () => {

      // put keys in backtick (``) to avoid errors caused by spaces or tabs
    const publicKeyArmored = ``;

    const privateKeyArmored = ``; // encrypted private key
    const passphrase = 'abcd'; // what the private key is encrypted with
    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    // 没有passphrase方式
    // const privateKey = await openpgp.readPrivateKey({ armoredKey: privateKeyArmored })

     // 存在passphrase密码
     const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readPrivateKey({ armoredKey: privateKeyArmored }),
        passphrase
    });
})();

参考

  1. What is PGP Encryption and How Does It Work?
  2. openpgp