Create() public static method

public static Create ( ) : RandomNumberGenerator
return RandomNumberGenerator
コード例 #1
0
        public string GenerateSalt()
        {
            byte[] saltBytes = new byte[128 / 8];
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(saltBytes);
            }
            string salt = Convert.ToBase64String(saltBytes);

            return(salt);
        }
コード例 #2
0
        private void btnselnumberchars_Click(object sender, EventArgs e)
        {
            R randgen = R.Create();

            byte[] rand = new byte[10];

            randgen.GetBytes(rand);
            // Random Number between 0 - 255 --- we need between 0 - chars2generate
            int startselection = (int)Math.Floor(rand[2] * (rtbseedgen.TextLength - cbbNumChars.SelectedIndex + 1) / 255.0);

            if (startselection < 0)
            {
                startselection = 0;
            }
            rtbseedgen.Select(startselection, cbbNumChars.SelectedIndex + 1);
            rtbseedgen.Focus();
        }
コード例 #3
0
        /// <summary>
        /// Produces 'Count' Random Characters from Character List
        /// </summary>
        /// <param name="count">Number of Chars to Produce</param>
        /// <param name="from_list">Char Array that holds possible Chars</param>
        /// <returns>String with Count Random Characters</returns>
        private string produce_random(int number = -1)
        {
            int count = 0;

            if (number == -1)
            {
                count = cbbNumChars.SelectedIndex + 1;
            }
            else
            {
                count = number;
            }

            char[] from_list = get_char_set().ToCharArray();

            if (from_list.Length == 0)
            {
                return("");
            }

            R randgen = R.Create();

            byte[] rand = new byte[5000];

            string ret = "";

            while (ret.Length < count)
            {
                randgen.GetNonZeroBytes(rand);

                foreach (byte x in rand)
                {
                    if (from_list.Contains((char)x))
                    {
                        ret += (char)x;
                    }
                    if (ret.Length >= count)
                    {
                        return(ret);
                    }
                }
            }
            return(ret);
        }
コード例 #4
0
        /// <include file='doc\RSAOAEPKeyExchangeFormatter.uex' path='docs/doc[@for="RSAOAEPKeyExchangeFormatter.CreateKeyExchange"]/*' />
        public override byte[] CreateKeyExchange(byte[] rgbData)
        {
            byte[] rgbKeyEx;

            if (_rsaKey is RSACryptoServiceProvider)
            {
                rgbKeyEx = ((RSACryptoServiceProvider)_rsaKey).Encrypt(rgbData, true);
            }
            else
            {
                int                  cb = _rsaKey.KeySize / 8;
                int                  cbHash;
                HashAlgorithm        hash;
                int                  i;
                MaskGenerationMethod mgf;
                byte[]               rgbDB;
                byte[]               rgbIn;
                byte[]               rgbMask;
                byte[]               rgbSeed;

                //  Create the OAEP padding object.

                //  1.  Hash the parameters to get rgbPHash

                hash   = (HashAlgorithm)CryptoConfig.CreateFromName("SHA1"); // Create the default SHA1 object
                cbHash = hash.HashSize / 8;
                if ((rgbData.Length + 2 + 2 * cbHash) > cb)
                {
                    throw new CryptographicException(String.Format(Environment.GetResourceString("Cryptography_Padding_EncDataTooBig"), cb - 2 - 2 * cbHash));
                }
                hash.ComputeHash(new byte[0]); // Use an empty octet string

                //  2.  Create DB object

                rgbDB = new byte[cb - cbHash - 1];

                //  Structure is as follows:
                //      pHash || PS || 01 || M
                //      PS consists of all zeros

                Buffer.InternalBlockCopy(hash.Hash, 0, rgbDB, 0, cbHash);
                rgbDB[rgbDB.Length - rgbData.Length - 1] = 1;
                Buffer.InternalBlockCopy(rgbData, 0, rgbDB, rgbDB.Length - rgbData.Length, rgbData.Length);

                // 3. Create a random value of size hLen

                if (RngValue == null)
                {
                    RngValue = RandomNumberGenerator.Create();
                }

                rgbSeed = new byte[cbHash];
                RngValue.GetBytes(rgbSeed);

                // 4.  Compute the mask value

                mgf = new PKCS1MaskGenerationMethod();

                rgbMask = mgf.GenerateMask(rgbSeed, rgbDB.Length);

                // 5.  Xor rgbMaskDB into rgbDB

                for (i = 0; i < rgbDB.Length; i++)
                {
                    rgbDB[i] = (byte)(rgbDB[i] ^ rgbMask[i]);
                }

                // 6.  Compute seed mask value

                rgbMask = mgf.GenerateMask(rgbDB, cbHash);

                // 7.  Xor rgbMask into rgbSeed

                for (i = 0; i < rgbSeed.Length; i++)
                {
                    rgbSeed[i] ^= rgbMask[i];
                }

                // 8. Concatenate rgbSeed and rgbDB to form value to encrypt

                rgbIn = new byte[cb];
                Buffer.InternalBlockCopy(rgbSeed, 0, rgbIn, 1, rgbSeed.Length);
                Buffer.InternalBlockCopy(rgbDB, 0, rgbIn, rgbSeed.Length + 1, rgbDB.Length);

                // 9.  Now Encrypt it

                rgbKeyEx = _rsaKey.EncryptValue(rgbIn);
            }
            return(rgbKeyEx);
        }
コード例 #5
0
 public static string RandomString(int size)
 {
     byte[] randBuffer = new byte[size + (10)];
     RandomNumberGenerator.Create().GetBytes(randBuffer);
     return(System.Convert.ToBase64String(randBuffer).Replace("/", string.Empty).Replace("+", string.Empty).Replace("=", string.Empty).Remove(size));
 }