public byte[] EncryptRaw(byte[] data, byte[] password)
        {
            var blockLength = _blockManipulator.NextBlockMultiple(data.Length + 8);
            var keyBuffer   = _keyGenerator.CreateKey(password);

            var result = new byte[blockLength];

            var blockBuffer = (Span <uint>) stackalloc uint[2];
            var byteBuffer  = (Span <byte>) stackalloc byte[4];

            Buffer.BlockCopy(BitConverter.GetBytes((long)data.Length), 0, result, 0, 8);

            Buffer.BlockCopy(data, 0, result, 8, data.Length);

            using var ms = new MemoryStream(result);

            for (var i = 0; i < blockLength; i += 8)
            {
                blockBuffer[0] = _fastBitConverter.GetUInt32UnsafeFastest(result, i);
                blockBuffer[1] = _fastBitConverter.GetUInt32UnsafeFastest(result, i + 4);
                _blockManipulator.EncryptBlock(blockBuffer, keyBuffer);
                _fastBitConverter.SetUInt32UnsafeFastest(byteBuffer, blockBuffer[0], 0);
                ms.Write(byteBuffer);
                _fastBitConverter.SetUInt32UnsafeFastest(byteBuffer, blockBuffer[1], 0);
                ms.Write(byteBuffer);
            }

            return(result);
        }
예제 #2
0
 public SimpleCipher()
 {
     generator = new SimpleRamndomGenerator(lenghtOfAlphabet, beginingOfAlphabet);
     key       = generator.CreateKey(100);
 }