Skip to content

Instantly share code, notes, and snippets.

@iSarCasm
Last active February 15, 2016 12:09
Show Gist options
  • Save iSarCasm/76709769d6c014364adb to your computer and use it in GitHub Desktop.
Save iSarCasm/76709769d6c014364adb 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<4> rotate(std::bitset<4> b)
{
bitset<4> 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;
}
bitset<8> de_transport(string c)
{
bitset<4> l = bitset<4>((int)c[0] - 31);
bitset<4> r = bitset<4>((int)c[1] - 31);
bitset<8> result = (bitset<8>)(l.to_string<char, string::traits_type, string::allocator_type>() + r.to_string<char, string::traits_type, string::allocator_type>());
cout << "left: " << c[0] << " = " << l << endl;
cout << "right: " << c[1] << " = " << r << endl;
cout << "b;pock: " << " = " << result << endl;
return result;
}
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<4> key[8];
key[0] = (bitset<4>)(key_char_1.to_string<char, string::traits_type, string::allocator_type>());
key[1] = (bitset<4>)(key_char_1.to_string<char, string::traits_type, string::allocator_type>().substr(4));
key[2] = (bitset<4>)(key_char_2.to_string<char, string::traits_type, string::allocator_type>());
key[3] = (bitset<4>)(key_char_2.to_string<char, string::traits_type, string::allocator_type>().substr(4));
cout << "Round keys: \n" << key[0] << endl << key[1] << endl << key[2] << endl << key[3] << 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<4> sub_table_1[4] = {
bitset<4>("01"), bitset<4>("00"), bitset<4>("10"), bitset<4>("11")
};
// Вторая таблица замены
bitset<4> sub_table_2[4] = {
bitset<4>("10"), bitset<4>("00"), bitset<4>("11"), bitset<4>("01")
};
char ch, new_ch, lchar, rchar;
bitset<4> buffer, left, right;
bitset<8> block;
string cipher;
ofstream dcrypt_file("decrypt.txt");
fstream fin("transport.txt", fstream::in);
int counter = 0;
while (fin >> noskipws >> ch) {
if (counter % 2 == 0) {
left = bitset<4>((int)ch - 31);
counter++;
continue;
}
else {
right = bitset<4>((int)ch - 31);
counter++;
}
for (int i = 0; i < 4; i++) {
bitset<4> rkey = key[i];
buffer = left;
left = right;
// 1. Сумма по модулю 2
right = right ^ rkey;
// 2. Сдвиг вправо на 2
right = rotate(right);
// 3. Замена по двум таблицам
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_1 = sub_table_1[(int)(sub_1.to_ulong())];
sub_2 = sub_table_2[(int)(sub_2.to_ulong())];
right = (bitset<4>)(to_s(sub_1) + to_s(sub_2));
right = buffer ^ right;
//cout << "[" << rkey << "] " << left <<" -> " << right << endl;
}
buffer = left;
left = right;
right = buffer;
bitset<8> block(left.to_string<char, string::traits_type, string::allocator_type>() + right.to_string<char, string::traits_type, string::allocator_type>());
new_ch = static_cast<unsigned char>(block.to_ulong());
cout << "changed block: " << block << endl;
dcrypt_file << new_ch;
}
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<4> rotate(std::bitset<4> b)
{
bitset<4> 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 transport(std::bitset<8> b)
{
string result;
bitset<4> left(b.to_string<char, string::traits_type, string::allocator_type>());
bitset<4> right(b.to_string<char, string::traits_type, string::allocator_type>().substr(4));
char lchar = static_cast<unsigned char>(31 + left.to_ulong());
char rchar = static_cast<unsigned char>(31 + right.to_ulong());
result += lchar;
result += rchar;
return result;
}
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<4> key[8];
key[0] = (bitset<4>)(key_char_1.to_string<char, string::traits_type, string::allocator_type>());
key[1] = (bitset<4>)(key_char_1.to_string<char, string::traits_type, string::allocator_type>().substr(4));
key[2] = (bitset<4>)(key_char_2.to_string<char, string::traits_type, string::allocator_type>());
key[3] = (bitset<4>)(key_char_2.to_string<char, string::traits_type, string::allocator_type>().substr(4));
cout << "Round keys: \n" << key[0] << endl << key[1] << endl << key[2] << endl << key[3] << endl;
// Rotation >> 2
bitset<4> k("1001");
cout << "Before rotate" << k << endl;
k = rotate(k);
cout << k << endl;
// Transport enc.
bitset<8> bits("01000111");
cout << "Transport (" << bits << "): " << transport(bits) << endl;
cout << "Source file: " << endl;
string line;
ifstream sourcefile("source.txt");
if (sourcefile.is_open())
{
while (getline(sourcefile, line))
{
cout << line << '\n';
}
sourcefile.close();
}
// Первая таблица замены
bitset<4> sub_table_1[4] = {
bitset<4>("01"), bitset<4>("00"), bitset<4>("10"), bitset<4>("11")
};
// Вторая таблица замены
bitset<4> sub_table_2[4] = {
bitset<4>("10"), bitset<4>("00"), bitset<4>("11"), bitset<4>("01")
};
char ch, new_ch, lchar, rchar;
bitset<4> buffer;
ofstream crypt_file("encrypt.txt");
ofstream tran_file("transport.txt");
fstream fin("source.txt", fstream::in);
while (fin >> noskipws >> ch) {
bitset<8> block(ch);
bitset<4> left(block.to_string<char, string::traits_type, string::allocator_type>());
bitset<4> right(block.to_string<char, string::traits_type, string::allocator_type>().substr(4));
//cout << "block before changes: " << block << endl;
for (int i = 3; i >= 0; i--) {
bitset<4> rkey = key[i];
buffer = left;
left = right;
// 1. Сумма по модулю 2
right = right ^ rkey;
// 2. Сдвиг вправо на 2
right = rotate(right);
// 3. Замена по двум таблицам
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_1 = sub_table_1[(int)(sub_1.to_ulong())];
sub_2 = sub_table_2[(int)(sub_2.to_ulong())];
right = (bitset<4>)(to_s(sub_1) + to_s(sub_2));
right = buffer ^ right;
//cout << "[" << rkey << "] " << left <<" -> " << right << endl;
}
buffer = left;
left = right;
right = buffer;
block = (bitset<8>)(left.to_string<char, string::traits_type, string::allocator_type>() + right.to_string<char, string::traits_type, string::allocator_type>());
new_ch = static_cast<unsigned char>(block.to_ulong());
crypt_file << new_ch;
//cout << "sent block: " << block << endl;
tran_file << transport(block);
}
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