Пример #1
0
        private static BitString GetLengthAsByte(BitString content)
        {
            if (content.BitLength < 1024)                                      // 128 * 8 bits
            {
                return(BitString.To8BitString((byte)(content.BitLength / 8))); // To add to below, byte is naturally unsigned, 0 to 255.
            }
            else if (content.BitLength < 524288)                               // 65536 * 8 bits
            {
                // Length is a multi-part field, the first bit is set to 1. The next 7 bits communicate the length (bytes) of the upcoming length property
                // Then the actual length of the content is present after.

                var bytesInContent       = content.BitLength / 8;
                var length               = BitString.PadToNextByteBoundry(((BigInteger)bytesInContent).ExactBitString());
                var bytesNeededForLength = length.BitLength / 8;

                var preamble = BitString.PadToNextByteBoundry(((BigInteger)bytesNeededForLength).ExactBitString());
                preamble.Set(0, true);

                return(preamble.ConcatenateBits(length));
            }
            else // We shouldn't be generating DER encoded strings this long
            {
                throw new Exception("Content too large to encode in DER encoder");
            }
        }
Пример #2
0
        public override SymmetricCipherResult ProcessPayload(IModeBlockCipherParameters param)
        {
            CheckPayloadRequirements(param.Payload);
            var key = param.Key.ToBytes();
            var actualBitsToProcess = param.Payload.BitLength;

            param.Payload = BitString.PadToNextByteBoundry(param.Payload);

            // CFB always utilizes engine in encrypt mode
            var engineParam = new EngineInitParameters(BlockCipherDirections.Encrypt, key, param.UseInverseCipherMode);

            _engine.Init(engineParam);

            var numberOfSegments = actualBitsToProcess / _shiftRegisterStrategy.ShiftSize;
            var outBuffer        = GetOutputBuffer(param.Payload.BitLength);

            if (param.Direction == BlockCipherDirections.Encrypt)
            {
                Encrypt(param, numberOfSegments, outBuffer);
            }
            else
            {
                Decrypt(param, numberOfSegments, outBuffer);
            }

            return(new SymmetricCipherResult(
                       new BitString(outBuffer).GetMostSignificantBits(actualBitsToProcess)
                       ));
        }