Back to Cptr427Winter2010
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DES { class DesUtility { /// <summary> /// We are using a 1's based numbering system from the right /// </summary> /// <param name="input">64-bit unsigned long data</param> /// <param name="posIn">a value between 1 and 64</param> /// <param name="posOut">a value between 1 and 64</param> /// <returns></returns> public static ulong shiftBit(ulong input, int posIn, int posOut) { ulong output = 0; input >>= posIn - 1; input &= 1; input <<= posOut - 1; output |= input; return output; } public static ulong reverseNumber(ulong input) { ulong output = 0; for (int i = 1; i <= 64; i++) { ulong temp = shiftBit(input, i, 65 - i); output |= temp; } return output; } /// <summary> /// Take the output position to be i and int input position to be /// permutation[i] to create the output ulong value. /// </summary> /// <param name="input"></param> /// <param name="permutation"></param> /// <returns></returns> public static ulong Permutation(ulong input, int[] permutation) { ulong output = 0; for (int i = 0; i < permutation.Length; i++) { output |= shiftBit(input, permutation[i], i + 1); } return output; } } }