RC4 algorithm C language (separating algorithm from OpenSSL Library: I)

RC4 algorithm C language (separating algorithm from OpenSSL Library: I)

Introduction to OpenSSL:

OpenSSL is a powerful, commercial and fully functional toolkit for transport layer security (TLS) and secure socket layer (SSL) protocols. It is also a general cryptography library. Including RSA, SM4, DES, AES and many other encryption algorithms.

The OpenSSL GitHub address is as follows:

GitHub - openssl/openssl: TLS/SSL and crypto library

In daily development work, sometimes you only want to use an algorithm in the OpenSSL library. At this time, it is often unnecessary to call the whole OpenSSL library; Or if we use an algorithm on the embedded platform, we only transplant this algorithm, and there is no need to transplant the whole OpenSSL library.

RC4 introduction:

RC4 was developed by Ronald Levitt in 1987. Although its official name is "Rivest Cipher 4", the acronym RC can also be understood as "Ron's Code"

RC4 is a very popular stream encryption technology. Because encryption and decryption use the same secret key, it is a symmetric encryption algorithm. Moreover, the data length remains unchanged before and after RC4 encryption. There is no grouping and blocking, and there is no need to supplement bytes. Therefore, it has great advantages in the field of data transmission.

1, Download code

Download the code from the Github repository of OpenSSL. You can download the master branch or the latest release version. Unzip the downloaded file to get the code.

Focus on the red box Directory:

  • The crypto directory contains various encryption algorithms
  • The include directory is the header file of the external interface of each encryption algorithm

2, Prepare environment

Create a new Visual Studio C + + console project "rc4test". The compilation options are: x86\Debug

3, Prepare documents

  1. Copy the: / crypto/rc4 folder in the OpenSSL source code to the VS project code directory
  2. Copy the: / include/openssl/rc4.h file in the OpenSSL source code to the rc4 folder in the VS project code directory
  3. After copying, the files in the VS project code directory rc4 folder are as follows:


Delete the following useless files in this project in the directory:

  • asm
  • build.info
  • rc4_local.h

4, Modify code

Modify call file

In the main file containing main function in VS Project: rc4test.cpp, add the header file containing rc4.h

#include "rc4/rc4.h"

Add the following code to the main function

    const char* Rc4Key = "12345678";
    RC4_KEY key;
    RC4_set_key(&key, strlen((const char *)Rc4Key), (unsigned char*)Rc4Key);

    const char* indata = "ABCG";
    unsigned char Encrydata[256] = {0};
    RC4(&key, strlen((const char*)indata),(unsigned char*) indata, Encrydata);
    
    unsigned char Decrydata[256] = { 0 };
    RC4_KEY key2;
    RC4_set_key(&key2, strlen((const char*)Rc4Key), (unsigned char*)Rc4Key);
    RC4(&key2, 7, (unsigned char*)Encrydata, Decrydata);

The modified md5test.cpp code is as follows:

#include <iostream>
#include "rc4/rc4.h"

int main()
{
    const char* Rc4Key = "12345678";
    RC4_KEY key;
    RC4_set_key(&key, strlen((const char*)Rc4Key), (unsigned char*)Rc4Key);

    const char* indata = "ABCG";
    unsigned char Encrydata[256] = { 0 };
    RC4(&key, strlen((const char*)indata), (unsigned char*)indata, Encrydata);

    unsigned char Decrydata[256] = { 0 };
    RC4_KEY key2;
    RC4_set_key(&key2, strlen((const char*)Rc4Key), (unsigned char*)Rc4Key);
    RC4(&key2, strlen((const char*)Encrydata), (unsigned char*)Encrydata, Decrydata);
}

Modify rc4.h

Delete conditional compilation OPENSSL_RC4_H

Delete conditional compilation OPENSSL_NO_DEPRECATED_3_0

Delete conditional compilation OPENSSL_NO_DEPRECATED_3_0

Delete conditional compilation OPENSSL_NO_RC4

Delete header file # include < OpenSSL / macros. H >

Delete header file # include < OpenSSL / opensslconf. H >

Delete function type macro OSSL_DEPRECATEDIN_3_0

The modified or deleted files are as follows:

# pragma once
#  include <stddef.h>
#  ifdef  __cplusplus
extern "C" {
#  endif

typedef struct rc4_key_st {
    RC4_INT x, y;
    RC4_INT data[256];
} RC4_KEY;

const char *RC4_options(void);
void RC4_set_key(RC4_KEY *key, int len,
                 const unsigned char *data);
void RC4(RC4_KEY *key, size_t len,
                               const unsigned char *indata,
                               unsigned char *outdata);

#  ifdef  __cplusplus
}
#  endif

Modify rc4_enc.c

Delete header file # include "internal/deprecated.h"

Delete header file # include "rc4# local. H"

Modify the header file # include < OpenSSL / RC4. H > to #include "rc4.h"

The modified or deleted files are as follows:

#include "rc4.h"

/*-
 * RC4 as implemented from a posting from
 * Newsgroups: sci.crypt
 * Subject: RC4 Algorithm revealed.
 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
 * Date: Wed, 14 Sep 1994 06:35:31 GMT
 */

void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
         unsigned char *outdata)
{
    register RC4_INT *d;
    register RC4_INT x, y, tx, ty;
    size_t i;

    x = key->x;
    y = key->y;
    d = key->data;

#define LOOP(in,out) \
                x=((x+1)&0xff); \
                tx=d[x]; \
                y=(tx+y)&0xff; \
                d[x]=ty=d[y]; \
                d[y]=tx; \
                (out) = d[(tx+ty)&0xff]^ (in);

    i = len >> 3;
    if (i) {
        for (;;) {
            LOOP(indata[0], outdata[0]);
            LOOP(indata[1], outdata[1]);
            LOOP(indata[2], outdata[2]);
            LOOP(indata[3], outdata[3]);
            LOOP(indata[4], outdata[4]);
            LOOP(indata[5], outdata[5]);
            LOOP(indata[6], outdata[6]);
            LOOP(indata[7], outdata[7]);
            indata += 8;
            outdata += 8;
            if (--i == 0)
                break;
        }
    }
    i = len & 0x07;
    if (i) {
        for (;;) {
            LOOP(indata[0], outdata[0]);
            if (--i == 0)
                break;
            LOOP(indata[1], outdata[1]);
            if (--i == 0)
                break;
            LOOP(indata[2], outdata[2]);
            if (--i == 0)
                break;
            LOOP(indata[3], outdata[3]);
            if (--i == 0)
                break;
            LOOP(indata[4], outdata[4]);
            if (--i == 0)
                break;
            LOOP(indata[5], outdata[5]);
            if (--i == 0)
                break;
            LOOP(indata[6], outdata[6]);
            if (--i == 0)
                break;
        }
    }
    key->x = x;
    key->y = y;
}

Modify rc4_skey.c

Delete header file # include "internal/deprecated.h"

Modify the header file # include < OpenSSL / RC4. H > to #include "rc4.h"

Delete header file # include "rc4# local. H"

Delete header file #include < OpenSSL / OpenSSL. H >

The modified or deleted files are as follows:

#include "rc4.h"

const char *RC4_options(void)
{
    if (sizeof(RC4_INT) == 1)
        return "rc4(char)";
    else
        return "rc4(int)";
}

/*-
 * RC4 as implemented from a posting from
 * Newsgroups: sci.crypt
 * Subject: RC4 Algorithm revealed.
 * Message-ID: <sternCvKL4B.Hyy@netcom.com>
 * Date: Wed, 14 Sep 1994 06:35:31 GMT
 */

void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
{
    register RC4_INT tmp;
    register int id1, id2;
    register RC4_INT *d;
    unsigned int i;

    d = &(key->data[0]);
    key->x = 0;
    key->y = 0;
    id1 = id2 = 0;

#define SK_LOOP(d,n) { \
                tmp=d[(n)]; \
                id2 = (data[id1] + tmp + id2) & 0xff; \
                if (++id1 == len) id1=0; \
                d[(n)]=d[id2]; \
                d[id2]=tmp; }

    for (i = 0; i < 256; i++)
        d[i] = i;
    for (i = 0; i < 256; i += 4) {
        SK_LOOP(d, i + 0);
        SK_LOOP(d, i + 1);
        SK_LOOP(d, i + 2);
        SK_LOOP(d, i + 3);
    }
}

5, Compile run

Set rc4.h,rc4_enc.c,rc4_skey.c file, add the configuration to the VS project.

When compiling, an error will be reported. VS is located in the structure rc4.h_ key_ St statement, the reason is RC4_INT type does not exist and is not defined. This RC4_INT is generated by Perl script according to the configuration file and parameters during the compilation of the whole OpenSSL project. Here, we only need to modify RC4 before the structure declaration_ Int is defined as follows:

typedef int RC4_INT;

Compile and run again

Observe the changes of Encrydata and decryptdata memory data

Welcome to pay attention, leave a message for discussion and share the source code

Keywords: C Algorithm

Added by n00b Saibot on Mon, 01 Nov 2021 00:57:49 +0200