Пример #1
0
        // Validada com PEX : 05/02/2014
        public static bool ValidarCNPJ(string cnpj)
        {
            #region Pex

            if (string.IsNullOrEmpty(cnpj))
            {
                return(false);
            }

            // apos remover os caracteres invalidos, verifica se a nova string ainda tem mais que 11 caracteres
            cnpj = HelperText.ExtractNumbers(cnpj);
            if (cnpj.Length != 14)
            {
                return(false);
            }

            #endregion Pex

            string s  = cnpj;
            string c  = s.Substring(0, 12);
            string dv = s.Substring(12, 2);

            int d1 = 0;
            for (int i = 0; i < 12; i++)
            {
                d1 += Int32.Parse(c.Substring(11 - i, 1)) * (2 + (i % 8));
            }
            if (0 == d1)
            {
                return(false);
            }

            d1 = 11 - (d1 % 11);
            if (d1 > 9)
            {
                d1 = 0;
            }
            if (Int32.Parse(dv.Substring(0, 1)) != d1)
            {
                return(false);
            }
            d1 *= 2;
            for (int i = 0; i < 12; i++)
            {
                d1 += Int32.Parse(c.Substring(11 - i, 1)) * (2 + ((i + 1) % 8));
            }
            d1 = 11 - (d1 % 11);
            if (d1 > 9)
            {
                d1 = 0;
            }
            if (Int32.Parse(dv.Substring(1, 1)) != d1)
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
        /// <summary>
        /// Caso a string não possa ser descriptografada uma exceção será gerada.
        /// </summary>
        /// <param name="encryptedText">Texto criptografado usando Encrypt</param>
        /// <param name="SaltKey">Chave pública de criptografia</param>
        /// <returns>string</returns>
        public static string Decrypt(string encryptedText, string SaltKey)
        {
            #region .: PEX Validation :.

            if (HelperText.IsNullOrEmptyOrWhiteSpace(encryptedText))
            {
                return(string.Empty);
            }

            if (HelperText.IsNullOrEmptyOrWhiteSpace(SaltKey))
            {
                return(string.Empty);
            }

            if (SaltKey.Length < 8)
            {
                return(string.Empty);
            }

            #endregion

            byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
            byte[] keyBytes        = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var    symmetricKey    = new RijndaelManaged()
            {
                Mode = CipherMode.CBC, Padding = PaddingMode.None
            };

            var    decryptor          = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
            int    decryptedByteCount = 0;
            byte[] plainTextBytes     = null;

            using (var memoryStream = new MemoryStream(cipherTextBytes))
            {
                var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
                plainTextBytes = new byte[cipherTextBytes.Length];

                decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            }

            return(Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray()));
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="plainText">Textoa ser criptografado</param>
        /// <param name="SaltKey">Chave pública de criptografia</param>
        /// <returns>string</returns>
        public static string Encrypt(string plainText, string SaltKey)
        {
            #region .: PEX Validation :.

            if (HelperText.IsNullOrEmptyOrWhiteSpace(plainText))
            {
                return(string.Empty);
            }

            if (HelperText.IsNullOrEmptyOrWhiteSpace(SaltKey))
            {
                return(string.Empty);
            }

            if (SaltKey.Length < 8)
            {
                return(string.Empty);
            }

            #endregion

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            byte[] cipherTextBytes;
            byte[] keyBytes     = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var    symmetricKey = new RijndaelManaged()
            {
                Mode = CipherMode.CBC, Padding = PaddingMode.Zeros
            };
            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

            using (var memoryStream = new MemoryStream())
            {
                var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                cipherTextBytes = memoryStream.ToArray();
            }

            return(Convert.ToBase64String(cipherTextBytes));
        }
Пример #4
0
        // Validada com PEX : 05/02/2014
        public static bool ValidarCPF(string cpf)
        {
            #region Pex

            if (string.IsNullOrEmpty(cpf))
            {
                return(false);
            }

            // verifica se a string tem mais que 11 caracteres
            cpf = cpf.Replace(".", "").Replace("-", "").Replace("_", "");
            if (cpf.Length != 11)
            {
                return(false);
            }

            // apos remover os caracteres invalidos, verifica se a nova string ainda tem mais que 11 caracteres
            cpf = HelperText.ExtractNumbers(cpf);
            if (cpf.Length != 11)
            {
                return(false);
            }

            #endregion Pex

            string s  = cpf;
            string c  = s.Substring(0, 9);
            string dv = s.Substring(9, 2);
            int    d1 = 0;
            for (int i = 0; i < 9; i++)
            {
                d1 += Int32.Parse(c.Substring(i, 1)) * (10 - i);
            }

            if (d1 == 0)
            {
                return(false);
            }
            d1 = 11 - (d1 % 11);
            if (d1 > 9)
            {
                d1 = 0;
            }
            if (Int32.Parse(dv.Substring(0, 1)) != d1)
            {
                return(false);
            }
            d1 *= 2;
            for (int i = 0; i < 9; i++)
            {
                d1 += Int32.Parse(c.Substring(i, 1)) * (11 - i);
            }
            d1 = 11 - (d1 % 11);
            if (d1 > 9)
            {
                d1 = 0;
            }
            if (Int32.Parse(dv.Substring(1, 1)) != d1)
            {
                return(false);
            }

            return(true);
        }