Beispiel #1
0
        public static Shifr[] InShifr(string _str, EllipticCurvePoint g)
        {
            StringBuilder str      = new StringBuilder(_str);
            int           _countSh = Convert.ToInt32(str.ToString(0, 24), 2);

            Shifr[] sh = new Shifr[_countSh];

            int count = Convert.ToInt32(str.ToString(24, 8), 2);

            List <byte> pkP = new List <byte>();

            for (int i = 0; i < count; i++)
            {
                byte temp = Convert.ToByte(str.ToString(32 + i * 8, 8), 2);
                pkP.Add(temp);
            }
            str = str.Remove(0, count * 8 + 32);

            byte[]             pointkP = pkP.ToArray();
            string             point1  = BitConverter.ToString(pointkP, 0).Replace("-", ""); // сжатая точка kP
            EllipticCurvePoint kP      = EllipticCurvePoint.UnCompressPoint(point1, g);

            string[] strshifr    = new string[_countSh];
            var      tasks       = new List <Task>();
            int      currentchar = 0;

            for (int i = 0; i < _countSh; i++)
            {
                int lenstrcadr = Convert.ToInt32(str.ToString(currentchar, 16), 2);
                strshifr[i]  = str.ToString(currentchar + 16, lenstrcadr);
                currentchar += lenstrcadr + 16;
                var i1 = i;
                tasks.Add(Task.Factory.StartNew(delegate()
                {
                    int correct = 0; // корректировка
                    correct     = Convert.ToInt32(strshifr[i1].Substring(0, 5), 2);

                    int countSecondPoint = Convert.ToInt32(strshifr[i1].Substring(5, 8), 2);

                    var secondPoint = new List <byte>();
                    for (int j = 0; j < countSecondPoint; j++)
                    {
                        byte temp = Convert.ToByte(strshifr[i1].Substring(13 + 8 * j, 8), 2);
                        secondPoint.Add(temp);
                    }
                    string _second = BitConverter.ToString(secondPoint.ToArray(), 0).Replace("-", ""); // сжатая точка
                    var second     = EllipticCurvePoint.UnCompressPoint(_second, g);

                    sh[i1] = new Shifr(kP, second, correct);
                }));
            }
            Task.WaitAll(tasks.ToArray());
            return(sh.ToArray());
        }
Beispiel #2
0
            public Encryption(string pathPublicKey)
            {
                #region открытие ключа
                StringBuilder sb        = new StringBuilder();
                byte[]        inputfile = File.ReadAllBytes(pathPublicKey);
                byte          rem       = inputfile[0];
                for (int i = 1; i < inputfile.Count() - 1; i++)
                {
                    sb.Append(Convert.ToString(Convert.ToByte(inputfile[i]), 2).FillWithZero(8));
                }
                sb.Append(Convert.ToString(Convert.ToByte(inputfile[inputfile.Count() - 1]), 2).FillWithZero(rem));
                #endregion
                if (sb.ToString(0, 1).Equals("1")) //  если кривая заданна в программе
                {
                    EllipticCurvePoint g = new EllipticCurvePoint(192);
                    if (sb.ToString(1, 2).Equals("00"))
                    {
                        //  g = new EllipticCurvePoint(192);
                    }
                    else if (sb.ToString(1, 2).Equals("01"))
                    {
                        g = new EllipticCurvePoint(256);
                    }
                    else if (sb.ToString(1, 2).Equals("10"))
                    {
                        g = new EllipticCurvePoint(384);
                    }
                    else if (sb.ToString(1, 2).Equals("11"))
                    {
                        g = new EllipticCurvePoint(521);
                    }
                    else
                    {
                        throw new Exception("Файл поврежден. Невозможно определить тип кривой.");
                    }

                    int         count = Convert.ToInt32(sb.ToString(3, 8), 2);
                    List <byte> point = new List <byte>();
                    for (int j = 0; j < count; j++)
                    {
                        byte temp = Convert.ToByte(sb.ToString(11 + 8 * j, 8), 2);
                        point.Add(temp);
                    }
                    sb = sb.Remove(0, 8 * count + 11);
                    string _second = BitConverter.ToString(point.ToArray(), 0).Replace("-", ""); // сжатая точка
                    openKey = EllipticCurvePoint.UnCompressPoint(_second, g);

                    count = Convert.ToInt32(sb.ToString(0, 8), 2);
                    point = new List <byte>();
                    for (int j = 0; j < count; j++)
                    {
                        byte temp = Convert.ToByte(sb.ToString(8 + 8 * j, 8), 2);
                        point.Add(temp);
                    }
                    _second = BitConverter.ToString(point.ToArray(), 0).Replace("-", ""); // сжатая точка
                    Point   = EllipticCurvePoint.UnCompressPoint(_second, g);
                }
                else
                {
                    throw new Exception("Файл поврежден");
                }
            }