Пример #1
0
        public override byte[] Decrypt(byte[] data)
        {
            lock (this)
            {
                using (ByteBuffer buffer = new ByteBuffer(data))
                {
                    byte[] header = buffer.ReadBytes(4);

                    if (!this.Decryptograph.IsValidPacket(header))
                    {
                        throw new CryptographyException("Invalid header.");
                    }

                    byte[] content = buffer.GetContent();

                    int length = AesCryptograph.RetrieveLength(header);

                    if (content.Length == length)
                    {
                        this.Decryptograph.Crypt(content);
                        BlurCryptograph.Decrypt(content);

                        return(content);
                    }
                    else
                    {
                        throw new CryptographyException(string.Format("Packet length not matching ({0} != {1}).", content.Length, length));
                    }
                }
            }
        }
Пример #2
0
        public override byte[] Encrypt(byte[] data)
        {
            lock (this)
            {
                byte[] result = new byte[data.Length];
                Buffer.BlockCopy(data, 0, result, 0, data.Length);

                byte[] header = this.Encryptograph.GenerateHeader(result.Length);

                BlurCryptograph.Encrypt(result);
                this.Encryptograph.Crypt(result);

                using (ByteBuffer buffer = new ByteBuffer(data.Length + 4))
                {
                    buffer.WriteBytes(header);
                    buffer.WriteBytes(result);

                    return(buffer.Array);
                }
            }
        }