Пример #1
0
        public string Decrypt(string keyMaterial, Encrypted encrypted)
        {
            CryptographicKey key = SymmetricKeyFromMaterial(keyMaterial);

            IBuffer encryptedBuffer = CryptographicBuffer.DecodeFromBase64String(encrypted.Value);
            IBuffer decryptedBuffer = CryptographicEngine.Decrypt(key, encryptedBuffer, null);

            return CryptographicBuffer.ConvertBinaryToString(encrypted.Encoding, decryptedBuffer);
        }
Пример #2
0
        /// <summary>
        /// Encrypts the text using the keyMaterial. This encryption only works with
        /// PKCS7 algorithms which handle the padding and also non CBC algorithms so 
        /// that it doesn't have to deal with creating an initialization vector
        /// </summary>
        public Encrypted Encrypt(string keyMaterial, string text)
        {
            CryptographicKey key = SymmetricKeyFromMaterial(keyMaterial);

            // Create a buffer that contains the encoded message to be encrypted. 
            IBuffer buffMsg = CryptographicBuffer.ConvertStringToBinary(text, BinaryStringEncoding.Utf8);

            IBuffer encryptedBuffer = CryptographicEngine.Encrypt(key, buffMsg, null);
            string encryptedValue = CryptographicBuffer.EncodeToBase64String(encryptedBuffer);
            var encrypted = new Encrypted(EncryptAlgorithm, BinaryStringEncoding.Utf8, encryptedValue);

            return encrypted;
        }