GetNonZeroBytes() public method

public GetNonZeroBytes ( byte data ) : void
data byte
return void
示例#1
0
 public TokenManager()
 {
     sha1 = SHA1.Create();
     random = RandomNumberGenerator.Create();
     LastSecretGeneration = DateTime.MinValue; //in order to force the update
     secret = new byte[10];
     previousSecret = new byte[10];
     random.GetNonZeroBytes(secret);
     random.GetNonZeroBytes(previousSecret);
 }
示例#2
0
 public TokenManager()
 {
     sha1 = HashAlgoFactory.Create<SHA1>();
     random = new RNGCryptoServiceProvider ();
     LastSecretGeneration = DateTime.MinValue; //in order to force the update
     secret = new byte[10];
     previousSecret = new byte[10];
     random.GetNonZeroBytes(secret);
     random.GetNonZeroBytes(previousSecret);
 }
示例#3
0
 public static int GetRandom(int aFrom, int aTo)
 {
     System.Security.Cryptography.RandomNumberGenerator mRandom = System.Security.Cryptography.RandomNumberGenerator.Create();
     byte[] mByte = new byte[1];
     mRandom.GetNonZeroBytes(mByte);
     return((mByte[0] % ((aTo - aFrom) + 1)) + aFrom);
 }
        public override byte[] CreateKeyExchange(byte[] rgbData)
        {
#if MONO
            if (rgbData == null)
            {
                throw new ArgumentNullException("rgbData");
            }
#endif

            if (_rsaKey == null)
            {
                throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
            }

            byte[] rgbKeyEx;
            if (OverridesEncrypt)
            {
                rgbKeyEx = _rsaKey.Encrypt(rgbData, RSAEncryptionPadding.Pkcs1);
            }
            else
            {
                int cb = _rsaKey.KeySize / 8;
                if ((rgbData.Length + 11) > cb)
                {
                    throw new CryptographicException(Environment.GetResourceString("Cryptography_Padding_EncDataTooBig", cb - 11));
                }
                byte[] rgbInput = new byte[cb];

                //
                //  We want to pad to the following format:
                //      00 || 02 || PS || 00 || D
                //
                //      PS - pseudorandom non zero bytes
                //      D - data
                //

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

                Rng.GetNonZeroBytes(rgbInput);
                rgbInput[0] = 0;
                rgbInput[1] = 2;
                rgbInput[cb - rgbData.Length - 1] = 0;
                Buffer.InternalBlockCopy(rgbData, 0, rgbInput, cb - rgbData.Length, rgbData.Length);

                //
                //  Now encrypt the value and return it. (apply public key)
                //

                rgbKeyEx = _rsaKey.EncryptValue(rgbInput);
            }
            return(rgbKeyEx);
        }
示例#5
0
 /// <summary>
 /// Gets the next available random integer using a Pseudo<see cref="RandomNumberGenerator"/> to generate random bytes
 /// </summary>
 /// <returns></returns>
 public static int GetPrngInt()
 {
     byte[] buffer  = new byte[System.Runtime.InteropServices.Marshal.SizeOf(typeof(int))];
     byte[] buffer2 = new byte[buffer.Length];
     prng.GetNonZeroBytes(buffer);
     prng.GetBytes(buffer2);
     for (int i = 0; i < buffer.Length; i++)
     {
         buffer[i] ^= buffer2[(buffer.Length - 1) - i];
     }
     return(System.BitConverter.ToInt32(buffer, 0));
 }
        /// <summary>
        /// https://khalidabuhakmeh.com/creating-random-numbers-with-dotnet-core
        /// WARNING: .NET Core 3 slight bias: https://github.com/dotnet/corefx/pull/31243
        /// </summary>
        public static int Next(this RandomNumberGenerator generator, int min, int max)
        {
            // match Next of Random
            // where max is exclusive
            max -= 1;

            var bytes = new byte[sizeof(int)];             // 4 bytes

            generator.GetNonZeroBytes(bytes);
            var val = BitConverter.ToInt32(bytes, 0);
            // constrain our values to between our min and max
            // https://stackoverflow.com/a/3057867/86411
            var result = ((val - min) % (max - min + 1) + (max - min + 1)) % (max - min + 1) + min;

            return(result);
        }
示例#7
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);
        }
示例#8
0
        // Encrypt a value using the RSA public key and the PKCS1 encoding.
        internal byte[] EncryptPKCS1(byte[] rgb, RandomNumberGenerator rng)
        {
            // Check the data against null.
            if (rgb == null)
            {
                throw new ArgumentNullException("rgb");
            }

            // Make sure that we have sufficient RSA parameters.
            if (rsaParams.Modulus == null)
            {
                throw new CryptographicException
                          (_("Crypto_RSAParamsNotSet"));
            }

            // Format the type codes, padding string, and data.
            int k = rsaParams.Modulus.Length;

            if (rgb.Length > (k - 11))
            {
                throw new CryptographicException
                          (_("Crypto_RSAMessageTooLong"));
            }
            byte[] msg = new byte [k];
            msg[1] = (byte)0x02;
            byte[] padding = new byte [k - rgb.Length - 3];
            rng.GetNonZeroBytes(padding);
            Array.Copy(padding, 0, msg, 2, padding.Length);
            Array.Copy(rgb, 0, msg, k - rgb.Length, rgb.Length);

            // Encrypt the message.
            byte[] encrypted = ApplyPublic(msg);

            // Destroy sensitive data.
            Array.Clear(msg, 0, msg.Length);
            Array.Clear(padding, 0, padding.Length);

            // Done.
            return(encrypted);
        }
示例#9
0
 public override void GetNonZeroBytes(byte[] data) => _impl.GetNonZeroBytes(data);
示例#10
0
		// PKCS #1 v.2.1, Section 7.2.1
		// RSAES-PKCS1-V1_5-ENCRYPT ((n, e), M)
		public static byte[] Encrypt_v15 (RSA rsa, RandomNumberGenerator rng, byte[] M) 
		{
			int size = rsa.KeySize / 8;
			if (M.Length > size - 11)
				throw new CryptographicException ("message too long");
			int PSLength = System.Math.Max (8, (size - M.Length - 3));
			byte[] PS = new byte [PSLength];
			rng.GetNonZeroBytes (PS);
			byte[] EM = new byte [size];
			EM [1] = 0x02;
			Buffer.BlockCopy (PS, 0, EM, 2, PSLength);
			Buffer.BlockCopy (M, 0, EM, (size - M.Length), M.Length);
	
			byte[] m = OS2IP (EM);
			byte[] c = RSAEP (rsa, m);
			byte[] C = I2OSP (c, size);
			return C;
		}
 /// <inheritdoc/>
 public override void GetNonZeroBytes(byte[] data)
 {
     RandomSource.GetNonZeroBytes(data);
 }
	// Encrypt a value using the RSA public key and the PKCS1 encoding.
	internal byte[] EncryptPKCS1(byte[] rgb, RandomNumberGenerator rng)
			{
				// Check the data against null.
				if(rgb == null)
				{
					throw new ArgumentNullException("rgb");
				}

				// Make sure that we have sufficient RSA parameters.
				if(rsaParams.Modulus == null)
				{
					throw new CryptographicException
						(_("Crypto_RSAParamsNotSet"));
				}

				// Format the type codes, padding string, and data.
				int k = rsaParams.Modulus.Length;
				if(rgb.Length > (k - 11))
				{
					throw new CryptographicException
						(_("Crypto_RSAMessageTooLong"));
				}
				byte[] msg = new byte [k];
				msg[1] = (byte)0x02;
				byte[] padding = new byte [k - rgb.Length - 3];
				rng.GetNonZeroBytes(padding);
				Array.Copy(padding, 0, msg, 2, padding.Length);
				Array.Copy(rgb, 0, msg, k - rgb.Length, rgb.Length);

				// Encrypt the message.
				byte[] encrypted = ApplyPublic(msg);

				// Destroy sensitive data.
				Array.Clear(msg, 0, msg.Length);
				Array.Clear(padding, 0, padding.Length);

				// Done.
				return encrypted;
			}