BigInteger.getBytes C# (CSharp) Method

getBytes() public method

public getBytes ( ) : byte[]
return byte[]
        public byte[] getBytes()
        {
                int numBits = bitCount();

                int numBytes = numBits >> 3;
                if((numBits & 0x7) != 0)
                        numBytes++;

                byte[] result = new byte[numBytes];

                //Console.WriteLine(result.Length);

                int pos = 0;
                uint tempVal, val = data[dataLength - 1];

                if((tempVal = (val >> 24 & 0xFF)) != 0)
                        result[pos++] = (byte)tempVal;
                if((tempVal = (val >> 16 & 0xFF)) != 0)
                        result[pos++] = (byte)tempVal;
                if((tempVal = (val >> 8 & 0xFF)) != 0)
                        result[pos++] = (byte)tempVal;
                if((tempVal = (val & 0xFF)) != 0)
                        result[pos++] = (byte)tempVal;

                for(int i = dataLength - 2; i >= 0; i--, pos += 4)
                {
                        val = data[i];
                        result[pos+3] = (byte)(val & 0xFF);
                        val >>= 8;
                        result[pos+2] = (byte)(val & 0xFF);
                        val >>= 8;
                        result[pos+1] = (byte)(val & 0xFF);
                        val >>= 8;
                        result[pos] = (byte)(val & 0xFF);
                }

                return result;
        }

Usage Example

Ejemplo n.º 1
0
        private static byte[] Compute(byte[] data, RSAPublicKey publicKey, int blockSize)
        {
            //
            // 公钥加密/解密公式为:ci = mi^e ( mod n )
            //
            // 先将 m(二进制表示)分成数据块 m1, m2, ..., mi ,然后进行运算。
            //
            BigInteger e = new BigInteger(publicKey.Exponent);
            BigInteger n = new BigInteger(publicKey.Modulus);

            int blockOffset = 0;

            using (MemoryStream stream = new MemoryStream())
            {
                while (blockOffset < data.Length)
                {
                    int    blockLen  = Math.Min(blockSize, data.Length - blockOffset);
                    byte[] blockData = new byte[blockLen];
                    Buffer.BlockCopy(data, blockOffset, blockData, 0, blockLen);

                    BigInteger mi = new BigInteger(blockData);
                    BigInteger ci = mi.modPow(e, n);//ci = mi^e ( mod n )

                    byte[] block = ci.getBytes();
                    stream.Write(block, 0, block.Length);
                    blockOffset += blockLen;
                }

                return(stream.ToArray());
            }
        }
All Usage Examples Of BigInteger::getBytes