Пример #1
0
        /// <summary>
        ///     Decrypts a string using the <see cref="Aes"/> algorithm.
        /// </summary>
        /// <param name="s">The encrypted string.</param>
        /// <param name="data">The <see cref="CryptographicData"/> used to encrypt the string.</param>
        /// <returns>Returns the decrypted string.</returns>
        public async Task <string> AesDecryptAsync(string s, CryptographicData data)
        {
            var transform    = aes.CreateDecryptor(data.AesKey, data.AesIV);
            var memoryStream = new MemoryStream(Convert.FromBase64String(s));
            var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read);
            var reader       = new StreamReader(cryptoStream);
            var decrypted    = await reader.ReadToEndAsync();

            reader.Dispose();
            cryptoStream.Dispose();
            memoryStream.Dispose();
            transform.Dispose();

            return(decrypted);
        }
Пример #2
0
        /// <summary>
        ///     Encrypts a string using the <see cref="Aes"/> algorithm.
        /// </summary>
        /// <param name="s">The cipher string.</param>
        /// <param name="data">The <see cref="CryptographicData"/> that should be used.</param>
        /// <returns>Returns the encrypted string.</returns>
        public async Task <string> AesEncryptAsync(string s, CryptographicData data)
        {
            var transform    = aes.CreateEncryptor(data.AesKey, data.AesIV);
            var memoryStream = new MemoryStream();

            using (var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write)) {
                using (var writer = new StreamWriter(cryptoStream)) {
                    await writer.WriteAsync(s);
                }
            }

            var encrypted = Convert.ToBase64String(memoryStream.ToArray());

            memoryStream.Dispose();
            transform.Dispose();

            return(encrypted);
        }
Пример #3
0
 /// <summary>
 ///     Validates a signature for a string using the <see cref="HMACSHA256"/> algorithm.
 /// </summary>
 /// <param name="s">The string to check.</param>
 /// <param name="signature">The signature to validate.</param>
 /// <param name="data">The <see cref="CryptographicData"/> used to create the signature.</param>
 /// <returns>Returns "true" if the signature is valid, otherwise "false".</returns>
 public bool HmacValidateSignature(string s, string signature, CryptographicData data)
 {
     hmac = new HMACSHA256(data.HmacKey);
     return(string.Equals(signature, HmacCreateSignature(s, data)));
 }
Пример #4
0
 /// <summary>
 ///     Creates a signature for a string using the <see cref="HMACSHA256"/> algorithm.
 /// </summary>
 /// <param name="s">The string to sign.</param>
 /// <param name="data">The <see cref="CryptographicData"/> that should be used.</param>
 /// <returns>Returns the signature for the string.</returns>
 public string HmacCreateSignature(string s, CryptographicData data)
 {
     hmac = new HMACSHA256(data.HmacKey);
     return(Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(s))));
 }