using System using System Collections using System Collections Generic

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Many_time_pad
{
class ManyTimePadDecryption
{
public static byte[] StringToByteArray(string cipherstext)
{
return Enumerable.Range(0, cipherstext.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(cipherstext.Substring(x, 2), 16))
.ToArray();
}
}
class Program
{
static void Main(string[] args)
{
string mainCipher = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";
string[] cipherTexts = {"466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d",
"315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba50",
"234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb741",
"32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de812",
"32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee41",
"3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de812",
"32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d",
"32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af513",
"315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e941",
"271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f404"
};
//string[] cipherTexts = {
// "955c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774941fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e",
// "234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f",
// "32510ba9a7b2bba9b8005d43a944b5714cc0bb0c8a34884dd91944b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81294b59b7afb5f41afa8d661cb",
// "32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3946ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e945cd3a9544ee4160ead45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b194f040d8ec6447e2c767f3d94ed81ea2e4c1404e1955a1010e7229be6636aaa",
// "3f561ba9adb4b6ebec54424ba957b564418fac0dd35f8c08d95a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81294b59b73fb4942cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722194f042f8ec85b7c2070",
// "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a94ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19989522b11be87a59c355d25f8e4",
// "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9945f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea953c8661dd9a4ce",
// "955c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f957da9d3",
// "271946f9bbb2aeadec111841a81abc940ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027",
// "466d06ece998b7a2fb1d464fed2ced7641ddaa3cc95c9941cf110abbf409ed39598005b3399ccfafb61d0955fca0a954be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537594541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83"
//};
// массив содержащий шифры, переведенные в десятичную сс (83 - длина строки, которую нам надо зашифровать, и >> длина ключа)
byte[,] byteCiphers = new byte[10, 83];
// массив, содержащий по 2 ключа для каждого шифра
byte[, ,] cipherKeys = new byte[2, 10, 83];
// массив для расшированных сообшений
byte[] decrypts = new byte[83];
// заполняем массив шифров
for (int i = 0; i < 10; i++)
{
byte[] tmp = ManyTimePadDecryption.StringToByteArray(cipherTexts[i]);
for (int j = 0; j < 83; j++)
{
byteCiphers[i, j] = tmp[j];
}
}
// перебираем все пары шифров между собой
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
// проходим во всем "столбцам" шифров
if (i != j)
{
byte tmp;
for (int k = 0; k < 83; k++)
{
tmp = (byte)(byteCiphers[i, k] ^ byteCiphers[j, k]);
BitArray btmp = new BitArray(new byte[] { tmp });
// если 7й бит - 1, то в 1м из 2х сообщений - пробел, выбираем оба случая
if (btmp.Get(6) == true)
{
cipherKeys[0, i, k] = (byte)(byteCiphers[j, k] ^ 32);
cipherKeys[1, j, k] = (byte)(byteCiphers[i, k] ^ 32);
}
}
}
}
}
// проверяем собранные ключи
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 83; l++)
{
byte tmp = (byte)(byteCiphers[i,l] ^ cipherKeys[k, j, l]);
if ((tmp >= 65 && tmp <= 122) || tmp == 32)
decrypts[l] = tmp;
else
decrypts[l] = 35;
}
Console.WriteLine("[Cipher: {0}]: {1}", i, System.Text.Encoding.Default.GetString(decrypts));
}
}
}
byte[] mainByteCipher = ManyTimePadDecryption.StringToByteArray(mainCipher);
byte[] tempMessage = new byte[] { 32,117,115,101,32,98,114,117,116,101,32,102,111,114,
};
int counter = 69;
for (int i = 0; i < tempMessage.Length; i++)
{
//Console.Write(Convert.ToChar((tempMessage[i] ^ byteCiphers[7, counter]) ^ mainByteCipher[counter]));
Console.Write("{0} ", tempMessage[i] ^ byteCiphers[7, counter]);
counter++;
}
Console.WriteLine();
byte[] t = new byte[] { 51,154,25,248,170,64};
counter = 34;
for (int i = 0; i < t.Length; i++)
{
Console.Write(Convert.ToChar(t[i] ^ mainByteCipher[counter]));
counter++;
}
byte[] initialKey = new byte[] { 102, 57, 110, 137, 201, 219, 216, 204,152,116,53,42,205,99,149,16,46,175,206,120,170,127,237,40,160,127,107,201,141,41,197,11, 105, 176,
51,154,25,248,170,64, 26,156, 109,112,143,128,192,102,199,99,254,240,18,49,72,205,216,232,2,208,91,169,135,119,51,93, 174, 252, 236,
213,156,67,58,107,38,139,96,191,78,240,60,154,97};
Console.Write("[Cipher: ]: ");
for (int i = 0; i < initialKey.Length; i++)
{
Console.Write(Convert.ToChar((initialKey[i] ^ mainByteCipher[i])));
}
Console.WriteLine();
Console.Write("[Cipher: ]: ");
for (int i = 0; i < initialKey.Length; i++)
{
Console.Write(Convert.ToChar((initialKey[i] ^ byteCiphers[7, i])));
}
Console.WriteLine();
Console.Write("[Cipher: ]: ");
for (int i = 0; i < initialKey.Length; i++)
{
Console.Write(Convert.ToChar((initialKey[i] ^ byteCiphers[1, i])));
}
Console.ReadLine();
}
}
}