Beispiel #1
0
        public bool Decode(byte[] data, string password)
        {
            var msEncoded = new MemoryStream(data);

            byte[] buffer = new byte[4];

            msEncoded.Read(buffer, 0, 4);
            int encodedBytes = BitConverter.ToInt32(buffer, 0);

            buffer = new byte[encodedBytes];
            msEncoded.Read(buffer, 0, buffer.Length);
            byte[] decodedBytes = EncryptionManager.DecryptData(buffer, password);

            msEncoded.Read(buffer, 0, 4);
            int hashByteLength = BitConverter.ToInt32(buffer, 0);

            if (hashByteLength > 512)
            {
                return(false);
            }

            byte[] hashBytesFromData = new byte[hashByteLength];
            msEncoded.Read(hashBytesFromData, 0, hashBytesFromData.Length);
            byte[] hashBytesFromDecodedDataBytes = SHA512.Create().ComputeHash(decodedBytes, 0, decodedBytes.Length);

            bool hashCompareResult = GeneralConverters.ByteArrayToBase64(hashBytesFromData) == GeneralConverters.ByteArrayToBase64(hashBytesFromDecodedDataBytes);

            if (!hashCompareResult)
            {
                return(false);
            }

            var msDecoded = new MemoryStream(decodedBytes);

            buffer = new byte[4];

            msDecoded.Read(buffer, 0, buffer.Length);
            int leftPaddingLength = BitConverter.ToInt32(buffer, 0);

            msDecoded.Read(buffer, 0, buffer.Length);
            int rightPaddingLength = BitConverter.ToInt32(buffer, 0);

            msDecoded.Read(buffer, 0, buffer.Length);
            int totalLength = BitConverter.ToInt32(buffer, 0);

            byte[] decodedDataBlock = new byte[totalLength];
            msDecoded.Read(decodedDataBlock, 0, decodedDataBlock.Length);

            byte[] sharedSecretBytes = new byte[totalLength - leftPaddingLength - rightPaddingLength];
            Buffer.BlockCopy(decodedDataBlock, leftPaddingLength, sharedSecretBytes, 0, sharedSecretBytes.Length);

            SharedSecret = GeneralConverters.GetStringFromByteArray(sharedSecretBytes);

            return(true);
        }