【C语言】RC4 测试代码

RC4 测试代码

#include<stdio.h>

// SBOX 大小
#define N 256

// 加解密
void rc4_crypt(unsigned char key[], size_t len, unsigned char* data, size_t size)
{
    unsigned char tmp = 0;
    unsigned char sbox[N];

    // 初始化 S-BOX
    for (size_t i = 0; i < N; ++ i)
    {
        sbox[i] = i;
    }
    for (size_t i = 0, j = 0; i < N; ++ i)
    {
        j = (j + sbox[i] + key[i % len]) % N;

        tmp = sbox[i];
        sbox[i] = sbox[j];
        sbox[j] = tmp;
    }

    // XOR 流
    for (size_t i = 0, j = 0, t = 0, k = 0; k < size; ++ k)
    {
        i = (i + 1) % N;
        j = (j + sbox[i]) % N;
        t = (sbox[i] + sbox[j]) % N;
        data[k] ^= sbox[t];

        tmp = sbox[i];
        sbox[i] = sbox[j];
        sbox[j] = tmp;
    }
}

int main(int argc, char* argv[])
{
    unsigned char key[4] = {0x61, 0x62, 0x63, 0x64};
    unsigned char pData[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
                            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
                            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
                            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
                            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    size_t len = sizeof(pData) - 1;

    printf("key = %c%c%c%c\n", key[0], key[1], key[2], key[3]);

    // 加密
    rc4_crypt(key, sizeof(key), pData, len);
    for (int i = 0; i < 10; ++ i) {
            for (int j = 0; j < 31; ++ j) {
                printf("%02x", pData[j + i * 31]);
            }
            printf("\n");
        }

    // 解密
    rc4_crypt(key, sizeof(key), pData, len);
    for (int i = 0; i < 5; ++ i) {
        for (int j = 0; j < 62; ++ j) {
            printf("%c", pData[j + i * 62]);
        }
        printf("\n");
    }

    return 0;
}
key = abcd
b70b2426e226483767fd5f54032f4b62c754f3d9ab9ccfa9886cbd32809b82
c23c0ffa8db38075b1d635e445aef80e3905002fe60e3fbff4fdc18ca78f87
5bcae3920a41bb08e358dc9236434de71df57a9bb21320b7355ee59223ad0e
7de7b8f42a991a24be47bea7501715bf4c8362226f89938739369cc9f313e0
648b66b37d6dfe399cf59c85ba9251a9ad89df7ef95489e04b19fab649ddca
afd312272e98723d07b179d471008e08bff3d00364721c1909af233137819b
2e5301f9800786b5b2b2e5ef2e90d5ab1a8bd4bc9eb39af76139c17bcb1a7d
e34cf9325cfa9547bf3485fd6195f4b299194e2e791f1b8a97d5bd0caf794f
dedc82588f40a81c4153f7ac215bded8a36a512b2f5e87ce1f70b68deaa77c
36cba0228085ddd26f126b48cc6c36936ba584619345867f9b307b65bee51c
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

相关推荐

  1. C语言RC4 测试代码

    2024-03-28 11:36:02       46 阅读
  2. C++测试代码

    2024-03-28 11:36:02       41 阅读
  3. c语言考试代码

    2024-03-28 11:36:02       67 阅读
  4. C语言】(4)数组

    2024-03-28 11:36:02       51 阅读
  5. <span style='color:red;'>C</span><span style='color:red;'>语言</span>-<span style='color:red;'>4</span>

    C语言-4

    2024-03-28 11:36:02      43 阅读
  6. C语言--指针4

    2024-03-28 11:36:02       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 11:36:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 11:36:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 11:36:02       87 阅读
  4. Python语言-面向对象

    2024-03-28 11:36:02       96 阅读

热门阅读

  1. el-upload上传文件前端自己读取excel

    2024-03-28 11:36:02       38 阅读
  2. uniapp H5 开发,公众号时请求跨域了,要用proxy

    2024-03-28 11:36:02       44 阅读
  3. Nginx服务

    2024-03-28 11:36:02       43 阅读
  4. Docker Compose 中的网络配置和优先级管理

    2024-03-28 11:36:02       45 阅读
  5. 无感刷新token

    2024-03-28 11:36:02       46 阅读
  6. CSS选择器 个人练习笔记

    2024-03-28 11:36:02       43 阅读
  7. 蓝桥杯-双指针

    2024-03-28 11:36:02       38 阅读
  8. JUC/多线程锁的用法(二)

    2024-03-28 11:36:02       36 阅读