/
desweak.cs
46 lines (39 loc) · 1.11 KB
/
desweak.cs
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
// DESTest.cs was failing with rev 38316...
// but the DES code was changed after the fix
// so the original code lives here forever ;-)
using System;
class Program {
public const int keySizeByte = 8;
internal static readonly ulong[] weakKeys = {
0x0101010101010101, /* 0000000 0000000 */
0xFEFEFEFEFEFEFEFE, /* FFFFFFF FFFFFFF */
0x1F1F1F1F0E0E0E0E, /* 0000000 FFFFFFF */
0xE0E0E0E0F1F1F1F1 /* FFFFFFF 0000000 */
};
internal static ulong PackKey (byte[] key)
{
byte[] paritySetKey = new byte [keySizeByte];
// adapted from bouncycastle - see bouncycastle.txt
for (int i=0; i < key.Length; i++) {
byte b = key [i];
paritySetKey [i] = (byte)((b & 0xfe) |
((((b >> 1) ^ (b >> 2) ^ (b >> 3) ^ (b >> 4) ^
(b >> 5) ^ (b >> 6) ^ (b >> 7)) ^ 0x01) & 0x01));
}
ulong res = 0;
for (int i = 0, sh = 64; (sh = sh - 8) >= 0; i++) {
res |= (ulong) paritySetKey [i] << sh;
}
return res;
}
static int Main ()
{
byte[] wk2p = { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E };
ulong lk = PackKey (wk2p);
foreach (ulong wk in weakKeys) {
if (lk == wk)
return 0;
}
return 1;
}
}