The A5/1 encryption algorithm is used for data confidentiality encryption in GSM. This algorithm uses three linear feedback shift registers, denoted as X, Y, and Z. Where X(x0, x1, …, x18) The register has 19 bits, the Y register has 22 bits, and the Z register has 23 bits. The following does not explain the principle, only the key stream generation algorithm.
A simple realization with C++, put a main.cpp text file encryption and decryption in the same directory of the code.
#include <cstdio>
#include <iostream>
#include <cstdlib>
#define _for(i,a,b) for(int i=(a); i<(b); ++i)
#define setbit(x,y) x|=(1<<y)
#define clrbit(x,y) x&=~(1<<y)
#define reversebit(x,y) x^=(1<<y)
#define getbit(x,y) (x&=(1<<y))
using namespace std;
typedef unsigned char uc;
bool x[19] = {1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1};
bool y[22] = {1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1};
bool z[23] = {1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0};
int maj(bool x, bool y, bool z) {
return x + y + z >= 2;
}
void X(bool x[]) {
bool temp = x[13] ^ x[16] ^ x[17] ^ x[18];
for(int i = 18; i >= 1; i--) {
x[i] = x[i-1];
}
x[0] = temp;
}
void Y(bool y[]) {
bool temp = y[20] ^ y[21];
for(int i = 21; i >= 1; i--) {
y[i] = y[i-1];
}
y[0] = temp;
}
void Z(bool z[]) {
bool temp = z[7] ^ z[20] ^ z[21] ^ z[22];
for(int i = 22; i >= 1; i--) {
z[i] = z[i-1];
}
z[0] = temp;
}
uc* generateSecretKeyStream(int n) {
bool temp[8];
uc* secretKeyStream = (uc*)malloc(n/8 + 1);
int cnt = 0, cnt1 = 0;
_for(i, 0, n) {
bool m = maj(x[8], y[10], z[10]);
if(x[8] == m) {
X(x);
}
if(y[10] == m) {
Y(y);
}
if(z[10] == m) {
Z(z);
}
bool s = x[18] ^ y[21] ^ z[22];
temp[cnt++] = s;
if(cnt == 8) {
_for(j, 0, cnt) {
if(temp[j] == 0) {
clrbit(secretKeyStream[cnt1], j);
} else {
setbit(secretKeyStream[cnt1], j);
}
}
cnt1++;
cnt = 0;
}
}
_for(j, 0, cnt) {
if(temp[j] == 0) {
clrbit(secretKeyStream[cnt1], j);
} else {
setbit(secretKeyStream[cnt1], j);
}
}
return secretKeyStream;
}
int getFileSize(FILE *fp) {
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
fseek(fp, 0L, 0);
return size;
}
uc* readFromFile(FILE *fp, int size) {
uc *buffer = (uc *)malloc(sizeof(uc) * size);
fread(buffer, size, 1, fp);
return buffer;
}
int main(int argc, char const **argv) {
FILE* fp = fopen("main.cpp", "r");
if(!fp) {
return -1;
}
int fileSize = getFileSize(fp);
uc *buffer = readFromFile(fp, fileSize);
uc* secretKeyStream = generateSecretKeyStream(fileSize * 8);
_for(i, 0, fileSize) {
buffer[i] ^= secretKeyStream[i];
}
FILE* fp1 = fopen("secret.a51", "w");
fwrite(buffer, fileSize, 1, fp1);
_for(i, 0, fileSize) {
buffer[i] ^= secretKeyStream[i];
}
FILE* fp2 = fopen("_secret.a51", "w");
fwrite(buffer, fileSize, 1, fp2);
fclose(fp);
fclose(fp1);
fclose(fp2);
free(buffer);
return 0;
}
[1] Introduction to RSA The RSA public key encryption algorithm was proposed by Ron Rivest, Adi Shamir and Leonard Adleman in 1977. It was first announced in the United States in July 1987, when all t...
[1] Introduction to SHA Secure Hash Algorithm (SHA) is a secure hash algorithm certified by FIPS. An algorithm that can calculate a fixed-length character string (also called a message digest) corresp...
[1] What is MD5 MD5 stands for Message-Digest Algorithm 5 (message-digest algorithm 5), which is used to ensure complete and consistent information transmission. It is one of the hash algorithms (also...
RSA encryption algorithm brief introduction Note: This article is only a personal summary of myself after I have finished the RSA password. If there is an incorrect place, welcome to the OVO RSA is a ...
Write a custom catalog title here Import the required jar package 1. Download the jar package that Base64 depends on 2. Download the jar package that IOUtils depends on 3. Import dependent packages in...
Recently, I have been involved in elliptic curve encryption in my work. I started to understand the concept of encryption in detail. The purpose of this blog is to record the process of my study and t...
1, Maven Dependent 2, Java code...
South China University of Technology Software College Chen Chunhua (Dr.) [email protected] First, the experimental purpose Encrypt and decrypt the experimental data using the DES algorithm, mast...
Walking on the rivers and lakes, safety first | Asymmetric encryption algorithm design and implementation I. Introduction and implementation of asymmetric encryption algorithms (1) Introduction to asy...