Example #1
0
        public byte[] Encrypt(String str)
        {
            BigInteger2[] message = this.splitMessage(Encoding.UTF8.GetBytes(str), this.n - 1, "encrypt");
            mess = new BigInteger2[message.Length];
            mess = message;
            //MessageBox.Show("Message Length: " + message.Length);
            //return data;
            ArrayList resultSet = new ArrayList();

            GC.Collect();
            //Encrypt each Message and populate resultSet
            for (int i = 0; i < message.Length; i++)
            {
                BigInteger2 unitResult = BigIntegerExtensions.modPower(message[i], e, Mod);
                unitResult = BigInteger2.NegateZeros(unitResult);

                if (i == 0 && unitResult.bitlength.Length - 2 < n)
                {
                    //MessageBox.Show("Padding with 'a'");
                    int         ind = n + 4 - unitResult.bitlength.Length;
                    BigInteger2 tmp = new BigInteger2(n, 0);

                    for (int xi = ind, xj = 2; xj < unitResult.bitlength.Length; xi++, xj++)
                    {
                        tmp.bitlength[xi] = unitResult.bitlength[xj];
                    }
                    unitResult = tmp;
                    //PAD Left with byte 'a'
                    byte[]   pad    = ASCIIEncoding.ASCII.GetBytes("a");
                    BitArray padAry = new BitArray(pad);

                    //PAD left of unitResult with padAry
                    BigInteger2 padded = new BigInteger2(unitResult.bitlength.Length - 2 + padAry.Length, 0);
                    int         pi, pj;
                    for (pi = 2, pj = 0; pj < padAry.Length; pi++, pj++)
                    {
                        padded.bitlength[pi] = padAry[pj];
                    }

                    //Continue filling from unitResult
                    for (int indi = pi, indj = 2; indi < padded.bitlength.Length; indi++, indj++)
                    {
                        padded.bitlength[indi] = unitResult.bitlength[indj];
                    }
                    unitResult = padded;
                }
                if (i > 0 && unitResult.bitlength.Length - 2 < n)
                {
                    //MessageBox.Show("Padding with zeros");
                    //pad result's leftside with zeros
                    int         ind = n + 4 - unitResult.bitlength.Length;
                    BigInteger2 tmp = new BigInteger2(n, 0);
                    for (int xi = ind, xj = 2; xj < unitResult.bitlength.Length; xi++, xj++)
                    {
                        tmp.bitlength[xi] = unitResult.bitlength[xj];
                    }

                    unitResult = null;
                    unitResult = tmp;
                }

                GC.Collect();
                resultSet.Add(unitResult);
            }

            //Create Full BigInteger2 containing bits from resultSet BigIntegers

            int bitSize = 0;

            foreach (BigInteger2 b in resultSet)
            {
                bitSize += (b.bitlength.Length - 2);
            }

            BigInteger2 wholeMessage = new BigInteger2(bitSize, 0);

            //Fill wholeMessage
            int curIndex = 2;

            foreach (BigInteger2 b in resultSet)
            {
                for (int i = 2; i < b.bitlength.Length; i++, curIndex++)
                {
                    wholeMessage.bitlength[curIndex] = b.bitlength[i];
                }
            }

            byte[] result = BigInteger2.getKeyBytes(wholeMessage);
            GC.Collect();
            // MessageBox.Show("Encryption Completed!!!");
            return(result);
        }
Example #2
0
 public byte[] getPrivateKey()
 {
     d = BigInteger2.NegateZeros(d);
     return(BigInteger2.getKeyBytes(d));
 }
Example #3
0
 public byte[] getModValue()
 {
     return(BigInteger2.getKeyBytes(Mod));
 }
Example #4
0
 public byte[] getPublicKey()
 {
     e = BigInteger2.NegateZeros(e);
     return(BigInteger2.getKeyBytes(e));
 }