public static byte[] UnpackAndDecrypt(byte[] hashedPassword, string purpose, Aes256CryptoBuffer key, out string keyId)
        {
            using (var key1 = new Aes256CryptoBuffer(key.Data))
            {
                key1.UnpackAndDecryptX(hashedPassword);
                if (key1.MimeType != "KeyX")
                {
                    throw new Exception("Not a KeyX.");
                }
                if (key1.Purpose != purpose)
                {
                    throw new Exception("Wrong key for purpose.");
                }

                using (var key2 = new Aes256CryptoBuffer(key1.PlainText))
                {
                    key2.UnpackAndDecryptX(hashedPassword);

                    if (key2.Id != key2.KeyId)
                    {
                        throw new Exception("Not a key.");
                    }
                    if (key2.Purpose != purpose)
                    {
                        throw new Exception("Wrong key for purpose.");
                    }
                    keyId = key2.KeyId;

                    var keyUnpadded = new byte[32];
                    Array.Copy(key2.PlainText, 0, keyUnpadded, 0, 32);

                    return(keyUnpadded);
                }
            }
        }
        public static Aes256CryptoBuffer UnpackAndDecrypt(byte[] hashedPassword, string purpose, Aes256CryptoBuffer key, Aes256CryptoBuffer cipherText)
        {
            using (var key1 = new Aes256CryptoBuffer(key.Data))
            {
                key1.UnpackAndDecryptX(hashedPassword);
                if (key1.MimeType != "KeyX")
                {
                    throw new Exception("Not a KeyX.");
                }
                if (key1.Purpose != purpose)
                {
                    throw new Exception("Wrong key for purpose.");
                }

                using (var key2 = new Aes256CryptoBuffer(key1.PlainText))
                {
                    key2.UnpackAndDecryptX(hashedPassword);

                    if (key2.Id != key2.KeyId)
                    {
                        throw new Exception("Not a key.");
                    }
                    if (key2.Purpose != purpose)
                    {
                        throw new Exception("Wrong key for purpose.");
                    }

                    cipherText.UnpackAndDecryptX(key2.PlainText);
                    return(cipherText);
                }
            }
        }