Advertisement
mrAnderson33

класс массив битов для DES

Jun 14th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 19.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. namespace CipherNamespace
  9. {
  10.     class BitArray: ICollection<bool>,ICloneable,IEnumerable
  11.     {
  12.         private List<bool> intraBitArray;      
  13.  
  14.         #region Constructors
  15.  
  16.         public BitArray()
  17.         {
  18.             intraBitArray = new List<bool>();
  19.             IsReadOnly = false;            
  20.         }
  21.  
  22.         public BitArray(BitArray bitArray)
  23.         {
  24.             intraBitArray = new List<bool>(bitArray.Count);
  25.             Parallel.For(0, bitArray.Count, (i) => { intraBitArray[i] = bitArray[i]; });
  26.             IsReadOnly = false;
  27.         }
  28.  
  29.         public BitArray(IEnumerable<bool> inputBoolList)
  30.         {
  31.             intraBitArray = (from element in inputBoolList.AsParallel() select element).ToList();
  32.             IsReadOnly = false;
  33.         }
  34.  
  35.         public BitArray(IEnumerable<byte> inputByteList)
  36.         {
  37.             getBitArrayFromByteArr(inputByteList.ToList());
  38.         }
  39.  
  40.         public BitArray(IEnumerable<byte> inputByteList,int byteLength)
  41.         {
  42.             intraBitArray = new List<bool>();
  43.             Func<byte, List<bool>> transformByteToListBools = new Func<byte, List<bool>>((element) => {
  44.                 List<bool> tempList = new List<bool>();
  45.                 for (int i = 0; i < byteLength; i++)
  46.                 {
  47.                     tempList.Add(false);
  48.                 }
  49.                 for (int i = 0; i < byteLength; i++)
  50.                 {
  51.                     tempList[i] = FirstSectionOfOperations.getIbit(element,i) == 1 ? true : false;
  52.                 }
  53.                 tempList.Reverse();
  54.                 return tempList;
  55.             });
  56.             inputByteList = inputByteList.Reverse().ToList();
  57.             intraBitArray = (from element in inputByteList.AsParallel() select transformByteToListBools(element)).SelectMany(x => x).ToList();
  58.             IsReadOnly = false;
  59.         }
  60.  
  61.         public void getBitArrayFromByteArr(List<byte> inputArray)
  62.         {
  63.             intraBitArray = new List<bool>();
  64.             Func<byte, List<bool>> transformByteToListBools = new Func<byte, List<bool>>((element) =>
  65.             {
  66.                 var tempList = new List<bool>() { false, false, false, false, false, false, false, false };
  67.                 for (int i = 0; i < 8; i++)
  68.                 {
  69.                     tempList[i] = FirstSectionOfOperations.getIbit(element,i) == 1 ? true : false;
  70.                 }
  71.                 tempList.Reverse();
  72.                 return tempList;
  73.             });
  74.             inputArray.Reverse();
  75.             intraBitArray = (from element in inputArray.AsParallel() select transformByteToListBools(element)).SelectMany(x => x).ToList();
  76.            
  77.             IsReadOnly = false;
  78.         }
  79.  
  80.         public BitArray(long inputLongValue)
  81.         {
  82.             byte[] longToByteArr = BitConverter.GetBytes(inputLongValue);
  83.             getBitArrayFromByteArr(longToByteArr.ToList());
  84.             IsReadOnly = false;
  85.         }
  86.  
  87.         public BitArray(int Length)
  88.         {
  89.             intraBitArray = new List<bool>();
  90.             Parallel.For(0, Length, (i) => {
  91.                 lock (intraBitArray)
  92.                 {
  93.                     intraBitArray.Add(false);
  94.                 }
  95.             });
  96.         }
  97.  
  98.         public BitArray(string num,bool isBinary = false)
  99.         {
  100.             if(isBinary)
  101.             {
  102.                 intraBitArray = new List<bool>();
  103.                 foreach (var n in num)
  104.                     if (n == '1' | n == '0') intraBitArray.Add(n == '1');
  105.                     else throw new Exception("Invalid char in binary number");
  106.             }
  107.             else
  108.             {
  109.  
  110.             }
  111.            
  112.         }
  113.         public BitArray(string num, int size)
  114.         {
  115.             intraBitArray = new List<bool>();
  116.             if (num.Length < size)
  117.             {
  118.                 var diff = size - num.Length;
  119.                 while (diff-- != 0) intraBitArray.Add(false);
  120.             }
  121.             foreach (var n in num)
  122.             {
  123.                 if (n == '1' | n == '0') intraBitArray.Add(n == '1');
  124.                 else throw new Exception("Invalid char in binary number");
  125.             }
  126.         }
  127.         #endregion
  128.  
  129.         #region ICollection Implementation
  130.  
  131.         /// <summary>
  132.         /// Count of values in BitArray
  133.         /// </summary>
  134.         public int Count
  135.         {
  136.             get
  137.             {
  138.                 return intraBitArray.Count;
  139.             }
  140.         }
  141.  
  142.         /// <summary>
  143.         /// BitArrayAccess mode
  144.         /// </summary>
  145.         public bool IsReadOnly {
  146.             get
  147.             {
  148.                 return intraIsReadOnly;
  149.             }
  150.             set
  151.             {
  152.                 intraIsReadOnly = value;
  153.             }
  154.         }
  155.         private  bool intraIsReadOnly { get; set; }
  156.  
  157.         /// <summary>
  158.         /// Add bool value to BitArray
  159.         /// </summary>
  160.         /// <param name="item">Bool value</param>
  161.         public void Add(bool item)
  162.         {
  163.             if(!IsReadOnly)
  164.             {
  165.                 intraBitArray.Add(item);
  166.             }
  167.             else
  168.             {
  169.                 throw new ReadException("BitArray access mode is only read");
  170.             }
  171.         }
  172.  
  173.         /// <summary>
  174.         /// Clear BitArray values
  175.         /// </summary>
  176.         public void Clear()
  177.         {
  178.             if(!IsReadOnly)
  179.             {
  180.                 intraBitArray.Clear();
  181.             }
  182.             else
  183.             {
  184.                 throw new ReadException("BitArray access mode is only read");
  185.             }
  186.         }
  187.  
  188.         /// <summary>
  189.         /// Is item contains in BitArray
  190.         /// </summary>
  191.         /// <param name="item"></param>
  192.         /// <returns></returns>
  193.         public bool Contains(bool item)
  194.         {
  195.             return intraBitArray.Contains(item);
  196.         }
  197.  
  198.         /// <summary>
  199.         /// Procedure to copy BitArray values to bool array from a given index
  200.         /// </summary>
  201.         /// <param name="array">Bool array</param>
  202.         /// <param name="arrayIndex">Start copy index</param>
  203.         public void CopyTo(bool[] array, int arrayIndex)
  204.         {
  205.             Parallel.For(0, intraBitArray.Count,
  206.                 (i,state)=> {
  207.                     try
  208.                     {
  209.                         array[i + arrayIndex] = intraBitArray[i];
  210.                     }
  211.                     catch
  212.                     {}
  213.                 });
  214.         }
  215.  
  216.         /// <summary>
  217.         /// Procedure to copy BitArray values to byte array from a given index
  218.         /// </summary>
  219.         /// <param name="array">Byte array</param>
  220.         /// <param name="arrayIndex">Start copy index</param>
  221.         public void CopyTo(byte[] array, int arrayIndex)
  222.         {
  223.             Parallel.For(0, intraBitArray.Count,
  224.                 (i, state) =>
  225.                 {
  226.                     try
  227.                     {
  228.                         if (intraBitArray[i] == true)
  229.                         {
  230.                             FirstSectionOfOperations.SetIbit(7 - (i + arrayIndex) % 8, ref (array[(array.Count() - 1) - ((int)((arrayIndex + i) / 8))]));
  231.                         }
  232.                         else
  233.                         {
  234.                             FirstSectionOfOperations.TakeOffIbit(7 - (i + arrayIndex) % 8, ref (array[(array.Count() - 1) - ((int)((arrayIndex + i) / 8))]));
  235.                         }
  236.                     }
  237.                     catch
  238.                     { }
  239.                 });
  240.         }
  241.  
  242.         public IEnumerator<bool> GetEnumerator()
  243.         {
  244.             return intraBitArray.GetEnumerator();
  245.         }
  246.  
  247.         /// <summary>
  248.         /// Remove item from BitArray
  249.         /// </summary>
  250.         /// <param name="item">Bool value</param>
  251.         /// <returns></returns>
  252.         public bool Remove(bool item)
  253.         {
  254.             if(!IsReadOnly)
  255.             {
  256.                 return intraBitArray.Remove(item);
  257.             }
  258.             else
  259.             {
  260.                 throw new ReadException("BitArray access mode is only read");
  261.             }
  262.         }
  263.  
  264.         /// <summary>
  265.         /// Remove item at given index from BitArray
  266.         /// </summary>
  267.         /// <param name="index"></param>
  268.         /// <returns></returns>
  269.         public void RemoveAt(int index)
  270.         {
  271.             if (!IsReadOnly)
  272.             {
  273.                 intraBitArray.RemoveAt(index);
  274.             }
  275.             else
  276.             {
  277.                 throw new ReadException("BitArray access mode is only read");
  278.             }
  279.         }
  280.  
  281.         IEnumerator IEnumerable.GetEnumerator()
  282.         {
  283.             return intraBitArray.GetEnumerator();
  284.         }
  285.  
  286.         public BitArray Union(BitArray second)
  287.         {
  288.             intraBitArray.AddRange((List<bool>)second);
  289.             return (BitArray)Clone();
  290.         }
  291.  
  292.         public BitArray Reverse()
  293.         {
  294.             intraBitArray.Reverse();
  295.             return this;
  296.         }
  297.  
  298.         public BitArray[] DevideByParts(int parts)
  299.         {
  300.             var sizeOfParts = intraBitArray.Count / parts;
  301.  
  302.             var listBitArrays = new List<BitArray>();
  303.  
  304.             //Console.WriteLine($"size of parts = {sizeOfParts}");
  305.  
  306.             int count = 0;
  307.             var temp = new BitArray();
  308.  
  309.             foreach(var bit in intraBitArray)
  310.             {
  311.                 temp.Add(bit);
  312.                 count++;
  313.                 if (count  > sizeOfParts-1)
  314.                 {
  315.                     listBitArrays.Add(temp);
  316.                     count = 0;
  317.                     temp = new BitArray();
  318.                 }
  319.             }
  320.             return listBitArrays.ToArray();
  321.             }
  322.  
  323.         public BitArray JoinArr(BitArray bitArray)
  324.         {
  325.             var res = new List<bool>();
  326.             foreach (var n in intraBitArray) res.Add(n);
  327.             foreach (var n in bitArray) res.Add(n);
  328.             return new BitArray(res);
  329.         }
  330.         public static BitArray ConcatArr(params BitArray[] bitArrays)
  331.         {
  332.             var res = new List<bool>();
  333.             foreach (var bitArray in bitArrays)
  334.                 foreach (var bit in bitArray)
  335.                     res.Add(bit);
  336.             return new BitArray(res);
  337.         }
  338.         #endregion
  339.  
  340.         #region ICloneable Implementation
  341.         public object Clone()
  342.         {
  343.             return new BitArray(intraBitArray);
  344.         }
  345.         #endregion
  346.  
  347.         #region Operator overloading
  348.  
  349.         /// <summary>
  350.         /// Bit negation over all members of BitArray
  351.         /// </summary>
  352.         /// <param name="bitArray"></param>
  353.         /// <returns></returns>
  354.         public static BitArray operator !(BitArray bitArray)
  355.         {
  356.             return new BitArray((from element in bitArray.AsParallel() select !element).ToList());
  357.         }
  358.  
  359.         /// <summary>
  360.         /// Bit XOR over all members of BitArray
  361.         /// </summary>
  362.         /// <param name="bitArray1"></param>
  363.         /// <param name="bitArray2"></param>
  364.         /// <returns></returns>
  365.         public static BitArray operator ^(BitArray bitArray1, BitArray bitArray2)
  366.         {
  367.             var temp = bitArray1.Clone() as BitArray;
  368.  
  369.             Parallel.For(0, bitArray1.Count, (i) =>
  370.             {
  371.                temp[i] ^= bitArray2[i];
  372.             });
  373.             return temp.Clone() as BitArray;
  374.         }
  375.  
  376.         /// <summary>
  377.         /// Bit conjunction over all members of BitArray
  378.         /// </summary>
  379.         /// <param name="bitArray1"></param>
  380.         /// <param name="bitArray2"></param>
  381.         /// <returns></returns>
  382.         public static BitArray operator &(BitArray bitArray1, BitArray bitArray2)
  383.         {
  384.             Parallel.For(0, bitArray1.Count, (i) => {
  385.                 bitArray1[i] &= bitArray2[i];
  386.             });
  387.             return (BitArray)bitArray1.Clone();
  388.         }
  389.  
  390.         /// <summary>
  391.         /// Bit disjunction over all members of BitArray
  392.         /// </summary>
  393.         /// <param name="bitArray1"></param>
  394.         /// <param name="bitArray2"></param>
  395.         /// <returns></returns>
  396.         public static BitArray operator |(BitArray bitArray1, BitArray bitArray2)
  397.         {
  398.             Parallel.For(0, bitArray1.Count, (i) => {
  399.                 bitArray1[i] |= bitArray2[i];
  400.             });
  401.             return (BitArray)bitArray1.Clone();
  402.         }
  403.  
  404.  
  405.         public static BitArray operator <<(BitArray bitArray, int n)// циклический сдвиг <<<
  406.         {
  407.             bool tempval = false;
  408.             for (int i = 0; i < n; i++)
  409.             {
  410.                 tempval = bitArray[0];
  411.                 bitArray.RemoveAt(0);
  412.                 bitArray.Add(tempval);
  413.             }
  414.             return (BitArray)bitArray.Clone();
  415.         }
  416.  
  417.         public override string ToString()
  418.         {
  419.             string res = "";
  420.             foreach (var item in this)
  421.                 res += (item == true ? "1" : "0"); //+ " ";
  422.             return res;
  423.         }
  424.         #endregion
  425.  
  426.         #region Bit Operations Classes
  427.         public static class FirstSectionOfOperations
  428.         {
  429.  
  430.             public static int getIbit(int Number,int IndexNumber)
  431.             {
  432.                 int intraNumber = Number;
  433.                 int result = ((1 << IndexNumber) & intraNumber) >> IndexNumber;
  434.                 return result;
  435.             }
  436.  
  437.             public static void SetIbit(int IndexNumber, ref byte Number)
  438.             {
  439.                 Number = (byte)((1 << IndexNumber) | Number);
  440.             }
  441.  
  442.             public static void TakeOffIbit(int IndexNumber, ref byte Number)
  443.             {
  444.                 Number = (byte)(~(1 << IndexNumber) & Number);
  445.             }
  446.  
  447.             public static void swapIJbit(int i, int j, ref int Number)
  448.             {
  449.                 int intraNumber = Number;
  450.                 int withoutIJbits = intraNumber & (-1 ^ ((1 << i) | (1 << j)));
  451.                 Number = ((int)getIbit(i, intraNumber) << j) | (int)getIbit(j, intraNumber) << i | withoutIJbits;
  452.             }
  453.  
  454.             public static void SetToZero(int m, ref int Number)
  455.             {
  456.                 Number = ((-1) << m) & Number;
  457.             }
  458.  
  459.         }
  460.         #endregion
  461.  
  462.         #region Extra Functionality
  463.  
  464.         public void IncreaseLength(int length)
  465.         {
  466.             intraBitArray.Reverse();
  467.             while (length-- != 0) intraBitArray.Add(false);
  468.             intraBitArray.Reverse();
  469.         }
  470.  
  471.         public void RemoveInsignificant()
  472.         {
  473.             var temp = new List<bool>();
  474.  
  475.             bool isfisrt = false;
  476.  
  477.             foreach (var el in intraBitArray)
  478.                 if (isfisrt) temp.Add(el);
  479.                 else isfisrt = el;
  480.  
  481.             intraBitArray = temp;
  482.         }
  483.         public bool this[int index]
  484.         {
  485.             get
  486.             {
  487.                 try
  488.                 {
  489.                     return intraBitArray[index];
  490.                 }
  491.                 catch (Exception)
  492.                 {
  493.                     throw;
  494.                 }
  495.             }
  496.             set
  497.             {
  498.                 try
  499.                 {
  500.                     intraBitArray[index] = value;
  501.                 }
  502.                 catch (Exception)
  503.                 {
  504.                     throw;
  505.                 }
  506.             }
  507.         }
  508.  
  509.         /// <summary>
  510.         ///  Applying input permutations to BitArray
  511.         /// </summary>
  512.         /// <param name="permutations"></param>
  513.         public void applyPermutations(IEnumerable<int> permutations)
  514.         {
  515.             intraBitArray = (from element in permutations select intraBitArray.ElementAt(element - 1)).ToList();
  516.         }
  517.  
  518.         public byte getByteValueOFBitArray
  519.         {
  520.             get
  521.             {
  522.                 if(Count <= 8)
  523.                 {
  524.                     byte result = 0;
  525.                     intraBitArray.Reverse();
  526.                     for (int i = 0; i < Count; i++)
  527.                     {
  528.                         if (intraBitArray[i])
  529.                         {
  530.                             FirstSectionOfOperations.SetIbit(i, ref result);
  531.                         }
  532.                         else
  533.                         {
  534.                             FirstSectionOfOperations.TakeOffIbit(i, ref result);
  535.                         }
  536.                     }
  537.                     return result;
  538.                 }
  539.                 else
  540.                 {
  541.                     throw new ArgumentOutOfRangeException("Count > 8");
  542.                 }
  543.             }
  544.         }
  545.  
  546.         public void removeNullsfromBegin()
  547.         {
  548.             while (true)
  549.             {
  550.                 if (intraBitArray[0] == false)
  551.                 {
  552.                     intraBitArray.RemoveAt(0);
  553.                 }
  554.                 else
  555.                 {
  556.                     intraBitArray.Reverse();
  557.                     intraBitArray.Add(false);
  558.                     intraBitArray.Reverse();
  559.                     break;
  560.                 }
  561.             }
  562.         }
  563.  
  564.         public uint getUintValFromBitArray
  565.         {
  566.             get
  567.             {
  568.                 byte[] tempRes = new byte[4];
  569.                 CopyTo(tempRes, 0);
  570.                 return BitConverter.ToUInt32(tempRes, 0);
  571.             }
  572.         }
  573.  
  574.         public long getLongValFromBitArray
  575.         {
  576.             get
  577.             {
  578.                 byte[] tempRes = new byte[8];
  579.                 CopyTo(tempRes, 0);
  580.                 return BitConverter.ToInt64(tempRes, 0);
  581.             }
  582.         }
  583.  
  584.         public char[] elementsOfBitArrayInChar
  585.         {
  586.             get
  587.             {
  588.                 return ToString().ToArray();
  589.             }
  590.         }
  591.  
  592.         public string getLongHexadecimalFromBitarray
  593.         {
  594.             get
  595.             {
  596.                 return getLongValFromBitArray.ToString("X");
  597.             }
  598.         }
  599.  
  600.         public string getUintHexadecimalFromBitarray
  601.         {
  602.             get
  603.             {
  604.                 return getUintValFromBitArray.ToString("X");
  605.             }
  606.         }
  607.  
  608.         public byte[] getByteArrayFromBitArray
  609.         {
  610.             get
  611.             {
  612.                 byte[] tempByteArr = new byte[Count / 8];
  613.                 CopyTo(tempByteArr, 0);
  614.                 return tempByteArr;
  615.             }
  616.         }
  617.         #endregion
  618.  
  619.         #region Exceptions
  620.         public class ReadException : Exception
  621.         {
  622.             public ReadException() : base() {
  623.             }
  624.  
  625.             public ReadException(string message) : base(message)
  626.             {}
  627.  
  628.             public ReadException(string message, Exception innerException) :
  629.                base(message, innerException)
  630.             {}
  631.  
  632.            
  633.         }
  634.         #endregion
  635.  
  636.         #region Converters
  637.  
  638.    
  639.         public static explicit operator List<bool>(BitArray bitArray)
  640.         {
  641.             return (from element in bitArray select element).ToList();
  642.         }
  643.  
  644.         public static explicit operator byte[](BitArray bitArray)
  645.         {
  646.             return bitArray.getByteArrayFromBitArray;
  647.         }
  648.  
  649.         public static explicit operator long(BitArray bitArray)
  650.         {
  651.             return bitArray.getLongValFromBitArray;
  652.         }
  653.  
  654.         public static explicit operator uint(BitArray bitArray)
  655.         {
  656.             return bitArray.getUintValFromBitArray;
  657.         }
  658.  
  659.         public static explicit operator string(BitArray bitArray)
  660.         {
  661.             return bitArray.ToString();
  662.         }
  663.  
  664.         #endregion
  665.     }
  666. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement