RC4系列

简单解释一下RC4

  • RC4作为一个对称加密,加解密共用一套代码
  • 它本质上就是一个单字节的异或加密
  • 加密分为初始化和加密阶段
    • 初始化会对s盒进行赋值,密钥填充,置换
    • 最后在按一定规则从s盒中选取字节与明文进行异或得到密文

python自带库


from Crypto.Cipher import ARC4
key = b"UKFC"
data = b'Re__Point'
rc4 = ARC4.new(key)
data = rc4.decrypt(data)
print(data)
# b'v9\xfb&\x0bgS\x95<'

C++手动的实现

#include 
typedef unsigned longULONG;

/*初始化函数*/
void rc4_init(unsigned char*s, unsigned char*key, unsigned long Len)
{
    int i = 0, j = 0;
    char k[256] = { 0 };
    unsigned char tmp = 0;
    for (i = 0; i<256; i++)
    {
        s[i] = i;
        k[i] = key[i%Len];
    }
    for (i = 0; i<256; i++)
    {
        j = (j + s[i] + k[i]) % 256;
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }
}

/*加解密*/
void rc4_crypt(unsigned char*s, unsigned char*Data, unsigned long Len)
{
    int i = 0, j = 0, t = 0;
    unsigned long k = 0;
    unsigned char tmp;
    for (k = 0; k

你可能感兴趣的:(CTF,python,算法,安全)