コード例 #1
0
        public static string GetCryptography(eTypeCryptography typeCryptography, string content, string key = null)
        {
            string cryptography = string.Empty;

            try
            {
                if (Validate(typeCryptography, content, key))
                {
                    switch (typeCryptography)
                    {
                    case eTypeCryptography.DES:
                        cryptography = DESCripto.ToCrypt(key, content);
                        break;

                    case eTypeCryptography.AES:
                        cryptography = AESCripto.ToCrypt(key, content);
                        break;

                    case eTypeCryptography.SHA256:
                        cryptography = SHA256.ToCrypt(content);
                        break;

                    case eTypeCryptography.MD5:
                        cryptography = MD5Cript.ToCrypt(content);
                        break;

                    case eTypeCryptography.ZenitPolar:
                        cryptography = Zenit_Polar.ToCript(content);
                        break;

                    case eTypeCryptography.RSA:
                        cryptography = RSACripto.Encrypt(content);
                        break;
                    }
                }

                return(cryptography);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cryptography = string.Empty;
            }
        }
コード例 #2
0
        private static bool Validate(eTypeCryptography typeCryptography, string content, string key)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(content))
                {
                    throw new Exception("Erro 01 - O conteúdo não foi informado.");
                }

                if (string.IsNullOrEmpty(key))
                {
                    switch (typeCryptography)
                    {
                    case eTypeCryptography.DES:
                    case eTypeCryptography.AES:
                        throw new Exception("Erro 02 - A chave de criptografia não foi informada.");
                    }
                }
                else
                {
                    switch (typeCryptography)
                    {
                    case eTypeCryptography.DES:
                        if (key.Length != 8)
                        {
                            throw new Exception("Erro 04 - A chave de criptografia não contém 8 caractéres.");
                        }
                        break;

                    case eTypeCryptography.AES:
                        if (key.Length != 16)
                        {
                            throw new Exception("Erro 04 - A chave de criptografia não contém 16 caractéres.");
                        }
                        break;
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
コード例 #3
0
        public static string GetDescryptography(eTypeCryptography typeCryptography, string content, string key = null)
        {
            string cryptography = string.Empty;

            try
            {
                if (Validate(typeCryptography, content, key))
                {
                    switch (typeCryptography)
                    {
                    case eTypeCryptography.DES:
                        cryptography = DESCripto.ToDescrypt(key, content);
                        break;

                    case eTypeCryptography.AES:
                        cryptography = AESCripto.ToDescrypt(key, content);
                        break;

                    case eTypeCryptography.SHA256:
                    case eTypeCryptography.MD5:
                        throw new Exception("Este algoritmo não permite descriptografia.");

                    case eTypeCryptography.ZenitPolar:
                        cryptography = Zenit_Polar.ToDescript(content);
                        break;

                    case eTypeCryptography.RSA:
                        cryptography = RSACripto.Decrypt(content);
                        break;
                    }
                }

                return(cryptography);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                cryptography = string.Empty;
            }
        }