Пример #1
0
        /// <summary>
        /// Gera uma chave unica utilizando o RNGCryptoServiceProvider
        /// </summary>
        public static string GetUniqueKey(int size, bool onlyUpper, bool removeZero)
        {
            //Remover o Zero 0 e o "O"
            string letters;

            if (onlyUpper && removeZero)
            {
                letters = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
            }
            else if (!onlyUpper && removeZero)
            {
                letters = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz";
            }
            else if (onlyUpper)
            {
                letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            }
            else
            {
                letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            }

            char[] chars = letters.ToCharArray();
            byte[] data  = new byte[size];
            System.Text.StringBuilder result = new System.Text.StringBuilder(size);
            System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length - 1)]);
            }

            return(result.ToString().ToUpper());
        }
Пример #2
0
        public static string GetHash(string plainText, byte[] saltBytes = null)
        {
            if (saltBytes == null)
            {
                int minSaltSize = 4;
                int maxSaltSize = 8;

                // determina el tamaño de la sal
                int saltSize = random.Next(minSaltSize, maxSaltSize);
                saltBytes = new byte[saltSize];

                // Utiliza un generador seguro de números aleatorios
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();

                // Obtiene la sal con el generador
                rng.GetNonZeroBytes(saltBytes);
            }

            // Convierte la contraseña a arreglo de bytes
            byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);

            // Arreglo para guardar la contraseña y la sal
            byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];

            // Almacena la contraseña
            for (int i = 0; i < plainTextBytes.Length; i++)
            {
                plainTextWithSaltBytes[i] = plainTextBytes[i];
            }

            // Añade la sal
            for (int i = 0; i < saltBytes.Length; i++)
            {
                plainTextWithSaltBytes[plainTextBytes.Length + i] = saltBytes[i];
            }

            // Especifica el algoritmo SHA512
            System.Security.Cryptography.HashAlgorithm hash = new System.Security.Cryptography.SHA512Managed();

            // Calcula el hash
            byte[] hashBytes = hash.ComputeHash(plainTextWithSaltBytes);

            // Arreglo para almacenar el hash y la sal
            byte[] hashWithSaltBytes = new byte[hashBytes.Length + saltBytes.Length];

            // Copia el hash en el arreglo
            for (int i = 0; i < hashBytes.Length; i++)
            {
                hashWithSaltBytes[i] = hashBytes[i];
            }

            // Añade la sal
            for (int i = 0; i < saltBytes.Length; i++)
            {
                hashWithSaltBytes[hashBytes.Length + i] = saltBytes[i];
            }

            // Convierte el resultado a cadena y lo devuelve
            return(Convert.ToBase64String(hashWithSaltBytes));
        }
        /// <summary>
        /// Gera um SALT.
        /// </summary>
        /// <returns>SALT.</returns>
        private byte[] GenerateSalt()
        {
            System.Security.Cryptography.RNGCryptoServiceProvider v_randomnumbergenerator;
            byte[] v_salt;
            int    v_saltlength;

            if (this.v_minsaltlength == this.v_maxsaltlength)
            {
                v_saltlength = this.v_minsaltlength;
            }
            else
            {
                v_saltlength = this.GenerateRandomNumber(this.v_minsaltlength, this.v_maxsaltlength);
            }

            v_salt = new byte[v_saltlength];

            v_randomnumbergenerator = new System.Security.Cryptography.RNGCryptoServiceProvider();
            v_randomnumbergenerator.GetNonZeroBytes(v_salt);

            v_salt[0] = (byte)((v_salt[0] & 0xfc) | (v_saltlength & 0x03));
            v_salt[1] = (byte)((v_salt[1] & 0xf3) | (v_saltlength & 0x0c));
            v_salt[2] = (byte)((v_salt[2] & 0xcf) | (v_saltlength & 0x30));
            v_salt[3] = (byte)((v_salt[3] & 0x3f) | (v_saltlength & 0xc0));

            return(v_salt);
        }
Пример #4
0
		/// <summary>
		/// You are using ZContext.Current!
		/// </summary>
		public ZActor(ZAction0 action, params object[] args)
			: this(default(string), action, args)
		{
			var rnd0 = new byte[8];
			using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetNonZeroBytes(rnd0);
			this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
		}
Пример #5
0
        // coded by Mitil. 2006/01/04
        public MersenneTwister()
        {
            MT();

            // auto generate seed for .NET
            UInt32[] seed_key = new UInt32[6];
            Byte[]   rnseed   = new Byte[8];

            seed_key[0] = (UInt32)System.DateTime.Now.Millisecond;
            seed_key[1] = (UInt32)System.DateTime.Now.Second;
            seed_key[2] = (UInt32)System.DateTime.Now.DayOfYear;
            seed_key[3] = (UInt32)System.DateTime.Now.Year;
            ;
            System.Security.Cryptography.RandomNumberGenerator rn
                = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rn.GetNonZeroBytes(rnseed);

            seed_key[4] = ((UInt32)rnseed[0] << 24) | ((UInt32)rnseed[1] << 16)
                          | ((UInt32)rnseed[2] << 8) | ((UInt32)rnseed[3]);
            seed_key[5] = ((UInt32)rnseed[4] << 24) | ((UInt32)rnseed[5] << 16)
                          | ((UInt32)rnseed[6] << 8) | ((UInt32)rnseed[7]);

            init_by_array(seed_key);

            rn       = null;
            seed_key = null;
            rnseed   = null;
        }
Пример #6
0
        private static byte[] GenerateRandomBytes(int count)
        {
            var b = new byte[count];
            var c = new System.Security.Cryptography.RNGCryptoServiceProvider();

            c.GetNonZeroBytes(b);
            return(b);
        }
 public async Task<IHttpActionResult> GetRandomPrime()
 {
     var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
     var bytes = new byte[16];
     var result = await Task.Run(() =>
     {
         rnd.GetNonZeroBytes(bytes);
         var res = new BigInteger(bytes);
         while (!res.IsProbablePrime(10))
         {
             rnd.GetNonZeroBytes(bytes);
             res = new BigInteger(bytes);
         }
         return res;
     });
     Debug.WriteLine(result);
     return Json(result.ToString());
 }
Пример #8
0
        /// <summary>
        /// You are using ZContext.Current!
        /// </summary>
        public ZActor(ZAction0 action, params object[] args)
            : this(default(string), action, args)
        {
            var rnd0 = new byte[8];
            var rng  = new System.Security.Cryptography.RNGCryptoServiceProvider();

            rng.GetNonZeroBytes(rnd0);
            this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
        }
Пример #9
0
 public static string GetUniqueKey(int maxSize)
 {
     char[] chars = new char[62];
     chars =
     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
     byte[] data = new byte[1];
     using (System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider())
     {
         crypto.GetNonZeroBytes(data);
         data = new byte[maxSize];
         crypto.GetNonZeroBytes(data);
     }
     StringBuilder result = new StringBuilder(maxSize);
     foreach (byte b in data)
     {
         result.Append(chars[b % (chars.Length)]);
     }
     return result.ToString();
 }
Пример #10
0
        public static string GetRandomString(int stringlength)
        {
            //https://stackoverflow.com/questions/1344221/how-can-i-generate-random-alphanumeric-strings-in-c
            char[] chars = new char[62];
            chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
            using (System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider())
            {
                crypto.GetNonZeroBytes(data);
                data = new byte[stringlength];
                crypto.GetNonZeroBytes(data);
            }
            StringBuilder result = new StringBuilder(stringlength);

            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return(result.ToString());
        }
Пример #11
0
        public static string GetCode(int size, bool includeLetters)
        {
            char[] chars = new char[62];
            string a;

            a     = includeLetters ? "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" : "1234567890";
            chars = a.ToCharArray();
            byte[] data = new byte[1];
            System.Security.Cryptography.RNGCryptoServiceProvider crypto = new System.Security.Cryptography.RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);
            data = new byte[size];
            crypto.GetNonZeroBytes(data);
            StringBuilder result = new StringBuilder(size);

            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return(result.ToString());
        }
Пример #12
0
        /// <summary>
        /// Gets random string
        /// </summary>
        public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
            using (System.Security.Cryptography.RNGCryptoServiceProvider crypto =
                       new System.Security.Cryptography.RNGCryptoServiceProvider())
            {
                crypto.GetNonZeroBytes(data);
                data = new byte[maxSize];
                crypto.GetNonZeroBytes(data);
            }
            StringBuilder result = new StringBuilder(maxSize);

            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return(result.ToString());
        }
        public static string GenerateUniqueKey(int keyLen)
        {
            string a = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";

            char[] aChar = a.ToCharArray();
            byte[] data  = new byte[keyLen - 1];

            System.Security.Cryptography.RNGCryptoServiceProvider theCrypt = new System.Security.Cryptography.RNGCryptoServiceProvider();
            theCrypt.GetNonZeroBytes(data);
            theCrypt.GetNonZeroBytes(data);

            StringBuilder keyRes = new StringBuilder(keyLen - 1);

            foreach (byte b in data)
            {
                keyRes.Append(aChar[b % (aChar.ToString().Length - 1)]);
            }


            return(keyRes.ToString());
        }
Пример #14
0
        /// <summary>
        /// Начинать слушать порт, на который приходят RTCP пакеты
        /// и слать в ответ свои
        /// </summary>
        public void StartReporting()
        {
            var random = new System.Security.Cryptography.RNGCryptoServiceProvider();

            byte[] randomBytes = new byte[sizeof(UInt32)];
            random.GetNonZeroBytes(randomBytes);

            recieverSSRC        = BitConverter.ToUInt32(randomBytes, 0);
            senderReportCount   = 0;
            receivedRtpPackets  = 0;
            recieverReportCount = 0;
            initTimestamp       = 0;

            udpClient.StartReceiving();
        }
Пример #15
0
 public Usuario(string username, string email, string nombre, string apellidos)
 {
     Username    = username ?? throw new ArgumentNullException(nameof(username));
     Email       = email ?? throw new ArgumentNullException(nameof(email));
     Nombre      = nombre ?? throw new ArgumentNullException(nameof(nombre));
     Apellidos   = apellidos ?? throw new ArgumentNullException(nameof(apellidos));
     Admin       = false;
     Saldo       = 0.0;
     Facturas    = new List <Factura>();
     CarroCompra = new Carro();
     byte[] generatedSalt = new byte[64];
     using (var random = new System.Security.Cryptography.RNGCryptoServiceProvider())
     {
         random.GetNonZeroBytes(generatedSalt);
     }
     Salt          = generatedSalt;
     FechaRegistro = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
 }
Пример #16
0
        /// <summary>
        /// Gets a random hexdecimal value as string.
        /// </summary>
        /// <returns></returns>
        public static string RandomIdGenerator()
        {
            int i;

            using (var randomNumber = new System.Security.Cryptography.RNGCryptoServiceProvider())
            {
                var randomId = new byte[8];
                randomNumber.GetNonZeroBytes(randomId);
                // If the system architecture is little-endian (that is, little end first),
                // reverse the byte array.
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(randomId);
                }

                i = BitConverter.ToInt32(randomId, 0);
            }

            return(i.ToString("X"));
        }
Пример #17
0
        public static string GetEncryptedPWD(string pwd, ref string Salt)
        {
            StringBuilder sBuilder;

            if (string.IsNullOrEmpty(Salt))
            {
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
                byte[] saltByte = new byte[8];
                rng.GetNonZeroBytes(saltByte);

                sBuilder = new StringBuilder();

                // Loop through each byte of the hashed data
                // and format each one as a hexadecimal string.
                for (int i = 0; i < saltByte.Length; i++)
                {
                    sBuilder.Append(saltByte[i].ToString("x2"));
                }

                Salt = sBuilder.ToString();
            }

            System.Security.Cryptography.SHA256CryptoServiceProvider sha = new System.Security.Cryptography.SHA256CryptoServiceProvider();
            //System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.ASCII.GetBytes(pwd + Salt);
            data = sha.ComputeHash(data);

            sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return(sBuilder.ToString());
        }
        public static string GenerateRandomContent(int bitLen)
        {
            if (bitLen <= 8)
            {
                bitLen = 128;
            }
            if (bitLen % 8 != 0)
            {
                throw new ArgumentException("bitLen must be a multiple of 8");
            }
            int bytesNeeded = Convert.ToInt32(bitLen / 8) - 1;

            byte[] buff = new byte[bytesNeeded + 1];
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            rng.GetNonZeroBytes(buff);
            StringBuilder sb = new StringBuilder(bytesNeeded * 2);
            int           i  = 0;

            for (i = 0; i <= buff.Length - 1; i++)
            {
                sb.AppendFormat("{0:X2}", buff[i]);
            }
            return(sb.ToString());
        }
Пример #19
0
        /// <summary>
        /// You are using ZContext.Current!
        /// </summary>
        public ZActor(ZAction0 action, params object[] args)
            : this(default(string), action, args)
        {
            var rnd0 = new byte[8];

            using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetNonZeroBytes(rnd0);
            Endpoint = $"inproc://{ZContext.Encoding.GetString(rnd0)}";
        }
Пример #20
0
 public static uint GetRandomNumber()
 {
     byte[] bytes = new byte[sizeof(UInt32)];
     provider.GetNonZeroBytes(bytes);
     return(BitConverter.ToUInt32(bytes, 0));
 }
Пример #21
0
        /// <summary>
        /// Начинать слушать порт, на который приходят RTCP пакеты
        /// и слать в ответ свои
        /// </summary>
        public void StartReporting()
        {
            var random = new System.Security.Cryptography.RNGCryptoServiceProvider();

            byte[] randomBytes = new byte[sizeof(UInt32)];
            random.GetNonZeroBytes(randomBytes);

            recieverSSRC = BitConverter.ToUInt32(randomBytes, 0);
            senderReportCount = 0;
            receivedRtpPackets = 0;
            recieverReportCount = 0;
            initTimestamp = 0;

            udpClient.StartReceiving();
        }
Пример #22
0
        /// <summary>
        /// Gera um SALT.
        /// </summary>
        /// <returns>SALT.</returns>
        private byte[] GenerateSalt()
        {
            System.Security.Cryptography.RNGCryptoServiceProvider v_randomnumbergenerator;
            byte[] v_salt;
            int v_saltlength;

            if (this.v_minsaltlength == this.v_maxsaltlength)
                v_saltlength = this.v_minsaltlength;
            else
                v_saltlength = this.GenerateRandomNumber(this.v_minsaltlength, this.v_maxsaltlength);

            v_salt = new byte[v_saltlength];

            v_randomnumbergenerator = new System.Security.Cryptography.RNGCryptoServiceProvider();
            v_randomnumbergenerator.GetNonZeroBytes(v_salt);

            v_salt[0] = (byte)((v_salt[0] & 0xfc) | (v_saltlength & 0x03));
            v_salt[1] = (byte)((v_salt[1] & 0xf3) | (v_saltlength & 0x0c));
            v_salt[2] = (byte)((v_salt[2] & 0xcf) | (v_saltlength & 0x30));
            v_salt[3] = (byte)((v_salt[3] & 0x3f) | (v_saltlength & 0xc0));

            return v_salt;
        }