加密模式

流密码

密码学中,流密码(英語:Stream cipher),又譯為串流加密、資料流加密,是一种对称加密算法,加密和解密双方使用相同伪随机加密数据流(pseudo-random stream)作为密钥,明文数据每次与密钥数据流顺次对应加密,得到密文数据流。实践中数据通常是一个位(bit)并用异或(xor)操作加密。

https://zh.wikipedia.org/wiki/%E6%B5%81%E5%AF%86%E7%A0%81

  • chacha20/salsa20
  • rc4

块密码/分组密码

块密码只能对确定长度的数据块进行处理, 如aes每个块的大小必须为16, 如果不满16就需要进行padding

加密模式

ECB

最简单的加密模式即为电子密码本(Electronic codebook,ECB)模式。需要加密的消息按照块密码的块大小被分为数个块,并对每个块进行独立加密。

CBC

在CBC模式中,每个明文块先与前一个密文块进行异或后,再进行加密。在这种方法中,每个密文块都依赖于它前面的所有明文块。同时,为了保证每条消息的唯一性,在第一个块中需要使用初始化向量。

PCBC

填充密码块链接(PCBC,Propagating cipher-block chaining)或称为明文密码块链接(Plaintext cipher-block chaining),是一种可以使密文中的微小更改在解密时导致明文大部分错误的模式,并在加密的时候也具有同样的特性。

块密码 -> 流密码

  • CFB
    密文反馈(CFB,Cipher feedback)模式类似于CBC,可以将块密码变为自同步的流密码;工作过程亦非常相似,CFB的解密过程几乎就是颠倒的CBC的加密过程.
    由于CFB加密没有使用异或, 所以即使CFB是流密码, 但加密解密操作不同.
  • OFB
    输出反馈模式(Output feedback, OFB)可以将块密码变成同步的流密码。它产生密钥流的块,然后将其与明文块进行异或,得到密文。与其它流密码一样,密文中一个位的翻转会使明文中同样位置的位也产生翻转。这种特性使得许多错误校正码,例如奇偶校验位,即使在加密前计算,而在加密后进行校验也可以得出正确结果。
  • CTR
    与OFB相似,CTR将块密码变为流密码。它通过递增一个加密计数器以产生连续的密钥流,其中,计数器可以是任意保证长时间不产生重复输出的函数,但使用一个普通的计数器是最简单和最常见的做法。使用简单的、定义好的输入函数是有争议的:批评者认为它“有意的将密码系统暴露在已知的、系统的输入会造成不必要的风险”。目前,CTR已经被广泛的使用了,由输入函数造成的问题被认为是使用的块密码的缺陷,而非CTR模式本身的弱点。无论如何,有一些特别的攻击方法,例如基于使用简单计数器作为输入的硬件差错攻击。

认证加密(AE)

A typical programming interface for an AE implementation provides the following functions:

  • Encryption
    Input: plaintext, key, and optionally a header in plaintext that will not be encrypted, but will be covered by authenticity protection.
    Output: ciphertext and authentication tag (message authentication code or MAC).
  • Decryption
    Input: ciphertext, key, authentication tag, and optionally a header (if used during the encryption).
    Output: plaintext, or an error if the authentication tag does not match the supplied ciphertext or header.

The header part is intended to provide authenticity and integrity protection for networking or storage metadata for which confidentiality is unnecessary, but authenticity is desired.

https://en.wikipedia.org/wiki/Authenticated_encryption#Authenticated_encryption_with_associated_data_(AEAD)

传统认证加密

  • Encrypt-then-MAC (EtM)
    首先对明文进行加密,然后根据得到的密文生成訊息鑑別碼(MAC)。密文和它的MAC一起发送。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    +---------+
    |plaintext|
    +---------+
    |
    v
    +----------+
    |Encryption|<------key1
    +----------+
    |
    |--------------+
    | |
    | v
    | +-------------+
    | |hash function|<-------key2
    | +-------------+
    | |
    v v
    +----------------+------+
    | Ciphertext | MAC |
    +----------------+------+
  • Encrypt-and-MAC (E&M)
    基于明文生成MAC,并且明文在没有MAC的情况下被加密。明文的MAC和密文一起发送。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    +---------+
    |plaintext|
    +---------+
    |
    |--------------------------------+
    | |
    v v
    +----------+ +-------------+
    |Encryption|<------key1----> |hash function|
    +----------+ +-------------+
    | |
    v v
    +------------------------------+--------+
    | Ciphertext | MAC |
    +------------------------------+--------+
  • MAC-then-Encrypt (MtE)
    基于明文生成MAC,然后将明文和MAC一起加密以基于两者生成密文。密文(包含加密的MAC)被发送。MtE方法本身并未被证明是“强不可伪造”的。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    +---------+
    |plaintext| key
    +---------+ |
    | |
    |------------------+ |
    | | |
    | v |
    | +-------------+ |
    | |hash function|<-------|
    | +-------------+ |
    | | |
    v v |
    +------------------+------+ |
    | plaintext | MAC | |
    +------------------+------+ |
    | |
    v |
    +----------------+ |
    | Encryption |<--------------+
    +----------------+
    |
    v
    +-------------+
    | Ciphertext |
    +-------------+

AEAD(带有关联数据的认证加密)

chacha20-poly1305

chacha20-poly1305是将流密码chahca20和消息验证码(MAC)poly1305组合而成的AEAD.

chacha20-poly1305加密过程
chacha20-enc

chacha20-poly1305解密
chacha20-dec