Помогите ускорить код, выполняет шифрование в режиме простая замена ГОСТ 28147-89
хотел реализовать ГОСТ 28147-89 режим простой замены получилось но с проблема в том что выполняется слишком долго 1мб шифрует за 20 секунд (приблизительно) а надо 1мб до 10 сек посоветуйте что можно сделать
private void button1_Click(object sender, EventArgs e)
{
Stopwatch sw = new Stopwatch();
byte[] key_Byte = {0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff};
string key256 = Convert.ToString(TO_BIT(key_Byte));
openFileDialog1.Filter = "Все файлы (*.*).|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
sw.Start();
string filePath = openFileDialog1.FileName;
byte[] fileBytes = File.ReadAllBytes(filePath);
string fromfile_Bin = TO_BIT(fileBytes);
string output_Bin = PZENCRYPT(fromfile_Bin, key256);
int length = output_Bin.Length / 8;
byte[] output_Byte = TO_BYTE(output_Bin, length);
File.WriteAllBytes("cipher.txt", output_Byte);
richTextBox1.Text = Convert.ToString(sw.Elapsed.TotalSeconds);
}
}
private static string PZENCRYPT(string ot, string full_key)
{
char[] xor_chars;
string key = "";
string ot_64, nblok1, nblok2, nblok = "", sblok2, ztblok = "", zt_64;
StringBuilder zt = new StringBuilder();
string[] keys = new string[8];
for (int i = 0; i < 8; i++)
{
keys[i] = full_key.Substring(i * 32, 32);
}
//----------------------------------------------------- 64 bit OT filling
int otLen = ot.Length;
if (otLen % 64 != 0)
{
ot = ot.PadRight(otLen + 64 - otLen % 64, '0');
}
int blockSize = 64;
int size = otLen / 64;
for (int blk = 0; blk < size; blk++)
{
ot_64 = ot.Substring(blk * blockSize, blockSize);
nblok2 = ot_64.Substring(0, 32);
nblok1 = ot_64.Substring(32, 32);
//----------------------------------------------------- start of 32 rounds
for (int r = 0; r < 32; r++)
{
if (r > 0 && r < 32) { nblok2 = nblok; nblok1 = ztblok; }
nblok = nblok1;
key = r > 23 ? keys[r % 8] : keys[7 - (r % 8)];
//----------------------------------------------------- addition modulo 2 to the power of 32
long key_long = Convert.ToInt64(key, 2);
long nblok1_long = Convert.ToInt64(nblok1, 2);
long nblok_long = (key_long + nblok1_long) % 4294967296;
nblok1 = Convert.ToString(nblok_long, 2).PadLeft(32, '0');
//----------------------------------------------------- replacement by S blocks
int[,] sblok_array = { { 4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3 },
{ 14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9 },
{ 5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11 },
{ 7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3 },
{ 6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2 },
{ 4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14 },
{ 13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12 },
{ 1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12 }};
sblok2 = "";
StringBuilder sblok2Builder = new StringBuilder();
string sblok = "";
for (int i = 0; i < 8; i++)
{
int sblok_int = Convert.ToInt32((nblok1.Substring(0, 4)), 2);
nblok1 = nblok1.Remove(0, 4);
sblok = Convert.ToString(sblok_array[i, sblok_int], 2);
sblok = sblok.PadLeft(4, '0');
sblok2Builder.Append(sblok);
}
sblok2 = sblok2Builder.ToString();
//----------------------------------------------------- cyclic displacement
string for_sikl = sblok2;
int shift = 11 % sblok2.Length;
sblok2 = sblok2.Substring(shift) + sblok2.Substring(0, shift);
//----------------------------------------------------- XOR with N block
xor_chars = sblok2.ToCharArray();
for (int i = 0; i < 32; i++)
{
xor_chars[i] = (sblok2[i] == nblok2[i]) ? '0' : '1';
}
ztblok = new string(xor_chars);
}
zt_64 = ztblok + nblok;
zt.Append(zt_64);
}
return zt.ToString();
}
private static string TO_BIT(byte[] bytes)
{
StringBuilder bynarystring = new StringBuilder();
foreach (byte b in bytes)
{
bynarystring.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
}
return Convert.ToString(bynarystring);
}
private static byte[] TO_BYTE(string binarystr, int ff_len)
{
byte[] bytes = new byte[ff_len];
int k = 0;
for (int i = 0; i < binarystr.Length; i += 8)
{
string bitsegment = binarystr.Substring(i, Math.Min(8, binarystr.Length - 1));
byte bytevalue = Convert.ToByte(bitsegment, 2);
bytes[k] = bytevalue;
k++;
}
return bytes;
}