示例#1
0
 public MegaAesCtrStreamCrypter(Stream stream)
     : base(stream, stream.Length, Mode.Crypt, Crypto.CreateAesKey(), Crypto.CreateAesKey().CopySubArray(8))
 {
 }
示例#2
0
 // Token: 0x060008D4 RID: 2260 RVA: 0x0004B04C File Offset: 0x0004924C
 public static byte[] EncryptAttributes(Attributes attributes, byte[] nodeKey)
 {
     byte[] array = ("MEGA" + JsonConvert.SerializeObject(attributes, Formatting.None)).ToBytes();
     array = array.CopySubArray(array.Length + 16 - array.Length % 16, 0);
     return(Crypto.EncryptAes(array, nodeKey));
 }
示例#3
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (this.position == this.streamLength)
            {
                return(0);
            }

            for (long pos = this.position; pos < Math.Min(this.position + count, this.streamLength); pos += 16)
            {
                // We are on a chunk bondary
                if (this.chunksPositions.Any(chunk => chunk == pos))
                {
                    if (pos != 0)
                    {
                        // Compute the current chunk mac data on each chunk bondary
                        this.ComputeChunk();
                    }

                    // Init chunk mac with Iv values
                    for (int i = 0; i < 8; i++)
                    {
                        this.currentChunkMac[i]     = this.iv[i];
                        this.currentChunkMac[i + 8] = this.iv[i];
                    }
                }

                this.IncrementCounter();

                // Iterate each AES 16 bytes block
                byte[] input       = new byte[16];
                byte[] output      = new byte[input.Length];
                int    inputLength = this.stream.Read(input, 0, input.Length);
                if (inputLength != input.Length)
                {
                    // Sometimes, the stream is not finished but the read is not complete
                    inputLength += this.stream.Read(input, inputLength, input.Length - inputLength);
                }

                // Merge Iv and counter
                byte[] ivCounter = new byte[16];
                Array.Copy(this.iv, ivCounter, 8);
                Array.Copy(this.counter, 0, ivCounter, 8, 8);

                byte[] encryptedIvCounter = Crypto.EncryptAes(ivCounter, this.fileKey);

                for (int inputPos = 0; inputPos < inputLength; inputPos++)
                {
                    output[inputPos] = (byte)(encryptedIvCounter[inputPos] ^ input[inputPos]);
                    this.currentChunkMac[inputPos] ^= (this.mode == Mode.Crypt) ? input[inputPos] : output[inputPos];
                }

                // Copy to buffer
                Array.Copy(output, 0, buffer, offset + pos - this.position, Math.Min(output.Length, this.streamLength - pos));

                // Crypt to current chunk mac
                this.currentChunkMac = Crypto.EncryptAes(this.currentChunkMac, this.fileKey);
            }

            long len = Math.Min(count, this.streamLength - this.position);

            this.position += len;

            // When stream is fully processed, we compute the last chunk
            if (this.position == this.streamLength)
            {
                this.ComputeChunk();

                // Compute Meta MAC
                for (int i = 0; i < 4; i++)
                {
                    this.metaMac[i]     = (byte)(this.fileMac[i] ^ this.fileMac[i + 4]);
                    this.metaMac[i + 4] = (byte)(this.fileMac[i + 8] ^ this.fileMac[i + 12]);
                }

                this.OnStreamRead();
            }

            return((int)len);
        }