Example #1
0
        //私钥加密
        private static byte[] priEncrypt(byte[] block, RSACryptoServiceProvider key)
        {
            RSAParameters param    = key.ExportParameters(true);
            BigInteger2   d        = new BigInteger2(param.D);
            BigInteger2   n        = new BigInteger2(param.Modulus);
            BigInteger2   biText   = new BigInteger2(block);
            BigInteger2   biEnText = biText.modPow(d, n);

            return(biEnText.getBytes());
        }
Example #2
0
        public BigInteger2[] splitMessage(byte[] wholeMesg, int mesgCellBits, string direction)
        {
            BigInteger2[] result = null;

            if (direction.Equals("decrypt"))
            {
                BitArray fullMesgArry = new BitArray(wholeMesg);
                int      wholeBits    = wholeMesg.Length * 8;

                int cells = 0;

                //remove front few zeros of padding
                BitArray firstByte = new BitArray(8, false);
                for (int m = 0; m < firstByte.Length; m++)
                {
                    firstByte[m] = fullMesgArry[m];
                }
                int x = 0;
                for (x = 0; x < firstByte.Length; x++)
                {
                    if (firstByte[x])
                    {
                        break;
                    }
                }
                //value of first x zero values to be deleted
                BitArray temp = new BitArray(wholeBits - x, false);

                //Fill the temp array
                int ix, jx;
                for (ix = x, jx = 0; ix < fullMesgArry.Length; ix++, jx++)
                {
                    temp[jx] = fullMesgArry[ix];
                }
                fullMesgArry = null;
                fullMesgArry = temp;

                //First Test to see whether 1st byte=='a'
                BitArray pad = new BitArray(8, false);

                //fill pad with first byte of wholeMesg
                for (int pi = 0; pi < 8; pi++)
                {
                    pad[pi] = fullMesgArry[pi];
                }

                BigInteger2 padInteger = new BigInteger2(8, 0);
                for (int pi = 2, pj = 0; pi < padInteger.bitlength.Length; pi++, pj++)
                {
                    padInteger.bitlength[pi] = pad[pj];
                }


                byte[] padbytes = BigInteger2.getBytes(padInteger);
                if (ASCIIEncoding.ASCII.GetBytes("a")[0] == padbytes[0])
                {
                    //MessageBox.Show("I am in removing 'a' byte from padding");
                    //Strip the first byte of fullMsgAry
                    BitArray fullAry = new BitArray(fullMesgArry.Length - 8, false);
                    for (int pi = 8, pj = 0; pi < fullMesgArry.Length; pi++, pj++)
                    {
                        fullAry[pj] = fullMesgArry[pi];
                    }
                    fullMesgArry = fullAry;
                }

                wholeBits = fullMesgArry.Length;
                cells     = wholeBits / mesgCellBits;

                int remBits = wholeBits % mesgCellBits;

                //number of BigInteger2 arry
                int numbOfFullCells = cells;
                int fullBits        = cells * mesgCellBits;
                if (remBits > 0)
                {
                    cells += 1;
                    //MessageBox.Show("Added 1 cell for remaining bits");
                }

                result = new BigInteger2[cells];

                //Fill cells:
                int i        = 0;
                int curIndex = 2;
                int curCell  = 0;

                if (numbOfFullCells > 0)
                {
                    //MessageBox.Show("I am in Full Cells");
                    BigInteger2 cellBgInt = new BigInteger2(mesgCellBits, 0);
                    for (i = 0; i < fullBits; i++, curIndex++)
                    {
                        cellBgInt.bitlength[curIndex] = fullMesgArry.Get(i);
                        if (curIndex == mesgCellBits + 1)
                        {
                            result[curCell] = cellBgInt;
                            curCell++;
                            curIndex  = 1;
                            cellBgInt = new BigInteger2(mesgCellBits, 0);
                        }
                    }
                }

                //Fill last Cell!!!
                if (remBits > 0)
                {
                    //MessageBox.Show("I am in remaining Cells");
                    BigInteger2 cellBgInt = new BigInteger2(remBits, 0);
                    for (int n = i, j = 2; n < fullMesgArry.Length; n++, j++)
                    {
                        cellBgInt.bitlength[j] = fullMesgArry.Get(n);
                    }
                    result[result.Length - 1] = cellBgInt;
                }

                GC.Collect();
            }
            else//encrypt
            {
                var list = GetBoolListFromBytes(wholeMesg);

                BitArray fullMesgArry = new BitArray(list.Count);
                for (var index = 0; index < fullMesgArry.Length; index++)
                {
                    fullMesgArry[index] = list[index];
                }

                int wholeBits = fullMesgArry.Count;

                int cells = 0;
                cells = wholeBits / (mesgCellBits - 1);

                int remBits = wholeBits % (mesgCellBits - 1);

                //number of BigInteger2 arry
                int numbOfFullCells = cells;
                int fullBits        = cells * (mesgCellBits - 1);
                if (remBits > 0)
                {
                    cells += 1;
                }

                result = new BigInteger2[cells];

                //Fill cells:
                int i        = 0;
                int curIndex = 3;
                int curCell  = 0;

                if (numbOfFullCells > 0)
                {
                    //MessageBox.Show("I am in Full Cells");
                    BigInteger2 cellBgInt = new BigInteger2(mesgCellBits, 0);
                    try
                    {
                        for (i = 0; i < fullBits; i++, curIndex++)
                        {
                            cellBgInt.bitlength[curIndex] = fullMesgArry.Get(i);
                            if (curIndex == mesgCellBits + 1)
                            {
                                cellBgInt.bitlength[2] = true;
                                result[curCell]        = cellBgInt;
                                curCell++;
                                curIndex  = 2;
                                cellBgInt = new BigInteger2(mesgCellBits, 0);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Happens in 1st: " + ex.Message);
                    }
                }

                //Fill last Cell!!!
                if (remBits > 0)
                {
                    //MessageBox.Show("I am in remaining Cells");
                    BigInteger2 cellBgInt = new BigInteger2(remBits + 1, 0);
                    cellBgInt.bitlength[2] = true;
                    try
                    {
                        for (int n = i, j = 3; n < fullMesgArry.Length; n++, j++)
                        {
                            cellBgInt.bitlength[j] = fullMesgArry.Get(n);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Exception happens in 2nd: " + ex.Message);
                    }
                    result[result.Length - 1] = cellBgInt;
                }
                GC.Collect();
            }

            return(result);
        }