2010-06-21 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / tests / desweak.cs
blob68b0aebed24be1ac78bff086a5cadb9a642349f0
1 // DESTest.cs was failing with rev 38316...
2 // but the DES code was changed after the fix
3 // so the original code lives here forever ;-)
5 using System;
7 class Program {
9 public const int keySizeByte = 8;
11 internal static readonly ulong[] weakKeys = {
12 0x0101010101010101, /* 0000000 0000000 */
13 0xFEFEFEFEFEFEFEFE, /* FFFFFFF FFFFFFF */
14 0x1F1F1F1F0E0E0E0E, /* 0000000 FFFFFFF */
15 0xE0E0E0E0F1F1F1F1 /* FFFFFFF 0000000 */
18 internal static ulong PackKey (byte[] key)
20 byte[] paritySetKey = new byte [keySizeByte];
21 // adapted from bouncycastle - see bouncycastle.txt
22 for (int i=0; i < key.Length; i++) {
23 byte b = key [i];
24 paritySetKey [i] = (byte)((b & 0xfe) |
25 ((((b >> 1) ^ (b >> 2) ^ (b >> 3) ^ (b >> 4) ^
26 (b >> 5) ^ (b >> 6) ^ (b >> 7)) ^ 0x01) & 0x01));
29 ulong res = 0;
30 for (int i = 0, sh = 64; (sh = sh - 8) >= 0; i++) {
31 res |= (ulong) paritySetKey [i] << sh;
33 return res;
36 static int Main ()
38 byte[] wk2p = { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E };
39 ulong lk = PackKey (wk2p);
40 foreach (ulong wk in weakKeys) {
41 if (lk == wk)
42 return 0;
44 return 1;