예제 #1
0
        private static string GetKeyFromFile(string file, string beginString, string endString, Func <string, string, string, string> func, Encoding encoding = null)
        {
            ArgumentValidator.Validate(file);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            string text = File.ReadAllText(file, encoding);

            return(func(text, beginString, endString));
        }
예제 #2
0
        /// <summary>
        /// MD5 encrypt.
        /// </summary>
        /// <param name="data">The string of encrypt.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is <see cref="Encoding.UTF8"/>.</param>
        /// <returns>Encrypt byte array.</returns>
        private static byte[] EncryptBytesInternal(string data, Encoding encoding)
        {
            ArgumentValidator.Validate(data);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            using (MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider())
            {
                return(provider.ComputeHash(encoding.GetBytes(data)));
            }
        }
예제 #3
0
        /// <summary>
        /// Decrypts the specified cipher.
        /// </summary>
        /// <param name="cipher">The cipher.</param>
        /// <param name="privateKey">The private key.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns></returns>
        public string Decrypt(string cipher, string privateKey, Encoding encoding = null)
        {
            ArgumentValidator.Validate(cipher, privateKey);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            using (RSACryptoServiceProvider rsa = CreateRsaProviderFromPrivateKey(privateKey))
            {
                return(encoding.GetString(rsa.Decrypt(System.Convert.FromBase64String(cipher), false)));
            }
        }
예제 #4
0
        /// <summary>
        /// The method for encrypt or dncrypt the specified <paramref name="data"/> with <paramref name="password"/>, <paramref name="vector"/>, <paramref name="salt"/>.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="data">The string to be encrypted or decrypted,not null.</param>
        /// <param name="password">The password used to derive the key.</param>
        /// <param name="vector">The initialization vector (IV) to use to derive the key.</param>
        /// <param name="salt">The key salt to use to derive the key.The default is <paramref name="password"/>.</param>
        /// <param name="keySize">The size, in bits, of the secret key used by the symmetric algorithm.</param>
        /// <param name="blockSize">The block size, in bits, of the cryptographic operation.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is <see cref="Encoding.UTF8"/>.</param>
        /// <param name="iterations">The number of iterations for the operation.</param>
        /// <param name="mode">The mode for operation of the symmetric algorithm.</param>
        /// <param name="padding">The padding mode used in the symmetric algorithm. The default is System.Security.Cryptography.PaddingMode.PKCS7.</param>
        /// <returns>The array of <see cref="byte"/> value by <paramref name="type"/>. </returns>
        internal static byte[] EncryptOrDecryptBytes(EncryptionTypes type, string data, string password, string vector = null, string salt = null, int keySize = 128, int blockSize = 128, Encoding encoding = null, int iterations = 1000, CipherMode mode = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7)
        {
            ArgumentValidator.Validate(data, password);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            return(EncryptOrDecrypt(type,
                                    type == EncryptionTypes.Encrypt ? encoding.GetBytes(data) : Convert.FromBase64String(data),
                                    GetKeyBytes(password, salt, keySize, encoding, iterations),
                                    GetVectorBytes(vector, blockSize, encoding)
                                    , keySize, mode, padding));
        }
예제 #5
0
        /// <summary>
        /// SHA encryption.
        /// </summary>
        /// <typeparam name="T">The <see cref="T:System.Security.Cryptography.HashAlgorithm"/> sub-class.</typeparam>
        /// <param name="data">The string to be encrypted,not null.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is <see cref="Encoding.UTF8"/>.</param>
        /// <returns>The encrypted byte array.</returns>
        internal static byte[] EncryptBytesInternal <T>(string data, Encoding encoding = null) where T : HashAlgorithm, new()
        {
            ArgumentValidator.Validate(data);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            byte[] bytes = encoding.GetBytes(data);

            using (HashAlgorithm hash = new T())
            {
                return(hash.ComputeHash(bytes));
            }
        }
예제 #6
0
        /// <summary>
        /// Gets the key bytes.
        /// </summary>
        /// <param name="password">The password used to derive the key.</param>
        /// <param name="salt">The key salt used to derive the key.</param>
        /// <param name="size">The size, in bits, of the secret key used by the symmetric algorithm.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is <see cref="Encoding.UTF8"/>.</param>
        /// <param name="iterations">The number of iterations for the operation.</param>
        /// <returns></returns>
        internal static byte[] GetKeyBytes(string password, string salt = null, int size = 128, Encoding encoding = null, int iterations = 1000)
        {
            ArgumentValidator.Validate(password);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            int length = size / 8;

            byte[] buffer = salt == null?encoding.GetBytes(password) : encoding.GetBytes(salt);

            Rfc2898DeriveBytes derive = new Rfc2898DeriveBytes(password, buffer, iterations);

            return(derive.GetBytes(length));
        }
예제 #7
0
        /// <summary>
        /// HMAC encryption.
        /// </summary>
        /// <param name="data">The string to be encrypted,not null.</param>
        /// <param name="key">Encryption key,not null.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is <see cref="Encoding.UTF8"/>.</param>
        /// <returns>The encrypted string.</returns>
        private static byte[] EncryptBytesInternal <TKeyedHashAlgorithm>(string data, string key, Encoding encoding = null) where TKeyedHashAlgorithm : KeyedHashAlgorithm, new()
        {
            ArgumentValidator.Validate(data, key);

            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            byte[] keys  = encoding.GetBytes(key);
            byte[] datas = encoding.GetBytes(data);

            using (TKeyedHashAlgorithm hash = new TKeyedHashAlgorithm()
            {
                Key = keys
            })
            {
                return(hash.ComputeHash(datas));
            }
        }
예제 #8
0
        /// <summary>
        /// Imports the private key from string.
        /// </summary>
        /// <param name="text">The text for private key.</param>
        /// <param name="beginString">The begin string. Default is <c>-----BEGIN RSA PRIVATE KEY-----</c>.</param>
        /// <param name="endString">The end string. Default is <c>-----END RSA PRIVATE KEY-----</c>.</param>
        /// <returns></returns>
        public static string ImportPrivateKeyFromString(string text, string beginString = null, string endString = null)
        {
            ArgumentValidator.Validate(text);

            return(GetPrivateKeyString(text, beginString, endString));
        }