-
Notifications
You must be signed in to change notification settings - Fork 0
/
pae256_helpfunctions.h
114 lines (100 loc) · 2.78 KB
/
pae256_helpfunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#ifndef MAIN_CPP_PAE256_HELPFUNCTIONS_H
#define MAIN_CPP_PAE256_HELPFUNCTIONS_H
#include <iostream>
#include <fstream>
#include <ctime>
#include <vector>
//! read filesize from ifstream
unsigned int getFileSize(const std::string& file_name) {
std::ifstream file(file_name);
file.seekg(0, std::ios::end);
return file.tellg();
}
//! switch rows in matrix
inline void switch_rows(char x[16][16], int a, int b) {
char tmp;
for (int i = 0; i < 16; i++) {
tmp = x[a][i];
x[a][i] = x[b][i];
x[b][i] = tmp;
}
}
//! switch columns in matrix
inline void switch_columns(char x[16][16], int a, int b) {
char tmp;
if (a < 16 && b < 16) {
for (int i = 0; i < 16; i++) {
tmp = x[i][a];
x[i][a] = x[i][b];
x[i][b] = tmp;
}
}
}
//! xor each cell in two matrices
inline void matrixXOR(char x[16][16], char y[][16]) {
for (uint8_t i = 0; i < 16; i++) {
for (uint8_t u = 0; u < 16; u++) {
x[i][u] = x[i][u] ^ y[i][u];
}
}
}
//! switch cells in matrix
inline void swapElements(char x[16][16], int a, int b, int c, int d) {
char tmp = x[a][b];
x[a][b] = x[c][d];
x[c][d] = tmp;
}
//! perform XOR of two rows in matrix
inline void rowXOR(char x[16][16], int a, int b) {
for (uint8_t u = 0; u < 16; u++) {
x[u][a] ^= x[u][b];
}
}
//! perform XOR of two columns in matrix
inline void colXOR(char x[16][16], int a, int b) {
for (uint8_t u = 0; u < 16; u++) {
x[a][u] ^= x[b][u];
}
}
//! perform XOR of two cells in matrix
inline void singleXOR(char x[16][16], int a, int b){
x[a][b] ^= x[15-a][15-b];
}
//! transpose matrix
inline void transpose(char matrix[16][16]){
char tmp[16][16];
for(int x = 0; x < 16; x++){
for(int y = 0; y < 16; y++){
tmp[x][y] = matrix[y][x];
}
}
for(int u = 0; u < 16; u++){
for(int v = 0; v < 16; v++){
matrix[u][v] = tmp[u][v];
}
}
}
//! mirrors the key
inline void mirror_key(char matrix[16][16]){
char tmp;
for(int x = 0; x < 16; x++){
for(int y = 0; y < 16; y++){
tmp = matrix[x][y];
matrix[x][y] = matrix[15-x][y];
matrix[15-x][y] = tmp;
}
}
}
//! perform inversion of cell in matrix
inline void invert(char matrix[16][16], int a, int b){
matrix[a][b] = ~matrix[a][b];
}
//! print start screen
void printstart() {
std::cout << "\n";
std::cout << "########################################################################\n";
std::cout << "### PDS Algorithm for Encryption © 2022 Patrick De Smet (V2.2) ###\n";
std::cout << "########################################################################\n";
std::cout << "\n";
}
#endif //MAIN_CPP_PAE256_HELPFUNCTIONS_H