Skip to content

Instantly share code, notes, and snippets.

@iSarCasm
Created February 15, 2016 12:04
Show Gist options
  • Save iSarCasm/e43fdb617201e31fa4d3 to your computer and use it in GitHub Desktop.
Save iSarCasm/e43fdb617201e31fa4d3 to your computer and use it in GitHub Desktop.
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
using namespace std;
bitset<8> rotate(std::bitset<8> b)
{
bitset<8> result = b;
bool buffer[2] = { b[0], b[1]}; // сохраняем младшие 2 бита (последние)
result = result >> 2;
result.set(3, buffer[0]);
result.set(2, buffer[1]);
return result;
}
string to_s(std::bitset<2> b)
{
return b.to_string<char, string::traits_type, string::allocator_type>();
}
bitset<8> read_transport(char c1, char c2)
{
bitset<4> left = bitset<4>((int)c1 - 32);
bitset<4> right = bitset<4>((int)c2 - 32);
cout << c1 << " + " << c2 << " = " << (left.to_string<char, string::traits_type, string::allocator_type>() + right.to_string<char, string::traits_type, string::allocator_type>()) << endl;
return (bitset<8>)(left.to_string<char, string::traits_type, string::allocator_type>() + right.to_string<char, string::traits_type, string::allocator_type>());
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "Russian");//русская локаль
string key_text;
ifstream keyfile("key.txt");
if (keyfile.is_open())
{
while (getline(keyfile, key_text))
{
cout << "Key: " << key_text << '\n';
}
keyfile.close();
}
bitset<8> key_char_1(key_text[0]);
bitset<8> key_char_2(key_text[1]);
cout << "Key: " << key_text << '\n';
bitset<8> key[7];
key[0] = (bitset<8>)(key_text[0]);
key[1] = (bitset<8>)(key_text[1]);
key[2] = (bitset<8>)(key_text[2]);
key[3] = (bitset<8>)(key_text[3]);
key[4] = key[2];
key[5] = key[1];
key[6] = key[0];
cout << "Round keys: \n" << key[0] << endl << key[1] << endl << key[2] << endl << key[3] << endl << key[4] << endl << key[5] << endl << key[6] << endl;
cout << "Source file: " << endl;
string line;
ifstream sourcefile("transport.txt");
if (sourcefile.is_open())
{
while (getline(sourcefile, line))
{
cout << line << '\n';
}
sourcefile.close();
}
bitset<2> sub_table_1[4] = {
bitset<2>("10"), bitset<2>("11"), bitset<2>("00"), bitset<2>("01")
};
bitset<2> sub_table_2[4] = {
bitset<2>("01"), bitset<2>("10"), bitset<2>("11"), bitset<2>("00")
};
bitset<2> sub_table_3[4] = {
bitset<2>("00"), bitset<2>("10"), bitset<2>("11"), bitset<2>("01")
};
bitset<2> sub_table_4[4] = {
bitset<2>("11"), bitset<2>("00"), bitset<2>("10"), bitset<2>("01")
};
char ch, lchar, rchar, new_left, new_right;
bitset<8> buffer, left, right;
bitset<2> sub_1, sub_2, sub_3, sub_4;
ofstream dcrypt_file("decrypt.txt");
fstream fin("transport.txt", fstream::in);
int counter = 0;
while (fin >> noskipws >> ch) {
if (counter % 4 == 0) {
// счтиываем первый символ
lchar = ch;
counter++;
continue; // прыгаем на следующий шаг цикла
}
else if (counter % 4 == 1) {
// счтиываем второй символ
rchar = ch;
counter++;
left = read_transport(lchar, rchar);
continue; // прыгаем на следующий шаг цикла
}
else if (counter % 4 == 2) {
// счтиываем третий символ
lchar = ch;
counter++;
continue; // прыгаем на следующий шаг цикла
}
else {
// счтиываем четвертый символ
rchar = ch;
right = read_transport(lchar, rchar);
counter++;
}
// имеем 7 раундов сети
//cout << "input: " << left << " + " << right << endl;
for (int i = 0; i < 7; i++) {
bitset<8> rkey = key[i]; // берем ключ
buffer = left; // сохраняем левый блок
left = right; // новый левый блок это старый правый блок
// сумма по модулю 2
//cout << "XOR" << endl;
//cout << "rkey: " << rkey << endl;
//cout << "right: " << right << endl;
right = rkey ^ right;
//cout << "result:" << right << endl;
// сдвиг влево на 3 бита
//cout << "rotate << 3" << endl;
//cout << "right: " << right << endl;
right = rotate(right);
//cout << "result:" << right << endl;
// разбиваем блок на 4 блока по 2 бита
//cout << "sub" << endl;
//cout << "right: " << right << endl;
sub_1 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(0, 2));
sub_2 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(2, 2));
sub_3 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(4, 2));
sub_4 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(6, 2));
// выполняем замену по таблицам
sub_1 = sub_table_1[(int)(sub_1.to_ulong())];
sub_2 = sub_table_2[(int)(sub_2.to_ulong())];
sub_3 = sub_table_3[(int)(sub_3.to_ulong())];
sub_4 = sub_table_4[(int)(sub_4.to_ulong())];
// собираем 8 битный блок
right = (bitset<8>)(to_s(sub_1) + to_s(sub_2) + to_s(sub_3) + to_s(sub_4));
//cout << "result:" << right << endl;
// сумма по модулю два левого и правого блока
//cout << "XOR" << endl;
//cout << "left: " << buffer << endl;
//cout << "right: " << right << endl;
right = buffer ^ right;
//cout << "result:" << right << endl;
//cout << i << " round: " << left << " + " << right << endl;
}
// размен последнего рауда
buffer = left;
left = right;
right = buffer;
//cout << "output: " << left << " + " << right << endl;
new_left = static_cast<unsigned char>(left.to_ulong()); // записываем результирующий левый блок
dcrypt_file << new_left;
new_right = static_cast<unsigned char>(right.to_ulong()); // записываем результирующий правый блок
dcrypt_file << new_right;
}
dcrypt_file.close();
std::cin.get();
return 0;
}
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
using namespace std;
bitset<8> rotate(std::bitset<8> b)
{
bitset<8> result = b;
bool buffer[2] = { b[0], b[1]}; // сохраняем младшие 2 бита (последние)
result = result >> 2;
result.set(3, buffer[0]);
result.set(2, buffer[1]);
return result;
}
string to_s(std::bitset<2> b)
{
return b.to_string<char, string::traits_type, string::allocator_type>();
}
void write_transport(bitset<8> block, ofstream& tr_file)
{
bitset<4> left_half = (bitset<4>)(block.to_string<char, string::traits_type, string::allocator_type>());
bitset<4> right_half = (bitset<4>)(block.to_string<char, string::traits_type, string::allocator_type>().substr(4));
char lchar = static_cast<unsigned char>(32 + left_half.to_ulong());
char rchar = static_cast<unsigned char>(32 + right_half.to_ulong());
cout << block << " = " << lchar << " + " << rchar <<endl;
tr_file << lchar;
tr_file << rchar;
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "Russian");//русская локаль
string key_text;
ifstream keyfile("key.txt");
if (keyfile.is_open())
{
while (getline(keyfile, key_text))
{
cout << "Key: " << key_text << '\n';
}
keyfile.close();
}
cout << "Key: " << key_text << '\n';
bitset<8> key[7];
key[0] = (bitset<8>)(key_text[0]);
key[1] = (bitset<8>)(key_text[1]);
key[2] = (bitset<8>)(key_text[2]);
key[3] = (bitset<8>)(key_text[3]);
key[4] = key[2];
key[5] = key[1];
key[6] = key[0];
cout << "Round keys: \n" << key[0] << endl << key[1] << endl << key[2] << endl << key[3] << endl << key[4] << endl << key[5] << endl << key[6] << endl;
bitset<2> sub_table_1[4] = {
bitset<2>("10"), bitset<2>("11"), bitset<2>("00"), bitset<2>("01")
};
bitset<2> sub_table_2[4] = {
bitset<2>("01"), bitset<2>("10"), bitset<2>("11"), bitset<2>("00")
};
bitset<2> sub_table_3[4] = {
bitset<2>("00"), bitset<2>("10"), bitset<2>("11"), bitset<2>("01")
};
bitset<2> sub_table_4[4] = {
bitset<2>("11"), bitset<2>("00"), bitset<2>("10"), bitset<2>("01")
};
char ch, new_left, new_right, lchar, rchar;
bitset<2> sub_1, sub_2, sub_3, sub_4;
int counter = 0;
bitset<8> buffer, left, right;
ofstream crypt_file("encrypt.txt");
ofstream tran_file("transport.txt");
fstream fin("source.txt", fstream::in);
std::string content((std::istreambuf_iterator<char>(fin)),
(std::istreambuf_iterator<char>()));
cout << "Source file: " << content << endl;
cout << "length: " << content.length() << endl;
if ((content.length() % 2) == 1) {
content += " ";
}
for (int i = 0; i < content.length(); i++) {
if (counter % 2 == 0) {
// счтиываем первый символ
left = bitset<8>(content[i]);
counter++;
continue; // прыгаем на следующий шаг цикла
}
else {
right = bitset<8>(content[i]); // считываем второй символ (алгоритм 16 битный)
counter++;
}
//cout << "input: " << left << " + " << right << endl;
// имеем 7 раундов сети
for (int i = 0; i < 7; i++) {
bitset<8> rkey = key[i]; // берем ключ
buffer = left; // сохраняем левый блок
left = right; // новый левый блок это старый правый блок
// сумма по модулю 2
//cout << "XOR" << endl;
//cout << "rkey: " << rkey << endl;
//cout << "right: " << right << endl;
right = rkey ^ right;
//cout << "result:" << right << endl;
// сдвиг влево на 3 бита
//cout << "rotate << 3" << endl;
//cout << "right: " << right << endl;
right = rotate(right);
//cout << "result:" << right << endl;
// разбиваем блок на 4 блока по 2 бита
//cout << "sub" << endl;
//cout << "right: " << right << endl;
sub_1 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(0, 2));
sub_2 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(2, 2));
sub_3 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(4, 2));
sub_4 = (bitset<2>)(right.to_string<char, string::traits_type, string::allocator_type>().substr(6, 2));
// выполняем замену по таблицам
sub_1 = sub_table_1[(int)(sub_1.to_ulong())];
sub_2 = sub_table_2[(int)(sub_2.to_ulong())];
sub_3 = sub_table_3[(int)(sub_3.to_ulong())];
sub_4 = sub_table_4[(int)(sub_4.to_ulong())];
// собираем 8 битный блок
right = (bitset<8>)(to_s(sub_1) + to_s(sub_2) + to_s(sub_3) + to_s(sub_4));
//cout << "result:" << right << endl;
// сумма по модулю два левого и правого блока
//cout << "XOR" << endl;
//cout << "left: " << buffer << endl;
//cout << "right: " << right << endl;
right = buffer ^ right;
//cout << "result:" << right << endl;
//cout << i << " round: " << left << " + " << right << endl;
}
// размен последнего рауда
buffer = left;
left = right;
right = buffer;
//cout << "output: " << left << " + " << right << endl;
new_left = static_cast<unsigned char>(left.to_ulong()); // записываем результирующий левый блок
crypt_file << new_left;
new_right = static_cast<unsigned char>(right.to_ulong()); // записываем результирующий правый блок
crypt_file << new_right;
write_transport(left, tran_file); // записуем рез. левый блок в транспортном виде
write_transport(right, tran_file); // записуем рез. правый блок в транспортном виде
}
crypt_file.close();
tran_file.close();
std::cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment