コード例 #1
0
        public string encrypt(string message, BigInteger e, BigInteger n)
        {
            int[] block = UtilityConverter.StringToNumBlock(message);

            string hex = "";

            for (int i = 0; i < block.Length; i++)
            {
                BigInteger enc    = BigInteger.ModPow(block[i], e, n);
                string     hexTmp = enc.ToString("x");

                //Console.WriteLine(enc + " to " + hexTmp + "("+hexTmp.Length+")");

                if (hex == "")
                {
                    hex = hexTmp;
                }
                else
                {
                    hex += "g" + hexTmp;
                }
            }

            //Console.WriteLine();

            return(hex);
        }
コード例 #2
0
ファイル: ECB.cs プロジェクト: tafci/DES-Implementation
        public static String decrypt(String chiperText, string ecbKey)
        {
            ASCIIEncoding encoder = new ASCIIEncoding();
            DES           des     = new DES();

            byte[]   kbytes = UtilityConverter.GetBytes(ecbKey);
            BitArray bitKey = new BitArray(kbytes);

            BitArray bits = UtilityConverter.FromHex(chiperText);

            BitArray[] group = new BitArray[(int)(bits.Length / 64)];

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < group.Length; i++)
            {
                group[i] = new BitArray(64);

                for (int j = 0; j < 64; j++)
                {
                    group[i][j] = bits[i * 64 + j];
                }

                BitArray dec = des.decrypt(group[i], bitKey);

                builder.Append(UtilityConverter.ToString(dec));
            }

            return(builder.ToString().Replace("\0", ""));
        }
コード例 #3
0
ファイル: ECB.cs プロジェクト: tafci/DES-Implementation
        public static String encrypt(String plainText, string ecbKey)
        {
            if (plainText == null)
            {
                return("");
            }

            ASCIIEncoding encoder = new ASCIIEncoding();
            DES           des     = new DES();

            byte[]   kbytes = UtilityConverter.GetBytes(ecbKey);
            BitArray bitKey = new BitArray(kbytes);

            byte[] tempB = encoder.GetBytes(plainText);
            byte[] bytes = new byte[(int)Math.Ceiling(tempB.Length / 8.0f) * 8];

            for (int i = 0; i < tempB.Length; i++)
            {
                bytes[i] = tempB[i];
            }

            BitArray[] group = new BitArray[(int)Math.Ceiling(bytes.Length / 8.0)];

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < group.Length; i++)
            {
                byte[] b = new byte[8];

                for (int j = 0; j < 8; j++)
                {
                    b[j] = bytes[i * 8 + j];
                }

                group[i] = new BitArray(b);

                BitArray enc = des.encrypt(group[i], bitKey);

                //UtilityConverter.ShowBitsLine(enc);

                builder.Append(UtilityConverter.ToHex(enc));
            }

            return(builder.ToString());
        }
コード例 #4
0
        public string decrypt(string hex, BigInteger d, BigInteger n)
        {
            string[] hexBlock = hex.Split('g');
            int      length   = hexBlock.Length;

            int[] numblock = new int[length];

            int i = 0;

            foreach (string s in hexBlock)
            {
                BigInteger bint = BigInteger.Parse(s, NumberStyles.AllowHexSpecifier);
                BigInteger dec  = BigInteger.ModPow(bint, d, n);
                numblock[i++] = (int)dec;
            }

            string plain = UtilityConverter.NumBlockToString(numblock);

            return(plain);
        }
コード例 #5
0
        private BitArray ChiperFunction(BitArray bit32, BitArray bit48)
        {
            // Expand
            int[]    expnansion = { 32,  1,  2,  3,  4,  5,
                                    4,      5,  6,  7,  8,  9,
                                    8,      9, 10, 11, 12, 13,
                                    12,    13, 14, 15, 16, 17,
                                    16,    17, 18, 19, 20, 21,
                                    20,    21, 22, 23, 24, 25,
                                    24,    25, 26, 27, 28, 29,
                                    28,    29, 30, 31, 32, 1 };
            BitArray bitExp48 = new BitArray(48);

            for (int i = 0; i < 48; i++)
            {
                bitExp48[i] = bit32[(expnansion[i] - 1)];
            }

            /*
             * UtilityConverter.ShowBits(bit48);
             * Console.Write(" | ");
             * UtilityConverter.ShowBits(bit32);
             * Console.Write(" >> ");
             * UtilityConverter.ShowBitsLine(bitExp48);
             */

            // XOR
            bitExp48 = bitExp48.Xor(bit48);

            //UtilityConverter.ShowBitsLine(bitExp48);

            // convert to group
            BitArray[] group = new BitArray[8];
            for (int i = 0; i < 8; i++)
            {
                group[i] = new BitArray(6);

                for (int j = 0; j < 6; j++)
                {
                    group[i][j] = bitExp48[(i * 6) + j];
                }
            }

            BitArray sboxSubs = new BitArray(32);
            int      subPos   = 0;

            for (int i = 0; i < 8; i++)
            {
                bool[]   firstLast = { group[i].Get(0), group[i].Get(5) };
                BitArray row       = new BitArray(firstLast);
                int      rowInt    = UtilityConverter.BinToInt(row);

                BitArray col = new BitArray(new bool[]
                                            { group[i].Get(1), group[i].Get(2), group[i].Get(3), group[i].Get(4) });
                int colInt = UtilityConverter.BinToInt(col);

                //UtilityConverter.ShowBits(row);
                //Console.WriteLine(rowInt+", "+colInt);
                //UtilityConverter.ShowBitsLine(col);

                BitArray bit4 = SBoxLookUp(i, rowInt, colInt);

                //UtilityConverter.ShowBits(group[i]);
                //Console.Write(" >> ");
                //UtilityConverter.ShowBitsLine(bit4);

                for (int shift = 0; shift < 4; shift++)
                {
                    sboxSubs[subPos] = bit4[shift];
                    subPos++;
                }
            }



            BitArray perm = new BitArray(32);

            for (int i = 0; i < 32; i++)
            {
                perm[i] = sboxSubs[(chiperPermutation[i] - 1)];
            }

            //UtilityConverter.ShowBits(sboxSubs);
            //Console.Write(" >> ");
            //UtilityConverter.ShowBitsLine(perm);

            return(perm);
        }