예제 #1
0
        public string Encode(PublicKey key, string message)
        {
            string cipherText;
            int    mLen = message.Length;

            if (mLen > key.getK() + 11)
            {
                throw new MessageTooLongException();
            }

            BigInteger psLen = key.getK() - mLen - 3;
            string     ps    = "";
            char       randomByte;
            Random     random = new Random(1);

            for (int i = 0; i < psLen; i++)
            {
                while ((randomByte = Convert.ToChar(random.Next() % 256)) == '0')
                {
                    ;
                }
                ps += randomByte;
            }

            string     em = "02" + ps + "0" + message;
            BigInteger m  = OS2IP(em);

            m          = PowModul(m, key.getE(), key.GetN());
            cipherText = I2OSP(m, key.getK());
            return(cipherText);
        }
예제 #2
0
        public void Start(string cipherText)
        {
            BigInteger r;
            BigInteger previousS;
            bool       finded;
            int        i = 0;

            while (true)
            {
                switch (i)
                {
                case 0:
                    CheckItems(cipherText);
                    Intervals.Add(new Interval()
                    {
                        Left  = 2 * B,
                        Right = 3 * B - 1
                    }
                                  );
                    S0 = S;
                    break;

                case 1:
                    S = PublicKey.GetN() / (2 * B);
                    CheckItems(cipherText);
                    NarrowInterval();
                    if (CheckIntervals())
                    {
                        return;
                    }
                    break;

                default:
                    if (Intervals.Count == 1)
                    {
                        finded    = false;
                        previousS = S;
                        r         = 2 * (Intervals[0].Right * previousS - 2 * B) / PublicKey.GetN();
                        while (!finded)
                        {
                            S      = (2 * B + r * PublicKey.GetN()) / Intervals[0].Right;
                            rightS = (3 * B + r * PublicKey.GetN()) / Intervals[0].Left;
                            for (; S < rightS; S++)
                            {
                                try
                                {
                                    CheckItem(cipherText);
                                }
                                catch (DecryptionErrorExecption)
                                {
                                    continue;
                                }
                                finded = true;
                                break;
                            }
                            r++;
                        }
                    }
                    else
                    {
                        S++;
                        CheckItems(cipherText);
                    }
                    NarrowInterval();
                    if (CheckIntervals())
                    {
                        return;
                    }
                    break;
                }
                i++;
            }
        }