Пример #1
0
        protected AesCryptoServiceBase(AesCspParameters aesCspParameters)
        {
            if (null == aesCspParameters)
            {
                throw new ArgumentNullException(nameof(aesCspParameters));
            }

            aes = new AesCryptoServiceProvider {
                Key = HexTk.UnHex(aesCspParameters.AesKey),
                IV  = HexTk.UnHex(aesCspParameters.AesIv)
            };
        }
Пример #2
0
        protected string EncryptString <T>(T source)
        {
            string encrypted = null;

            if (null != source)
            {
                using var msEncrypt = new MemoryStream();
                var encryptor = aes.CreateEncryptor();
                using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

                // socpe of sw here is important! Encoding does not happen before sw is closed - writing is finished!
                using (var swEncrypt = new StreamWriter(csEncrypt)) {
                    swEncrypt.Write(source.ToString());
                }

                var encryptedBytes = msEncrypt.ToArray();
                encrypted = HexTk.HexDump(encryptedBytes);
            }

            return(encrypted);
        }
Пример #3
0
        /// <summary>
        ///     inverts the encryption of a string - works if the same AES key has been used for encryption.
        ///     Notice for programmers: Each decryption needs a fresh decryptor, reuse of decryptor throws ugly exceptions.
        /// </summary>
        /// <param name="encryptedText"></param>
        /// <returns></returns>
        protected string DecryptString(string encryptedText)
        {
            var decrypted = string.Empty;

            try {
                var trimmed = encryptedText.ParseText();
                if (!string.IsNullOrEmpty(trimmed))
                {
                    var unHexed = HexTk.UnHex(trimmed);
                    using var msDecrypt = new MemoryStream(unHexed);
                    // must get a fresh Decryptor for each decryption
                    var decryptor = aes.CreateDecryptor();
                    using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
                    using var srDecrypt = new StreamReader(csDecrypt);
                    decrypted           = srDecrypt.ReadToEnd();
                }
            } catch (Exception exception) {
                Log.Warning(exception, AesResources.Decryption_Failed, encryptedText);
            }

            return(decrypted);
        }