コード例 #1
0
        public byte[] Protect(byte[] plaintext)
        {
            var key = Task.Factory.RunSync(() => DataKeyService.GenerateKey());

            var encryptedData = CreateProtector(key.EncryptionKey).Protect(plaintext);

            // To make it secure, we should combine the key's length, the key and the cipher data.
            var cipher = key.EncryptionKeyReference;

            if (cipher.Length > byte.MaxValue)
            {
                throw new Exception("Cipher key is longer than a byte!");
            }

            return(new byte[] { (byte)cipher.Length }
                   .Concat(cipher, encryptedData).ToArray().GZip());
        }