Example #1
0
        public String decipher(List <Org.BouncyCastle.Math.BigInteger> cipher, MHPrivateKey key)
        {
            String decrypted = "";

            Org.BouncyCastle.Math.BigInteger temp = Org.BouncyCastle.Math.BigInteger.ValueOf(0);
            int tmp = 0;

            Org.BouncyCastle.Math.BigInteger bits = Org.BouncyCastle.Math.BigInteger.ValueOf(0);

            for (int i = 0; i < cipher.Count; i++)
            {
                temp = cipher.ElementAt(i);
                int bitlen = temp.BitLength;
                int ff     = 0;
                while (bitlen < (int)Math.Pow(2, ff))
                {
                    ff++;
                }
                if (ff > bitlen)
                {
                    bitlen = ff;
                }

                for (int j = 0; j < bitlen; j++)
                {
                    if (temp.Mod(Org.BouncyCastle.Math.BigInteger.ValueOf(2)).CompareTo(Org.BouncyCastle.Math.BigInteger.ValueOf(1)) == 0)
                    {
                        bits = bits.Add(key.w1.Multiply(Org.BouncyCastle.Math.BigInteger.ValueOf((long)Math.Pow(2, j))));
                    }
                    temp = temp.ShiftRight(1);
                }
                bits = bits.Mod(key.n);
                List <Org.BouncyCastle.Math.BigInteger> list = key.a;
                Org.BouncyCastle.Math.BigInteger        temper;

                int k = key.a.Count - 1;
                while (k >= 0)
                {
                    temper = list.ElementAt(k);
                    if (bits.CompareTo(temper) > -1)
                    {
                        tmp += (int)Math.Pow(2, k);
                        bits = bits.Subtract(temper);
                    }
                    k--;
                }
                decrypted += (binaryToChar(Convert.ToString(tmp, 2))).ToString();

                bits = Org.BouncyCastle.Math.BigInteger.ValueOf(0);
                tmp  = 0;
            }
            return(decrypted);
        }
Example #2
0
        public MHKey()
        {
            generator gen = new generator(8);

            Org.BouncyCastle.Math.BigInteger w = gen.w;
            e = new List <Org.BouncyCastle.Math.BigInteger>();
            for (int i = 0; i < gen.a.Count; i++)
            {
                e.Add((w.Multiply(gen.a.ElementAt(i))).Mod(gen.n)); // w*a Mod n ..
            }

            Org.BouncyCastle.Math.BigInteger w1  = Org.BouncyCastle.Math.BigInteger.ValueOf(1);
            Org.BouncyCastle.Math.BigInteger one = Org.BouncyCastle.Math.BigInteger.ValueOf(2);
            while (one.IntValue != 1)
            {
                w1  = w1.Add(Org.BouncyCastle.Math.BigInteger.ValueOf(1)).Mod(gen.n);
                one = w.Multiply(w1).Mod(gen.n);
            }
            privateKey = new MHPrivateKey(gen.a, w.ModInverse(gen.n), gen.n);
        }