Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > .NET Frameworks (точка нет фреймворки) > C# (си шарп)
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 24.12.2012, 11:36   #1
liiy
 
Регистрация: 30.04.2011
Сообщений: 5
Печаль S-DES

Помогите переделать из формы в консоль
Вложения
Тип файла: rar SDES_SourceCode.rar (452.6 Кб, 72 просмотров)
liiy вне форума Ответить с цитированием
Старый 24.12.2012, 22:20   #2
liiy
 
Регистрация: 30.04.2011
Сообщений: 5
По умолчанию кто поможет убить до конца этот код)))

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Collections;
using System.Threading;
using System.IO;

using System.Security.Cryptography;

namespace _1111
{
class SDES
{
SDES my_Des;
Random rand = new Random();

BitArray[,] S_Box1 = new BitArray[4, 4];
BitArray[,] S_Box2 = new BitArray[4, 4];
BitArray Master_key;


public SDES(string _key)
{
Master_key = new BitArray(10);
for (int i = 0; i < _key.Length; i++)
{
Master_key[i] = str2bin(_key[i]);
}

BitArray b0 = new BitArray(2);
b0[0] = false;
b0[1] = false;

BitArray b1 = new BitArray(2);
b0[0] = false;
b0[1] = true;

BitArray b2 = new BitArray(2);
b0[0] = true;
b0[1] = false;

BitArray b3 = new BitArray(2);
b0[0] = true;
b0[1] = true;



S_Box1[0, 0] = b1;
S_Box1[0, 1] = b0;
S_Box1[0, 2] = b3;
S_Box1[0, 3] = b2;

S_Box1[1, 0] = b3;
S_Box1[1, 1] = b2;
S_Box1[1, 2] = b1;
S_Box1[1, 3] = b0;

S_Box1[2, 0] = b0;
S_Box1[2, 1] = b2;
S_Box1[2, 2] = b1;
S_Box1[2, 3] = b3;

S_Box1[3, 0] = b3;
S_Box1[3, 1] = b1;
S_Box1[3, 2] = b3;
S_Box1[3, 3] = b2;
//---------------------
S_Box2[0, 0] = b0;
S_Box2[0, 1] = b1;
S_Box2[0, 2] = b2;
S_Box2[0, 3] = b3;

S_Box2[1, 0] = b2;
S_Box2[1, 1] = b0;
S_Box2[1, 2] = b1;
S_Box2[1, 3] = b3;

S_Box2[2, 0] = b3;
S_Box2[2, 1] = b0;
S_Box2[2, 2] = b1;
S_Box2[2, 3] = b0;

S_Box2[3, 0] = b2;
S_Box2[3, 1] = b1;
S_Box2[3, 2] = b0;
S_Box2[3, 3] = b3;
//---------------------
}

public byte Encrypt1(byte block)
{
BitArray bits_block = byte2bits(block);
BitArray[] keys = Generate_Keys();
return bits2byte(RIP(Fk(Switch(Fk(IP(bits_ block), keys[0])), keys[1])));
//ciphertext = IP-1( fK2 ( SW (fK1 (IP (plaintext)))))
}

public byte Decrypt1(byte block)
{
BitArray bits_block = byte2bits(block);
BitArray[] keys = Generate_Keys();
return bits2byte(RIP(Fk(Switch(Fk(IP(bits_ block), keys[1])), keys[0])));
//IP-1 ( fK1( SW( fK2( IP(ciphertext)))))
}

BitArray byte2bits(byte block)
{
string bits = decimal2binstr(block);
BitArray result = new BitArray(8);
for (int i = 0; i < bits.Length; i++)
{
result[i] = str2bin(bits[i]);
}
return result;
}

byte bits2byte(BitArray block)
{
string result = "";
for (int i = 0; i < block.Length; i++)
{
result += bin2str(block[i]);
}
return binstr2decimal(result);
}
liiy вне форума Ответить с цитированием
Старый 24.12.2012, 22:21   #3
liiy
 
Регистрация: 30.04.2011
Сообщений: 5
По умолчанию

BitArray[] Generate_Keys()
{
BitArray[] keys = new BitArray[2];
BitArray[] temp = Split_Block(P10(Master_key));
keys[0] = P8(Circular_left_shift(temp[0], 1), Circular_left_shift(temp[1], 1));
keys[1] = P8(Circular_left_shift(temp[0], 3), Circular_left_shift(temp[1], 3)); //1 + 2 = 3
return keys;
}

// десятичной в двоичную строку
public string decimal2binstr(byte num)
{
string ret = "";
for (int i = 0; i < 8; i++)
{
if (num % 2 == 1)
ret = "1" + ret;
else
ret = "0" + ret;
num >>= 1;
}
return ret;
}

// binary to decimal string
public byte binstr2decimal(string binstr)
{
byte ret = 0;
for (int i = 0; i < binstr.Length; i++)
{
ret <<= 1;
if (binstr[i] == '1')
ret++;
}
return ret;
}

public string bin2str(bool input)
{
if (input)
return "1";
else
return "0";
}

public bool str2bin(char bit)
{
if (bit == '0')
return false;
else if (bit == '1')
return true;
else
throw new Exception("Ключ должен быть в двоичном формате [0,1]");
}

//generates permated array P10
BitArray P10(BitArray key)
{
//0 1 2 3 4 5 6 7 8 9
//2 4 1 6 3 9 0 8 7 5
BitArray permutatedArray = new BitArray(10);

permutatedArray[0] = key[2];
permutatedArray[1] = key[4];
permutatedArray[2] = key[1];
permutatedArray[3] = key[6];
permutatedArray[4] = key[3];
permutatedArray[5] = key[9];
permutatedArray[6] = key[0];
permutatedArray[7] = key[8];
permutatedArray[8] = key[7];
permutatedArray[9] = key[5];

return permutatedArray;
}

//generates permuted array P8
BitArray P8(BitArray part1, BitArray part2)
{
//0 1 2 3 4 5 6 7
//5 2 6 3 7 4 9 8
//6 3 7 4 8 5 10 9
BitArray permutatedArray = new BitArray(8);

permutatedArray[0] = part2[0];//5
permutatedArray[1] = part1[2];
permutatedArray[2] = part2[1];//6
permutatedArray[3] = part1[3];
permutatedArray[4] = part2[2];//7
permutatedArray[5] = part1[4];
permutatedArray[6] = part2[4];//9
permutatedArray[7] = part2[3];//8

return permutatedArray;
}

BitArray P4(BitArray part1, BitArray part2)
{
//0 1 2 3
//2 4 3 1
//1 3 2 0
BitArray permutatedArray = new BitArray(4);

permutatedArray[0] = part1[1];
permutatedArray[1] = part2[1];//3
permutatedArray[2] = part2[0];//2
permutatedArray[3] = part1[0];

return permutatedArray;
}

BitArray EP(BitArray input)
{
//0 1 2 3
//4 1 2 3 2 3 4 1
//3 0 1 2 1 2 3 0
BitArray permutatedArray = new BitArray(8);

permutatedArray[0] = input[3];
permutatedArray[1] = input[0];
permutatedArray[2] = input[1];
permutatedArray[3] = input[2];
permutatedArray[4] = input[1];
permutatedArray[5] = input[2];
permutatedArray[6] = input[3];
permutatedArray[7] = input[0];

return permutatedArray;
}
liiy вне форума Ответить с цитированием
Старый 24.12.2012, 22:23   #4
liiy
 
Регистрация: 30.04.2011
Сообщений: 5
По умолчанию

//generates permuted text IP
BitArray IP(BitArray plainText)
{
//0 1 2 3 4 5 6 7
//1 5 2 0 3 7 4 6
BitArray permutatedArray = new BitArray(8);

permutatedArray[0] = plainText[1];
permutatedArray[1] = plainText[5];
permutatedArray[2] = plainText[2];
permutatedArray[3] = plainText[0];
permutatedArray[4] = plainText[3];
permutatedArray[5] = plainText[7];
permutatedArray[6] = plainText[4];
permutatedArray[7] = plainText[6];

return permutatedArray;
}

BitArray RIP(BitArray permutedText)
{
//0 1 2 3 4 5 6 7
//3 0 2 4 6 1 7 5

BitArray permutatedArray = new BitArray(8);

permutatedArray[0] = permutedText[3];
permutatedArray[1] = permutedText[0];
permutatedArray[2] = permutedText[2];
permutatedArray[3] = permutedText[4];
permutatedArray[4] = permutedText[6];
permutatedArray[5] = permutedText[1];
permutatedArray[6] = permutedText[7];
permutatedArray[7] = permutedText[5];

return permutatedArray;
}

BitArray Circular_left_shift(BitArray a, int bitNumber)
{
BitArray shifted = new BitArray(a.Length);
int index = 0;
for (int i = bitNumber; index < a.Length; i++)
{
shifted[index++] = a[i % a.Length];
}
return shifted;
}

BitArray[] Split_Block(BitArray block)
{
BitArray[] splited = new BitArray[2];
splited[0] = new BitArray(block.Length / 2);
splited[1] = new BitArray(block.Length / 2);
int index = 0;

for (int i = 0; i < block.Length / 2; i++)
{
splited[0][i] = block[i];
}
for (int i = block.Length / 2; i < block.Length; i++)
{
splited[1][index++] = block[i];
}
return splited;
}

BitArray S_Boxes(BitArray input, int no)
{
BitArray[,] current_S_Box;

if (no == 1)
current_S_Box = S_Box1;
else
current_S_Box = S_Box2;

return current_S_Box[binstr2decimal(bin2str(input[0]) + bin2str(input[3])),
binstr2decimal(bin2str(input[1]) + bin2str(input[2]))];
}

BitArray F(BitArray right, BitArray sk)
{
BitArray[] temp = Split_Block(Xor(EP(right), sk));
return P4(S_Boxes(temp[0], 1), S_Boxes(temp[1], 2));
}

BitArray Fk(BitArray IP, BitArray key)
{
BitArray[] temp = Split_Block(IP);
BitArray Left = Xor(temp[0], F(temp[1], key));
BitArray joined = new BitArray(8);
int index = 0;
for (int i = 0; i < 4; i++)
{
joined[index++] = Left[i];
}
for (int i = 0; i < 4; i++)
{
joined[index++] = temp[1][i];
}
return joined;
}

BitArray Switch(BitArray input)
{
BitArray switched = new BitArray(8);
int index = 0;
for (int i = 4; index < input.Length; i++)
{
switched[index++] = input[i % input.Length];
}
return switched;
}

BitArray Xor(BitArray a, BitArray b)
{
return b.Xor(a);
}


}
liiy вне форума Ответить с цитированием
Старый 24.12.2012, 22:24   #5
liiy
 
Регистрация: 30.04.2011
Сообщений: 5
По умолчанию

private void Encrypt(string txt_enc_in, string txt_enc_out)
{
Random rand = new Random();
txt_enc_in = @"e:\1.txt";
txt_enc_out = @"e:\4.txt";
FileStream fs = new FileStream(txt_enc_in, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
FileStream fs2 = new FileStream(txt_enc_out, FileMode.Create, FileAccess.Write);
BinaryWriter bwr = new BinaryWriter(fs2);
int blocksize = 4 * 1024;
int iteration_number;
if (fs.Length < blocksize)
iteration_number = 1;
else if (fs.Length % blocksize == 0)
iteration_number = (int)fs.Length / blocksize;
else
iteration_number = ((int)fs.Length / blocksize) + 1;
while (iteration_number-- > 0)
{
if (iteration_number == 0)
blocksize = (int)fs.Length % blocksize;
byte[] input = br.ReadBytes(blocksize);
byte[] output = new byte[input.Length];
for (int i = 0; i < output.Length; i++)
{
output[i] = my_Des.Decrypt1(input[i]);
}
bwr.Write(output);
bwr.Flush();
}
bwr.Close();
fs2.Close();
br.Close();
fs.Close();

}


private void Decrypt(string txt_dec_in, string txt_dec_out)
{
txt_dec_in = @"e:\4.txt";
txt_dec_out = @"e:\5.txt";
FileStream fs = new FileStream(txt_dec_in, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryReader br = new BinaryReader(fs);
FileStream fs2 = new FileStream(txt_dec_out, FileMode.Create, FileAccess.Write, FileShare.Write);
BinaryWriter bwr1 = new BinaryWriter(fs2);
int blocksize = 4 * 1024;
int iteration_number1;
if (fs.Length < blocksize)
iteration_number1 = 1;
else if (fs.Length % blocksize == 0)
iteration_number1 = (int)fs.Length / blocksize;
else
iteration_number1 = ((int)fs.Length / blocksize) + 1;
while (iteration_number1-- > 0)
{
if (iteration_number1 == 0)
blocksize = (int)fs.Length % blocksize;
byte[] input = br.ReadBytes(blocksize);
byte[] output = new byte[input.Length];
for (int i = 0; i < output.Length; i++)
{
output[i] = my_Des.Encrypt1(input[i]);
}
bwr1.Write(output);
bwr1.Flush();
}
bwr1.Close();
fs2.Close();
br.Close();
fs.Close();
}
private void Generate_Key(string txt_Enc_Key)
{
int key = rand.Next(0, 1025);
txt_Enc_Key = decimal2binstr(key);
while (txt_Enc_Key.Length < 10)
{
txt_Enc_Key = "0" + txt_Enc_Key;
}


}
public string decimal2binstr(int num)
{
string ret = "";
for (int i = 0; i < 8; i++)
{
if (num % 2 == 1)
ret = "1" + ret;
else
ret = "0" + ret;
num >>= 1;
}
return ret;
}
static void Main(string[] args)
{

}
}
}
liiy вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
шифрование DES bpystep Помощь студентам 1 26.04.2012 20:07
шифрование DES bpystep Помощь студентам 0 24.04.2012 19:03
DES Євгеній Бєлік Помощь студентам 1 01.11.2011 15:11
нужен DES на С++ Kukurudza Общие вопросы C/C++ 3 19.10.2011 21:50
DES Shamonya Общие вопросы Delphi 2 12.04.2011 18:08