Ejemplo n.º 1
0
        /// <summary>
        /// Decrypts the <paramref name="encrypted"/> data with the <paramref name="cipher"/>.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encrypted">The encrypted.</param>
        /// <returns>T.</returns>
        public static Nullable <T> DecryptNullable <T>(
            this ICipher cipher,
            byte[] encrypted) where T : struct
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }
            if (encrypted.Length < 2)
            {
                throw new ArgumentException(Resources.InvalidArgumentLength, nameof(encrypted));
            }
            if (!EncryptTypedData.ContainsKey(typeof(T)))
            {
                throw new ArgumentException("The specified data type cannot be decrypted.");
            }

            var decrypted = cipher.Decrypt(encrypted);

            if (decrypted.Length < 2)
            {
                throw new ArgumentException("The argument is not a valid encrypted Nullable<T> value.");
            }

            return(FromByteArray.ToNullable <T>(decrypted));
        }
Ejemplo n.º 2
0
        public static ushort[] DecryptUInt16Array(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encrypted == null)
            {
                return(null);
            }

            return(FromByteArray.ToUInt16Array(cipher.Decrypt(encrypted)));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Decrypts the <paramref name="encryptedText"/> to a string, provided the original string was encoded with <see cref="T:System.Text.Encoding.UTF8"/>.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encryptedText">The crypto text.</param>
        /// <returns>The decrypted text.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="cipher"/> is <see langword="null"/>.</exception>
        public static string DecryptString(
            this ICipher cipher,
            byte[] encryptedText)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encryptedText == null)
            {
                return(null);
            }

            return(FromByteArray.ToString(cipher.Decrypt(encryptedText)));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Decrypts the <paramref name="encrypted"/> to an array of <see cref="bool"/> values.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encrypted">The data to be decrypted.</param>
        /// <returns>The encrypted text.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="cipher"/> is <see langword="null"/>.</exception>
        public static bool[] DecryptBooleanArray(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }

            if (encrypted == null)
            {
                return(null);
            }

            var decrypted = cipher.Decrypt(encrypted);

            return(FromByteArray.ToBooleanArray(decrypted));
        }
Ejemplo n.º 5
0
        public static ushort DecryptUInt16(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }

            var decrypted = cipher.Decrypt(encrypted);

            if (decrypted.Length < 2)
            {
                throw new ArgumentException(Resources.InvalidEncryptedValue);
            }

            return(FromByteArray.ToChar(decrypted));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Decrypts the <paramref name="encrypted"/> text to a <see cref="decimal"/> value.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="encrypted">The encrypted text.</param>
        /// <returns>The decrypted value.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="cipher"/> or <paramref name="encrypted"/> are <see langword="null"/>.</exception>
        public static Guid DecryptGuid(
            this ICipher cipher,
            byte[] encrypted)
        {
            if (cipher == null)
            {
                throw new ArgumentNullException(nameof(cipher));
            }
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }

            var decrypted = cipher.Decrypt(encrypted);

            if (decrypted.Length != 16)
            {
                throw new ArgumentException(Resources.InvalidEncryptedValue);
            }

            return(FromByteArray.ToGuid(decrypted));
        }