-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gost28147 #2
base: develop
Are you sure you want to change the base?
Gost28147 #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
cmake_minimum_required(VERSION 2.8) # �������� ������ CMake. | ||
# ���� ������ ������������ ��������� | ||
# ������ ��������, ��������� ��������� �����. | ||
project(Gost28147) | ||
|
||
add_definitions(-D_CRT_SECURE_NO_WARNINGS) | ||
|
||
include_directories(.) | ||
|
||
list(APPEND sources | ||
./Gost28147.cpp | ||
./main.cpp) | ||
|
||
add_executable(Gost28147 ${sources}) # ������� ����������� ���� � ������ <name> | ||
# �� ��������� <name>.cpp |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#include "Gost28147.h" | ||
|
||
void Gost28147_encode(uint8_t* blockin, uint8_t* blockout, uint32_t* Key, uint8_t** SubTable) | ||
{ | ||
uint8_t first_uint8_t, second_uint8_t, zam_symbol, n; | ||
int8_t q; | ||
|
||
uint32_t n1 = 0, n2 = 0, SUM232 = 0; // ���������� N1, N2, � �������� | ||
|
||
n1 = *((uint32_t *)&blockin[0]); | ||
n2 = *((uint32_t *)&blockin[4]); | ||
|
||
// 32 ����� ������� ������ | ||
// ���� ��������� � ��������� ������ ������� | ||
int8_t c = 0; | ||
for (uint8_t k = 0; k<32; k++) | ||
{ | ||
if (k == 24) | ||
c = 7; | ||
|
||
// ��������� � ��������� ��1 | ||
SUM232 = Key[c] + n1; | ||
|
||
// �������� �� ������� ����� | ||
first_uint8_t = 0, second_uint8_t = 0, zam_symbol = 0; | ||
n = 7; | ||
for (q = 3; q >= 0; q--) | ||
{ | ||
zam_symbol = *((uint8_t *)&SUM232 + q); | ||
first_uint8_t = (zam_symbol & 0xF0) >> 4; | ||
second_uint8_t = (zam_symbol & 0x0F); | ||
first_uint8_t = SubTable[n][first_uint8_t]; | ||
n--; | ||
second_uint8_t = SubTable[n][second_uint8_t]; | ||
n--; | ||
zam_symbol = (first_uint8_t << 4) | second_uint8_t; | ||
*((uint8_t *)&SUM232 + q) = zam_symbol; | ||
} | ||
|
||
SUM232 = (SUM232 << 11) | (SUM232 >> 21); // ����������� ����� �� 11 | ||
SUM232 = n2^SUM232; // ���������� � ��������� ��2 | ||
|
||
if (k<31) | ||
{ | ||
n2 = n1; | ||
n1 = SUM232; | ||
} | ||
|
||
if (k<24) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Может быть будет проще c = k % 8 ??? Одна строчка и никаких if. Чем меньше в коде условий, тем лучше |
||
{ | ||
c++; | ||
if (c>7) c = 0; | ||
} | ||
else | ||
{ | ||
c--; | ||
if (c<0) c = 7; | ||
} | ||
} | ||
n2 = SUM232; | ||
|
||
// ���������� ��������� | ||
uint8_t ind = 0; | ||
for (q = 0; q <= 3; q++) | ||
blockout[ind++] = *((uint8_t *)&n1 + q); | ||
|
||
for (q = 0; q <= 3; q++) | ||
blockout[ind++] = *((uint8_t *)&n2 + q); | ||
} | ||
|
||
void Gost28147_decode(uint8_t* blockin, uint8_t* blockout, uint32_t* Key, uint8_t** SubTable) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У тебя до этого была хорошая идея, что кодирование и декодирование делалось одним методом, но оформил ты её плохо. На чистом языке Си это можно сделать так где-нибудь сразу после #include в этом файле обяъвляешь прототип функции (обрати внимание на названия переменных и метода)
|
||
{ | ||
uint8_t first_uint8_t, second_uint8_t, zam_symbol, n; | ||
int8_t q; | ||
|
||
uint32_t n1 = 0, n2 = 0, SUM232 = 0; // ���������� N1, N2, � �������� | ||
|
||
n1 = *((uint32_t *)&blockin[0]); | ||
n2 = *((uint32_t *)&blockin[4]); | ||
|
||
// 32 ����� ������� ������ | ||
// ���� ��������� � ��������� ������ ������� | ||
int8_t c = 0; | ||
for (uint8_t k = 0; k<32; k++) | ||
{ | ||
if (k == 8) c = 7; | ||
|
||
// ��������� � ��������� ��1 | ||
SUM232 = Key[c] + n1; | ||
|
||
// �������� �� ������� ����� | ||
first_uint8_t = 0, second_uint8_t = 0, zam_symbol = 0; | ||
n = 7; | ||
for (q = 3; q >= 0; q--) | ||
{ | ||
zam_symbol = *((uint8_t *)&SUM232 + q); | ||
first_uint8_t = (zam_symbol & 0xF0) >> 4; | ||
second_uint8_t = (zam_symbol & 0x0F); | ||
first_uint8_t = SubTable[n][first_uint8_t]; | ||
n--; | ||
second_uint8_t = SubTable[n][second_uint8_t]; | ||
n--; | ||
zam_symbol = (first_uint8_t << 4) | second_uint8_t; | ||
*((uint8_t *)&SUM232 + q) = zam_symbol; | ||
} | ||
|
||
SUM232 = (SUM232 << 11) | (SUM232 >> 21); // ����������� ����� �� 11 | ||
SUM232 = n2^SUM232; // ���������� � ��������� ��2 | ||
|
||
if (k<31) | ||
{ | ||
n2 = n1; | ||
n1 = SUM232; | ||
} | ||
|
||
if (k<8) | ||
{ | ||
c++; | ||
if (c>7) c = 0; | ||
} | ||
else | ||
{ | ||
c--; | ||
if (c<0) c = 7; | ||
} | ||
} | ||
n2 = SUM232; | ||
|
||
// ���������� ��������� | ||
uint8_t ind = 0; | ||
for (q = 0; q <= 3; q++) | ||
blockout[ind++] = *((uint8_t *)&n1 + q); | ||
|
||
for (q = 0; q <= 3; q++) | ||
blockout[ind++] = *((uint8_t *)&n2 + q); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef GOST28147LIB | ||
#define GOST28147LIB | ||
|
||
#include <stdint.h> | ||
|
||
/** | ||
* \brief ����� �������, ����������� �������� ���������� �� ���� 28147-89. | ||
* \version 1.0. | ||
* \date 29 ����� 2017 ����. | ||
* | ||
* �������� ������������������ �������������� ������ � ��������� <a href="http://www.certisfera.ru/uploads/28147-89.pdf">���� 28147-89</a>. | ||
* ��������� ����� ������������ (�������������) ������ - ����� ������� ������. | ||
* ���� ������ 256 ��� ����������� �� 8 32-������ ������. | ||
* ���� ����������� (������� �����) ������� �� 8-� ����� ������, ������ �� ������� ����� ����� 64 ����. | ||
* ����������� �� ���� ����������� 32-��������� ������ ����������� �� 8 ��������������� ������ 4-��������� | ||
* ��������, ������ �� ������� ������������� � 4-��������� ������ ��������������� ����� ������, �������������� | ||
* ����� ������� �� ����������� �����, ���������� �� ������ ���� ���������� � ������. ������� ������ ���������� | ||
* ����� ������ � �������, ���������� ������ ������ �������� �������� ��������. ����� 4-��������� �������� | ||
* ������� ��������������� ������������ � 32-��������� ������. | ||
*/ | ||
|
||
///< ������� Gost28147_encode ��������� ������������ �� ���� 28147-89. | ||
void Gost28147_encode(uint8_t* blockin, uint8_t* blockout, uint32_t* Key, uint8_t** SubTable); | ||
///< ������� Gost28147_encode ��������� ������������� �� ���� 28147-89. | ||
void Gost28147_decode(uint8_t* blockin, uint8_t* blockout, uint32_t* Key, uint8_t** SubTable); | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "Gost28147.h" | ||
|
||
long filesize(FILE *stream); | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
if (argc != 4) | ||
{ | ||
printf("Usage:\n\tgost-28147-89.exe <mode> <path to source file> <path to target file>\n\tmode = 1 - encoding\n\tmode = 2 - decoding\nInput any key to continue..."); | ||
getchar(); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
uint32_t* Key = (uint32_t*)malloc(8 * sizeof(uint32_t)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Уже хорошо, что не в цикле. Но зачем ты вообще делаешь динамическое выделение памяти? Почему нельзя просто объявить массив key[] = {0x0123,...}, тоже самое для subTable |
||
Key[0] = 0x0123; | ||
Key[1] = 0x4567; | ||
Key[2] = 0x89AB; | ||
Key[3] = 0xCDEF; | ||
Key[4] = 0x0123; | ||
Key[5] = 0x4567; | ||
Key[6] = 0x89AB; | ||
Key[7] = 0xCDEF; | ||
|
||
uint8_t** SubTable; | ||
SubTable = (uint8_t**)malloc(8 * sizeof(uint8_t*)); | ||
for (int i = 0; i < 8; i++) | ||
{ | ||
SubTable[i] = (uint8_t*)malloc(16 * sizeof(uint8_t)); | ||
for (int j = 0; j < 16; j++) | ||
SubTable[i][j] = j; | ||
} | ||
|
||
// �����, 1 - ������������, 2 - ������������� | ||
int mode; | ||
sscanf(argv[1], "%d", &mode); | ||
|
||
FILE *f_in, *f_out; // ������ ��� �������� � ��������� ������ | ||
|
||
// ��������� ����� | ||
f_in = fopen(argv[2], "rb"); | ||
f_out = fopen(argv[3], "wb"); | ||
|
||
// ��������� ������� ������ | ||
long inputsize = filesize(f_in); | ||
|
||
// ���������� ������ ������� ������ | ||
float blokoff; | ||
blokoff = (float)8 * inputsize; | ||
blokoff = blokoff / 64; | ||
int blockN = (int)blokoff; | ||
if (blokoff - blockN>0) blockN++; | ||
|
||
int sh; | ||
if (inputsize >= 4) sh = 4; else sh = inputsize; | ||
int sh1 = 0; | ||
int flag = 0; | ||
|
||
uint8_t blockin[9]; | ||
uint8_t blockout[9]; | ||
blockin[8] = '\0'; | ||
blockout[8] = '\0'; | ||
|
||
for (int i = 0; i < blockN; i++) | ||
{ | ||
for (int q = 0; q < 8; q++) | ||
{ | ||
*((uint8_t *)&blockin + q) = 0x00; | ||
*((uint8_t *)&blockout + q) = 0x00; | ||
} | ||
|
||
if ((sh1 + sh) < inputsize) | ||
{ | ||
fread(&blockin[0], sh, 1, f_in); | ||
sh1 += sh; | ||
} | ||
else | ||
{ | ||
sh = inputsize - sh1; | ||
fread(&blockin[0], sh, 1, f_in); | ||
flag = 1; | ||
} | ||
|
||
if ((sh1 + sh) < inputsize) | ||
{ | ||
fread(&blockin[4], sh, 1, f_in); | ||
sh1 += sh; | ||
} | ||
else | ||
{ | ||
if (flag == 0) | ||
{ | ||
sh = inputsize - sh1; | ||
fread(&blockin[4], sh, 1, f_in); | ||
} | ||
} | ||
|
||
if (mode == 1) | ||
Gost28147_encode(blockin, blockout, Key, SubTable); | ||
else if (mode == 2) | ||
Gost28147_decode(blockin, blockout, Key, SubTable); | ||
else | ||
{ | ||
printf("Wrong mode. Exit. Press any key to continue..."); | ||
getchar(); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
// ������� ��������� � ���� | ||
fwrite(&blockout[0], 8, 1, f_out); | ||
} | ||
|
||
// ��������� ����� | ||
fclose(f_in); | ||
fclose(f_out); | ||
|
||
return EXIT_SUCCESS; | ||
} | ||
|
||
long filesize(FILE * stream) | ||
{ | ||
long curpos, length; | ||
curpos = ftell(stream); | ||
fseek(stream, 0L, SEEK_END); | ||
length = ftell(stream); | ||
fseek(stream, curpos, SEEK_SET); | ||
return length; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Очень плохие имена переменных! Почитай вот это https://learn.javascript.ru/variable-names