请你按算法加解密流程一步一步结合数学表达式详细解释chacha20流密码算法

2023-05-13by@201905556706-28oo0&@GPT-4.Model: gpt-3.5
201905556706-28oo0

请你按算法加解密流程一步一步结合数学表达式详细解释chacha20流密码算法

Avatar
Avatar

ChaCha20是一种流密码算法,它使用32位字长的分组加密数据。它具有高效的加密速度和较高的安全性。ChaCha20算法使用一个256位的密钥和一个随机的96位的nonce作为输入。本文将按照算法加解密流程一步一步结合数学表达式详细解释ChaCha20流密码算法。

  1. 初始化

首先,需要对ChaCha20算法进行初始化。初始化包括密钥扩展和nonce扩展。密钥扩展使用密钥和常量字符串"expand 32-byte k"生成一个扩展密钥。具体地,将密钥分为8个32位字,然后将这些字按顺序存储在一个矩阵中,得到一个4x4的矩阵。接着,使用常量字符串"expand 32-byte k"对这个矩阵进行4轮加密,得到一个256位的扩展密钥。

nonce扩展使用nonce和常量字符串"expand 16-byte n"生成一个扩展nonce。具体地,将nonce分为3个32位字,然后将这些字按顺序存储在一个矩阵中,得到一个4x3的矩阵。接着,将这个矩阵的第4列设置为常量字符串"expand 16-byte n",然后使用密钥扩展中得到的扩展密钥对这个矩阵进行20轮加密,得到一个384位的扩展nonce。

  1. 加密

一旦完成初始化,就可以开始进行加密。ChaCha20算法将数据分成32位的块,并按照以下方式进行加密:

  • 将扩展密钥和扩展nonce分别存储在两个矩阵中。
  • 将矩阵中的每个元素和一个计数器进行异或操作,得到一个新的矩阵。
  • 对新的矩阵进行20轮加密,得到一个加密后的矩阵。
  • 将加密后的矩阵和原始数据进行异或操作,得到加密后的数据块。

以下是具体的数学表达式:

  • 密钥扩展

将密钥分为8个32位字,按顺序存储在一个矩阵中:

[k0k1k2k3k4k5k6k7k8k9k10k11k12k13k14k15]\begin{bmatrix} k_{0} & k_{1} & k_{2} & k_{3} \\ k_{4} & k_{5} & k_{6} & k_{7} \\ k_{8} & k_{9} & k_{10} & k_{11} \\ k_{12} & k_{13} & k_{14} & k_{15} \end{bmatrix}

使用常量字符串"expand 32-byte k"对这个矩阵进行4轮加密:

v0=0x61707865v1=0x3320646ev2=0x79622d32v3=0x6b206574h0=k0,h1=k1,h2=k2,h3=k3h4=k4,h5=k5,h6=k6,h7=k7h8=k8,h9=k9,h10=k10,h11=k11h12=k12,h13=k13,h14=k14,h15=k15for i=0 to 9h0=h0+h4;h12=rotate left(h12h0,16);h8=h8+h12;h4=rotate left(h4h8,12);h1=h1+h5;h13=rotate left(h13h1,16);h9=h9+h13;h5=rotate left(h5h9,12);h2=h2+h6;h14=rotate left(h14h2,16);h10=h10+h14;h6=rotate left(h6h10,12);h3=h3+h7;h15=rotate left(h15h3,16);h11=h11+h15;h7=rotate left(h7h11,12);h0=h0+k0;h1=h1+k1;h2=h2+k2;h3=h3+k3h4=h4+k4;h5=h5+k5;h6=h6+k6;h7=h7+k7h8=h8+k8;h9=h9+k9;h10=h10+k10;h11=h11+k11h12=h12+k12;h13=h13+k13;h14=h14+k14;h15=h15+k15\begin{aligned} v_{0} &= 0x61707865 \\ v_{1} &= 0x3320646e \\ v_{2} &= 0x79622d32 \\ v_{3} &= 0x6b206574 \\ \\ h_{0} &= k_{0}, h_{1} = k_{1}, h_{2} = k_{2}, h_{3} = k_{3} \\ h_{4} &= k_{4}, h_{5} = k_{5}, h_{6} = k_{6}, h_{7} = k_{7} \\ h_{8} &= k_{8}, h_{9} = k_{9}, h_{10} = k_{10}, h_{11} = k_{11} \\ h_{12} &= k_{12}, h_{13} = k_{13}, h_{14} = k_{14}, h_{15} = k_{15} \\ \\ \text{for } i &= 0 \text{ to } 9 \\ \quad h_{0} &= h_{0} + h_{4}; h_{12} = \text{rotate left}(h_{12} \oplus h_{0}, 16); h_{8} = h_{8} + h_{12}; h_{4} = \text{rotate left}(h_{4} \oplus h_{8}, 12); \\ \quad h_{1} &= h_{1} + h_{5}; h_{13} = \text{rotate left}(h_{13} \oplus h_{1}, 16); h_{9} = h_{9} + h_{13}; h_{5} = \text{rotate left}(h_{5} \oplus h_{9}, 12); \\ \quad h_{2} &= h_{2} + h_{6}; h_{14} = \text{rotate left}(h_{14} \oplus h_{2}, 16); h_{10} = h_{10} + h_{14}; h_{6} = \text{rotate left}(h_{6} \oplus h_{10}, 12); \\ \quad h_{3} &= h_{3} + h_{7}; h_{15} = \text{rotate left}(h_{15} \oplus h_{3}, 16); h_{11} = h_{11} + h_{15}; h_{7} = \text{rotate left}(h_{7} \oplus h_{11}, 12); \\ \\ h_{0} &= h_{0} + k_{0}; h_{1} = h_{1} + k_{1}; h_{2} = h_{2} + k_{2}; h_{3} = h_{3} + k_{3} \\ h_{4} &= h_{4} + k_{4}; h_{5} = h_{5} + k_{5}; h_{6} = h_{6} + k_{6}; h_{7} = h_{7} + k_{7} \\ h_{8} &= h_{8} + k_{8}; h_{9} = h_{9} + k_{9}; h_{10} = h_{10} + k_{10}; h_{11} = h_{11} + k_{11} \\ h_{12} &= h_{12} + k_{12}; h_{13} = h_{13} + k_{13}; h_{14} = h_{14} + k_{14}; h_{15} = h_{15} + k_{15} \\ \end{aligned}

最终得到一个256位的扩展密钥。

  • nonce扩展

将nonce分为3个32位字,按顺序存储在一个矩阵中:

[n0n1n2n3n4n5n6n7n8"expand 16-byte n""expand 16-byte n""expand 16-byte n"]\begin{bmatrix} n_{0} & n_{1} & n_{2} \\ n_{3} & n_{4} & n_{5} \\ n_{6} & n_{7} & n_{8} \\ \text{"expand 16-byte n"} & \text{"expand 16-byte n"} & \text{"expand 16-byte n"} \end{bmatrix}

使用密钥扩展中得到的扩展密钥对这个矩阵进行20轮加密:

\begin{aligned} \text{for } i &= 0 \text{ to } 9 \\ \quad h_{0} &= h_{0} + h_{4}; h_{12} = \text{rotate left}(h_{12} \oplus h_{0}, 16); h_{8} = h_{8} + h_{12}; h_{4} = \text{rotate left}(h_{4} \oplus h_{8}, 12); \\ \quad h_{1} &= h_{1} + h_{5}; h_{13} = \text{rotate left}(h_{13} \oplus h_{1}, 16); h_{9} = h_{9} + h_{13}; h_{5} = \text{rotate left}(h_{5} \oplus h_{9}, 12); \\ \quad h_{2} &= h_{2} + h_{6}; h_{14} = \text{rotate left}(h_{14} \oplus h_{2}, 16); h_{10} = h_{10} + h_{14}; h_{6} = \text{rotate left}(h_{6} \oplus h_{10}, 12
201905556706-28oo0

请你按算法加解密流程一步一步结合伪代码详细解释chacha20流密码算法

Avatar
Avatar

ChaCha20是一种流密码算法,它使用32位字长的分组加密数据。它具有高效的加密速度和较高的安全性。ChaCha20算法使用一个256位的密钥和一个随机的96位的nonce作为输入。本文将按照算法加解密流程一步一步结合伪代码详细解释ChaCha20流密码算法。

  1. 初始化

首先,需要对ChaCha20算法进行初始化。初始化包括密钥扩展和nonce扩展。密钥扩展使用密钥和常量字符串"expand 32-byte k"生成一个扩展密钥。具体伪代码如下:

function key_expansion(key, constant):
    state[16] = key[0..15]
    state[16..19] = constant
    for i = 0 to 9:
        state[0] += state[4]; state[12] = rotate_left(state[12] ^ state[0], 16); state[8] += state[12]; state[4] = rotate_left(state[4] ^ state[8], 12);
        state[1] += state[5]; state[13] = rotate_left(state[13] ^ state[1], 16); state[9] += state[13]; state[5] = rotate_left(state[5] ^ state[9], 12);
        state[2] += state[6]; state[14] = rotate_left(state[14] ^ state[2], 16); state[10] += state[14]; state[6] = rotate_left(state[6] ^ state[10], 12);
        state[3] += state[7]; state[15] = rotate_left(state[15] ^ state[3], 16); state[11] += state[15]; state[7] = rotate_left(state[7] ^ state[11], 12);
    return state

nonce扩展使用nonce和常量字符串"expand 16-byte n"生成一个扩展nonce。具体伪代码如下:

function nonce_expansion(nonce, constant):
    state[16..18] = nonce[0..8]
    state[19..21] = constant
    for i = 0 to 9:
        state[0] += state[4]; state[12] = rotate_left(state[12] ^ state[0], 16); state[8] += state[12]; state[4] = rotate_left(state[4] ^ state[8], 12);
        state[1] += state[5]; state[13] = rotate_left(state[13] ^ state[1], 16); state[9] += state[13]; state[5] = rotate_left(state[5] ^ state[9], 12);
        state[2] += state[6]; state[14] = rotate_left(state[14] ^ state[2], 16); state[10] += state[14]; state[6] = rotate_left(state[6] ^ state[10], 12);
        state[3] += state[7]; state[15] = rotate_left(state[15] ^ state[3], 16); state[11] += state[15]; state[7] = rotate_left(state[7] ^ state[11], 12);
    return state
  1. 加密

一旦完成初始化,就可以开始进行加密。ChaCha20算法将数据分成32位的块,并按照以下方式进行加密:

  • 将扩展密钥和扩展nonce分别存储在两个矩阵中。
  • 将矩阵中的每个元素和一个计数器进行异或操作,得到一个新的矩阵。
  • 对新的矩阵进行20轮加密,得到一个加密后的矩阵。
  • 将加密后的矩阵和原始数据进行异或操作,得到加密后的数据块。

以下是具体的伪代码:

function chacha20(key, nonce, counter, data):
    expanded_key = key_expansion(key, "expand 32-byte k")
    expanded_nonce = nonce_expansion(nonce, "expand 16-byte n")
    for i = 0 to len(data) step 64:
        matrix[16] = expanded_key
        matrix[12..14] = counter
        matrix[4..11] = expanded_nonce
        matrix[0] = 0x61707865, matrix[1] = 0x3320646e, matrix[2] = 0x79622d32, matrix[3] = 0x6b206574
        for j = 0 to 9:
            matrix[0] += matrix[4]; matrix[12] = rotate_left(matrix[12] ^ matrix[0], 16); matrix[8] += matrix[12]; matrix[4] = rotate_left(matrix[4] ^ matrix[8], 12);
            matrix[1] += matrix[5]; matrix[13] = rotate_left(matrix[13] ^ matrix[1], 16); matrix[9] += matrix[13]; matrix[5] = rotate_left(matrix[5] ^ matrix[9], 12);
            matrix[2] += matrix[6]; matrix[14] = rotate_left(matrix[14] ^ matrix[2], 16); matrix[10] += matrix[14]; matrix[6] = rotate_left(matrix[6] ^ matrix[10], 12);
            matrix[3] += matrix[7]; matrix[15] = rotate_left(matrix[15] ^ matrix[3], 16); matrix[11] += matrix[15]; matrix[7] = rotate_left(matrix[7] ^ matrix[11], 12);
        for j = 0 to 15:
            matrix[j] += expanded_key[j]
        for j = 0 to 15:
            matrix[j] = matrix[j] ^ expanded_nonce[j]
        for j = 0 to 15:
            matrix[j] = matrix[j] ^ counter[j]
        for j = 0 to 15:
            output[i+j] = data[i+j] ^ matrix[j]
        counter[0] += 1
    return output

以上是ChaCha20的加密流程。解密流程与加密流程相同,因为ChaCha20是对称密钥算法。

201905556706-28oo0

Qt能不能新建合适的类型文件写入由.txt文本内容经加密后生成的std:string类型数据cipher

Avatar
Avatar

An error occurred with your deployment

EDGE_FUNCTION_INVOCATION_TIMEOUT