Пример #1
0
        public static byte[] ReadOPData(byte[] opdata, KeyPair keyPair)
        {
            if (!MAC.CheckMAC(new ArraySegment <byte>(opdata, 0, opdata.Length - MAC.MAC_SIZE), new ArraySegment <byte>(opdata, opdata.Length - MAC.MAC_SIZE, MAC.MAC_SIZE), keyPair.macKey))
            {
                return(null);
            }

            ArraySegment <byte> magic = new ArraySegment <byte>(opdata, 0, OPDATA_MAGIC_NUMBER.Length);

            for (int i = 0; i < OPDATA_MAGIC_NUMBER.Length; i++)
            {
                if (magic.Array[magic.Offset + i] != OPDATA_MAGIC_NUMBER[i])
                {
                    return(null);
                }
            }

            ArraySegment <byte> length = new ArraySegment <byte>(opdata, OPDATA_MAGIC_NUMBER.Length, PAYLOAD_LENGTH_SIZE);

            ulong payloadLength = 0;

            for (int i = 0; i < PAYLOAD_LENGTH_SIZE; i++)
            {
                payloadLength |= (ulong)length.Array[length.Offset + i] << (i * 8);
            }

            byte[] decryptedBlob = Crypto.Decrypt(new ArraySegment <byte>(opdata, OPDATA_MAGIC_NUMBER.Length + PAYLOAD_LENGTH_SIZE + Crypto.IV_SIZE, opdata.Length - OPDATA_MAGIC_NUMBER.Length - PAYLOAD_LENGTH_SIZE - Crypto.IV_SIZE - MAC.MAC_SIZE), new ArraySegment <byte>(opdata, OPDATA_MAGIC_NUMBER.Length + PAYLOAD_LENGTH_SIZE, Crypto.IV_SIZE), keyPair.encryptionKey);

            byte[] plainText = new byte[payloadLength];

            Array.Copy(decryptedBlob, decryptedBlob.Length - (int)payloadLength, plainText, 0, plainText.Length);

            return(plainText);
        }
Пример #2
0
        public static KeyPair DecryptItemKey(byte[] data, KeyPair keyPair)
        {
            if (!MAC.CheckMAC(new ArraySegment <byte>(data, 0, data.Length - MAC.MAC_SIZE), new ArraySegment <byte>(data, data.Length - MAC.MAC_SIZE, MAC.MAC_SIZE), keyPair.macKey))
            {
                return(null);
            }

            byte[] itemKeyDecryptedBlob = Crypto.Decrypt(new ArraySegment <byte>(data, Crypto.IV_SIZE, data.Length - Crypto.IV_SIZE - MAC.MAC_SIZE), new ArraySegment <byte>(data, 0, Crypto.IV_SIZE), keyPair.encryptionKey);
            if (itemKeyDecryptedBlob == null || itemKeyDecryptedBlob.Length == 0)
            {
                return(null);
            }

            byte[] itemEncryptionKey = new byte[itemKeyDecryptedBlob.Length / 2];
            byte[] itemMACKey        = new byte[itemKeyDecryptedBlob.Length / 2];

            Array.Copy(itemKeyDecryptedBlob, 0, itemEncryptionKey, 0, itemEncryptionKey.Length);
            Array.Copy(itemKeyDecryptedBlob, itemEncryptionKey.Length, itemMACKey, 0, itemMACKey.Length);

            return(new KeyPair()
            {
                encryptionKey = itemEncryptionKey, macKey = itemMACKey
            });
        }