Ejemplo n.º 1
0
                private string Encrypt(byte[] data, byte[] password)
                {
                    var rLen         = NextBlockMultiple(data.Length + 4);
                    var keyBuffer    = CreateKey(password);
                    var blockBuffer  = BlockPool.Rent(2);
                    var result       = BufferPool.Rent(rLen);
                    var lengthBuffer = BufferPool.Rent(4);
                    var bitBuffer    = BufferPool.Rent(4);

                    FastBitConverter.SetUInt32Unsafe(lengthBuffer, (uint)data.Length, 0);
                    Array.Copy(lengthBuffer, 0, result, 0, 4);
                    Array.Copy(data, 0, result, 4, data.Length);
                    using (var stream = new MemoryStream(result))
                    {
                        for (int i = 0; i < rLen; i += 8)
                        {
                            blockBuffer[0] = FastBitConverter.GetUInt32UnsafeFastest(result, (uint)i);
                            blockBuffer[1] = FastBitConverter.GetUInt32UnsafeFastest(result, (uint)i + 4);
                            EncryptBlock(blockBuffer, keyBuffer);
                            FastBitConverter.SetUInt32Unsafe(bitBuffer, blockBuffer[0], 0);
                            stream.Write(bitBuffer, 0, 4);
                            FastBitConverter.SetUInt32Unsafe(bitBuffer, blockBuffer[1], 0);
                            stream.Write(bitBuffer, 0, 4);
                        }
                    }

                    var str = Convert.ToBase64String(result);

                    BlockPool.Return(blockBuffer);
                    BufferPool.Return(result);
                    BufferPool.Return(lengthBuffer);
                    BufferPool.Return(bitBuffer);
                    BlockPool.Return(keyBuffer);
                    return(str);
                }
Ejemplo n.º 2
0
                /// <summary>
                /// This function will not throw anything.
                /// </summary>
                /// <param name="data">The data to decrypt.</param>
                /// <param name="key">The key to use.</param>
                /// <returns>An array if successful, null if not.</returns>
                public byte[] DecryptNonExceptable(byte[] data, byte[] key)
                {
                    if (data.Length % 8 != 0)
                    {
                        return(null);
                    }
                    var keyBuffer   = CreateKey(key);
                    var blockBuffer = BlockPool.Rent(2);
                    var buffer      = BufferPool.Rent(data.Length);
                    var bitBuffer   = BufferPool.Rent(4);

                    Array.Copy(data, buffer, data.Length);
                    using (var stream = new MemoryStream(buffer))
                    {
                        for (uint i = 0; i < buffer.Length; i += 8)
                        {
                            blockBuffer[0] = FastBitConverter.GetUInt32UnsafeFastest(buffer, i);
                            blockBuffer[1] = FastBitConverter.GetUInt32UnsafeFastest(buffer, i + 4);
                            DecryptBlock(blockBuffer, keyBuffer);
                            FastBitConverter.SetUInt32Unsafe(bitBuffer, blockBuffer[0], 0);
                            stream.Write(bitBuffer, 0, 4);
                            FastBitConverter.SetUInt32Unsafe(bitBuffer, blockBuffer[1], 0);
                            stream.Write(bitBuffer, 0, 4);
                        }
                    }

                    var length = FastBitConverter.GetUInt32UnsafeFastest(buffer, 0);

                    if (length > buffer.Length - 4)
                    {
                        BufferPool.Return(buffer);
                        BufferPool.Return(bitBuffer);
                        BlockPool.Return(blockBuffer);
                        BlockPool.Return(keyBuffer);
                        return(null);
                    }

                    var result = new byte[length];

                    Array.Copy(buffer, 4, result, 0, length);
                    BufferPool.Return(buffer);
                    BufferPool.Return(bitBuffer);
                    BlockPool.Return(blockBuffer);
                    BlockPool.Return(keyBuffer);
                    return(result);
                }