"stream cipher (sequence cipher) and RC4 algorithm"
RC4 is a sequence cipher, which is a variable key length, byte-oriented sequence cipher, a plaintext byte that differs from a key byte, or produces a secret text section.
algorithm principle: for n = 8 bit long words, that is, in one byte, at this time n = 256, with a variable length key from 1 to 256 bytes to initialize a 256-byte state vector arr,arr element arr[0],arr[1],,,arr[255], Arr from start to finish contains all 8 bits of 0~255, key key in the key stream is selected by 256 elements in ARR to act as a certain way; the elements in Arr are replaced once for each key value generated.
Initialization
Initialize the element in arr to arr[0] = 0,arr[1] = 1,,,,, arr[255] = 255, the length of the key is Len, and for each element in arr, the element in the arr[i] is replaced by the key K, Because only the elements in the array are in a disordered arrangement, the array arr also contains 0~255 elements.
Initializes the core code for (int i = 0; i < i++) {Arr[i] = i; K[i] = key[i% len]; The K array is a loop of the elements in the key in the array}//The data in the array is arranged in a disordered order for (int i = 0; i < i++) {j = (j + arr[i] + k[i])% 256; TMP = Arr[i]; Arr[i] = Arr[j]; ARR[J] = tmp; }
when encrypting, the input plaintext is different from the key or, when decrypted, the ciphertext is different from the key.
The core code for encryption and decryption for (size_t j = 0; J < Len; J + +) {x = (x + 1)% 256; y = (y + arr[x])% 256; TMP = arr[x]; ARR[X] = Arr[y]; Arr[y] = tmp; T = (Arr[x] + arr[y])% 256; PTR[J] ^= arr[t]; }
The following is the complete program:
Header file "stream.h" #pragma once//sequence password (stream password) #include &NBSP;<STRING.H>VOID&NBSP;RC4 (char* arr, Char* ptr, size_t len) //to encrypt or decrypt, arr is the key, PTR is plaintext { int x = -1; int y = 0; int t = 0; char tmp = 0; for (size_t j = 0; j < len; j++) { x = (x + 1) % 256; y = (Y + arr[x]) % 256; tmp = arr[x]; arr[x] = arr[y]; arr[y] = tmp; t = ( Arr[x] + arr[y]) % 256; ptr[j] ^= arr[t]; }} Void init (Char *arr, char* key, size_t len) //initialization key , (Key scheduling algorithm KSA) { int j = 0; char k[256] = { 0 }; char tmp = 0; for ( int i = 0; i < 256; i++) { arr[i] = i; k[i] = key[i The % len]; //k array is a loop of elements in a key in an array } //the data in the array in a disordered arrangement for (int i = 0; i < 256; i++) { j = (j + Arr[i] + k[i]) % 256; tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; }}void menu (char *arr, char* key, char* ptr) //main Menu { char n, ch; char arr1[256] = { 0 }; printf ("**********************************\n"); printf ("*********&NBSP;&NBSP;--RC4 algorithm *********\n"); printf ("********* 1. Encryption and decryption *********\n"); printf ("********* 0. Exit *********\n "); printf (" ****** \ n "); printf ("---Please select: "); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%c ", &NBSP;&N); switch (n) { case ' 1 ': printf ("Please enter plaintext that needs to be encrypted:"); fflush (stdin); //Clear Buffer for (int i = 0; i < 256; i++) { if ((Ch = getchar ()) != ' \ n ') { ptr[i] = ch; } else { break; } } init (Arr, key, strlen (key)); for (int i = 0; i < 256; i++) //Saving the initialized arr { arr1[i] = arr[i]; } &NBSP;&NBSP;RC4 (Arr, ptr, strlen (PTR)); //Encryption printf ("\ n output ciphertext:"); printf ("%s", ptr); &nbSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RC4 (Arr1, ptr, strlen (PTR)) //decrypt printf ("\ n the output of the plaintext is: "); printf ("%s ", ptr); break; case ' 0 ': exit (exit_failure); default: break ; }}
Source Files Stream.cpp#define _crt_secure_no_warnings 1#include <stdio.h> #include <stdlib.h> #include "stream.h "Int main () {char arr[256] = {0}; Char key[256] = {"Zheshiyigemiyao"}; Char ptr[256] = {0}; Menu (arr, key, PTR); System ("pause"); return 0;}
This article from "Unintentional persistent" blog, declined reprint!
Stream cipher (sequence cipher) and RC4 algorithm