예제 #1
0
        /// <summary>
        /// Decrypt an encrypted text.
        /// </summary>
        /// <param name="encryptedText">The encrypted text.</param>
        /// <param name="options">The decryption options.</param>
        /// <returns>Returns a decrypted text.</returns>
        public static string Decrypt(string encryptedText, IDictionary <string, object> options)
        {
            var toDecrypt     = encryptedText;
            var decryptedText = string.Empty;

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var decodeFromBase64 = options.ContainsKey("DecodeFromBase64") &&
                                   Convert.ToBoolean(options["DecodeFromBase64"], CultureInfo.InvariantCulture);
            var encodeToBase64 = options.ContainsKey("EncodeToBase64") && Convert.ToBoolean(options["EncodeToBase64"], CultureInfo.InvariantCulture);

            var method = options.ContainsKey("Method") ? options["Method"].ToString() : string.Empty;

            switch (method)
            {
            // RSA
            case "RSA":
                var privateKeyPath = options["PrivateKeyPath"].ToString();

                if (privateKeyPath.StartsWith("\"", StringComparison.InvariantCulture))
                {
                    privateKeyPath = privateKeyPath.Substring(1, privateKeyPath.Length - 1);
                }

                if (privateKeyPath.EndsWith("\"", StringComparison.InvariantCulture))
                {
                    privateKeyPath = privateKeyPath.Substring(0, privateKeyPath.Length - 1);
                }

                decryptedText = decodeFromBase64 ? RsaUtils.Base64DecodeAndDecrypt(toDecrypt, privateKeyPath) : RsaUtils.Decrypt(toDecrypt, privateKeyPath);
                break;

            // AES
            case "AES":
                var key = options["key"].ToString();
                var initializationValue = options["iniValue"].ToString();

                decryptedText = decodeFromBase64 ? AesUtils.Base64DecodeAndDecrypt(toDecrypt, key, initializationValue) : AesUtils.Decrypt(toDecrypt, key, initializationValue);
                break;

            // Base64
            case "Base64":
                decryptedText = GeneralUtils.Base64Decode(toDecrypt);
                break;
            }

            return(encodeToBase64 ? GeneralUtils.Base64Encode(decryptedText) : decryptedText);
        }
예제 #2
0
        public static string Encrypt(string plainText, IDictionary <string, object> options)
        {
            var toEncrypt     = plainText;
            var encryptedText = string.Empty;

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var decodeFromBase64 = options.ContainsKey("DecodeFromBase64") &&
                                   Convert.ToBoolean(options["DecodeFromBase64"], CultureInfo.InvariantCulture);
            var encodeToBase64 = options.ContainsKey("EncodeToBase64") && Convert.ToBoolean(options["EncodeToBase64"], CultureInfo.InvariantCulture);

            var method = options.ContainsKey("Method") ? options["Method"].ToString() : string.Empty;

            switch (method)
            {
            // RSA
            case "RSA":
                var publicKeyPath = options["PublicKeyPath"].ToString();

                if (publicKeyPath.StartsWith("\"", StringComparison.InvariantCulture))
                {
                    publicKeyPath = publicKeyPath.Substring(1, publicKeyPath.Length - 1);
                }

                if (publicKeyPath.EndsWith("\"", StringComparison.InvariantCulture))
                {
                    publicKeyPath = publicKeyPath.Substring(0, publicKeyPath.Length - 1);
                }

                if (decodeFromBase64)
                {
                    toEncrypt = GeneralUtils.Base64Decode(toEncrypt);
                }

                encryptedText = encodeToBase64 ? RsaUtils.EncryptAndBase64Encode(toEncrypt, publicKeyPath) : RsaUtils.Encrypt(toEncrypt, publicKeyPath);
                break;

            // AES
            case "AES":
                var key = options["key"].ToString();
                var initializationValue = options["iniValue"].ToString();

                encryptedText = encodeToBase64 ? AesUtils.EncryptAndBase64Encode(toEncrypt, key, initializationValue) : AesUtils.Encrypt(toEncrypt, key, initializationValue);
                break;

            // Base64
            case "Base64":
                encryptedText = GeneralUtils.Base64Encode(toEncrypt);
                break;

            // Hashing
            case "Hashing":
                switch (options["hashMethod"].ToString())
                {
                case "MD5":
                    // create new instance of MD5
                    var md5 = MD5.Create();

                    try
                    {
                        // convert the input text to array of bytes
                        var hashDataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(toEncrypt));

                        // return hexadecimal string
                        encryptedText = BitConverter.ToString(hashDataMd5).Replace("-", string.Empty);
                    }
                    finally
                    {
                        md5.Dispose();
                    }

                    break;

                case "SHA1":
                    // create new instance of SHA1
                    var sha1 = SHA1.Create();

                    try
                    {
                        // convert the input text to array of bytes
                        var hashDataSha1 = sha1.ComputeHash(Encoding.Default.GetBytes(toEncrypt));

                        // return hexadecimal string
                        encryptedText = BitConverter.ToString(hashDataSha1).Replace("-", string.Empty);
                    }
                    finally
                    {
                        sha1.Dispose();
                    }

                    break;

                case "SHA2":
                case "SHA256":
                    // create new instance of SHA256
                    var sha2 = SHA256.Create();

                    try
                    {
                        // convert the input text to array of bytes
                        var hashDataSha2 = sha2.ComputeHash(Encoding.Default.GetBytes(toEncrypt));

                        // return hexadecimal string
                        encryptedText = BitConverter.ToString(hashDataSha2).Replace("-", string.Empty);
                    }
                    finally
                    {
                        sha2.Dispose();
                    }

                    break;
                }

                break;
            }

            return(encryptedText);
        }