public static Shifr[] encrypt(byte[][] message, EllipticCurvePoint publicB, EllipticCurvePoint _pointG)
        {
            Shifr[] sh = new Shifr[message.GetLength(0)];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            byte[] key = new byte[15];
            rng.GetBytes(key);
            BigInteger k = new BigInteger(key);

            if (k < 0)
            {
                k = -k;
            }
            ;

            ParallelEncoding pe = new ParallelEncoding(k, message, publicB, _pointG);

            Task t1 = new Task(pe.Func0);
            Task t2 = new Task(pe.Func1);
            Task t3 = new Task(pe.Func2);

            t1.Start();
            t2.Start();
            t3.Start();
            pe.Func3();
            Task.WaitAll(t1, t2, t3);

            return(pe.Shifr);
        }
 public void FuncNoParallel()
 {
     for (int i = 0; i < lengthmess; i++)
     {
         int                correct   = 0;
         BigInteger         bigtext   = new BigInteger(Message[i]);
         EllipticCurvePoint pointtext = PointG.SearchPoint(bigtext, out correct);
         EllipticCurvePoint point2    = EllipticCurvePoint.AffineCoords(EllipticCurvePoint.AddPoint(pointtext, kPublicB));
         Shifr[i] = new Shifr(kP, point2, correct);
     }
 }
Exemple #3
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());
        }
        public void Func3()
        {
            for (int i = 3; i < lengtshifr; i += 4)
            {
                Shifr _sh = Sh[i];
                EllipticCurvePoint para2 = _sh.PointB;
                EllipticCurvePoint t     = EllipticCurvePoint.AddPoint(para2, KeyPara1);
                BigInteger         q     = t.X * Additional.Inverse(t.Z, t.P);
                q = BigInteger.Remainder(q, t.P);
                if (_sh.Correct != 0)
                {
                    q += _sh.Correct;
                }

                Message[i] = q.ToByteArray();
            }
        }