当前位置: 首页>>代码示例>>C#>>正文


C# System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes方法代码示例

本文整理汇总了C#中System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes方法的典型用法代码示例。如果您正苦于以下问题:C# System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes方法的具体用法?C# System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes怎么用?C# System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Security.Cryptography.RNGCryptoServiceProvider的用法示例。


在下文中一共展示了System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GeneratePassword

        public static string GeneratePassword(int intLength, int intNumberOfNonAlphaNumericCharacters)
        {
            int intNonAlphaCount = 0;
            byte[] buffer = new byte[intLength];
            char[] aryPassword = new char[intLength];
            char[] aryPunctuations = "[email protected]@$%$%*()@-+=[{]}#%>|%#?".ToCharArray();

            System.Security.Cryptography.RNGCryptoServiceProvider objCrypto = new System.Security.Cryptography.RNGCryptoServiceProvider();
            objCrypto.GetBytes(buffer);

            for (int i = 0; i < intLength; i++)
            {
                //Convert each byte into its representative character
                int intRndChr = (buffer[i] % 87);
                if (intRndChr < 10)
                    aryPassword[i] = Convert.ToChar(Convert.ToUInt16(48 + intRndChr));
                else if (intRndChr < 36)
                    aryPassword[i] = Convert.ToChar(Convert.ToUInt16((65 + intRndChr) - 10));
                else if (intRndChr < 62)
                    aryPassword[i] = Convert.ToChar(Convert.ToUInt16((97 + intRndChr) - 36));
                else
                {
                    if (intNonAlphaCount >= intNumberOfNonAlphaNumericCharacters)
                    {
                        i--;
                        objCrypto.GetBytes(buffer);
                    }
                    else
                    {
                        aryPassword[i] = aryPunctuations[intRndChr - 62];
                        intNonAlphaCount++;
                    }
                }
            }

            if (intNonAlphaCount < intNumberOfNonAlphaNumericCharacters)
            {
                Random objRandom = new Random();
                for (int i = 0; i < (intNumberOfNonAlphaNumericCharacters - intNonAlphaCount); i++)
                {
                    int intIndex;
                    do
                    {
                        intIndex = objRandom.Next(0, intLength);
                    } while (!Char.IsLetterOrDigit(aryPassword[intIndex]));
                    aryPassword[intIndex] = aryPunctuations[objRandom.Next(0, aryPunctuations.Length)];
                }
            }

            Array.Reverse(aryPassword);
            return new string(aryPassword);
        }
开发者ID:AgentTy,项目名称:General,代码行数:52,代码来源:Password.cs

示例2: Challenge

 public Challenge(string CBCryptHostId)
 {
     this.CBCryptHostId = CBCryptHostId;
     this.ChallengeBytes = new byte[ChallengeSize];
     // The easiest way for me to generate an ephemeral key is to feed random bytes into CBCrypt.GenerateKeyPair
     var tempKey = new byte[tempKeySize];
     using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) {
         rng.GetBytes(this.ChallengeBytes);
         rng.GetBytes(tempKey);
     }
     this.ServerEphemeralKey = new CBCryptKey(tempKey);
     this.ServerPublicKeyDerEncoded = this.ServerEphemeralKey.GetPublicKeyDerEncoded();
 }
开发者ID:RoPe93,项目名称:CBcrypt,代码行数:13,代码来源:Challenge.cs

示例3: GenerateNewSalt

 public static string GenerateNewSalt()
 {
     byte[] saltInBytes = new byte[16];
        System.Security.Cryptography.RNGCryptoServiceProvider saltGenerator = new System.Security.Cryptography.RNGCryptoServiceProvider();
        saltGenerator.GetBytes(saltInBytes);
        return Convert.ToBase64String(saltInBytes);
 }
开发者ID:grrizzly,项目名称:3D-Repository,代码行数:7,代码来源:Utility.cs

示例4: CreateSalt

 /// <summary>
 /// Create string salt
 /// </summary>
 /// <param name="grv">Create string salt</param>
 public static string CreateSalt()
 {
     byte[] bytSalt = new byte[9];
     System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
     rng.GetBytes(bytSalt);
     return Convert.ToBase64String(bytSalt);
 }
开发者ID:thanhphung901,项目名称:ketoan.app,代码行数:11,代码来源:Security.cs

示例5: GetNonceAsHexDigitString

 static string GetNonceAsHexDigitString(int lengthInBytes)
 {
     var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
     var bytes = new byte[lengthInBytes];
     rng.GetBytes(bytes);
     return ToHexDigitString(bytes);
 }
开发者ID:pusp,项目名称:o2platform,代码行数:7,代码来源:HttpMultipartMimeForm.cs

示例6: GenerateRandomString

        /// <remarks>
        /// http://stackoverflow.com/questions/730268/unique-random-string-generation
        /// </remarks>
        string GenerateRandomString(int length,
            string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
        {
            if (length < 0) throw new ArgumentOutOfRangeException(nameof(length), "length cannot be less than zero.");
            if (string.IsNullOrEmpty(allowedChars)) throw new ArgumentException("allowedChars may not be empty.");

            const int byteSize = 0x100;
            var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
            if (byteSize < allowedCharSet.Length)
                throw new ArgumentException($"allowedChars may contain no more than {byteSize} characters.");

            // Guid.NewGuid and System.Random are not particularly random. By using a
            // cryptographically-secure random number generator, the caller is always
            // protected, regardless of use.
            using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
            {
                var result = new StringBuilder();
                var buf = new byte[128];
                while (result.Length < length)
                {
                    rng.GetBytes(buf);
                    for (var i = 0; i < buf.Length && result.Length < length; ++i)
                    {
                        // Divide the byte into allowedCharSet-sized groups. If the
                        // random value falls into the last group and the last group is
                        // too small to choose from the entire allowedCharSet, ignore
                        // the value in order to avoid biasing the result.
                        var outOfRangeStart = byteSize - (byteSize%allowedCharSet.Length);
                        if (outOfRangeStart <= buf[i]) continue;
                        result.Append(allowedCharSet[buf[i]%allowedCharSet.Length]);
                    }
                }
                return result.ToString();
            }
        }
开发者ID:carmbrester,项目名称:Charm.Core,代码行数:38,代码来源:Randomizer.cs

示例7: GetNext

        public string GetNext(int length, char[] charSet)
        {
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", "Length cannot be less than zero.");
            }

            const int byteSize = 0x100;
            using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
            {
                var result = new StringBuilder();
                var buf = new byte[128];
                while (result.Length < length)
                {
                    rng.GetBytes(buf);
                    for (var i = 0; i < buf.Length && result.Length < length; ++i)
                    {
                        var outOfRangeStart = byteSize - (byteSize % charSet.Length);
                        if (outOfRangeStart <= buf[i]) continue;
                        result.Append(charSet[buf[i] % charSet.Length]);
                    }
                }
                return result.ToString();
            }
        }
开发者ID:alekseysukharev,项目名称:bank,代码行数:25,代码来源:RandomStringGenerator.cs

示例8: TestOccasaionallyIgnored

 public void TestOccasaionallyIgnored()
 {
     var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
     var buf = new byte[1];
     rnd.GetBytes(buf);
     if ( buf[0] > 100 ) Assert.Ignore("too big");
 }
开发者ID:inorton,项目名称:testvault,代码行数:7,代码来源:ExampleTests.cs

示例9: ZActor

 public ZActor(ZContext context, ZAction action, params object[] args)
     : this(context, default(string), action, args)
 {
     var rnd0 = new byte[8];
     using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) rng.GetBytes(rnd0);
     this.Endpoint = string.Format("inproc://{0}", ZContext.Encoding.GetString(rnd0));
 }
开发者ID:Giten2004,项目名称:clrzmq4,代码行数:7,代码来源:ZActor.cs

示例10: Generate

        public static string Generate()
        {
            // Generate random
            var rnd = new System.Security.Cryptography.RNGCryptoServiceProvider();
            var entropy = new byte[bytes - 4];
            try {
                rnd.GetBytes(entropy);
            } finally {
                rnd.Dispose();
            }

            // Hash
            var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] hash;
            try {
                hash = sha.ComputeHash(entropy);
            } finally {
                sha.Dispose();
            }

            // Compute output
            var raw = new byte[bytes];
            Array.Copy(entropy, 0, raw, 0, bytes - 4);
            Array.Copy(hash, 0, raw, bytes - 4, 4);

            // Convert to Base64
            return Convert.ToBase64String(raw).Replace('+', '!').Replace('/', '~');
        }
开发者ID:invertedtomato,项目名称:Amos2,代码行数:28,代码来源:Tokens.cs

示例11: GetRandomSeed

 static int GetRandomSeed()
 {
     byte[] bytes = new byte[4];
     System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
     rng.GetBytes(bytes);
     return BitConverter.ToInt32(bytes, 0);
 }
开发者ID:Vlanta,项目名称:CspBase,代码行数:7,代码来源:Program.cs

示例12: Espresso_Publisher

		static void Espresso_Publisher(ZContext context) 
		{
			// The publisher sends random messages starting with A-J:

			using (var publisher = new ZSocket(context, ZSocketType.PUB))
			{
				publisher.Bind("tcp://*:6000");

				ZError error;

				while (true)
				{
					var bytes = new byte[5];
					using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
					{
						rng.GetBytes(bytes);
					}

					if (!publisher.SendBytes(bytes, 0, bytes.Length, ZSocketFlags.None, out error))
					{
						if (error == ZError.ETERM)
							return;	// Interrupted
						throw new ZException(error);
					}

					Thread.Sleep(1);
				}
			}
		}
开发者ID:ray-zong,项目名称:zguide,代码行数:29,代码来源:espresso.cs

示例13: CreateSalt

 public string CreateSalt()
 {
     int size = 10;//new Random().Next(10, 15);
     var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
     var buff = new byte[size];
     rng.GetBytes(buff);
     return Convert.ToBase64String(buff);
 }
开发者ID:br3ach3r,项目名称:projectbase,代码行数:8,代码来源:Encrypt.cs

示例14: CreateSalt

        protected string CreateSalt(int size)
        {
            var rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            var buff = new byte[size];
            rng.GetBytes(buff);

            return Convert.ToBase64String(buff);
        }
开发者ID:MiroPakanec,项目名称:EFlats,代码行数:8,代码来源:CtrHash.cs

示例15: GetRandomNumber

 public static int GetRandomNumber(int min, int max)
 {
     System.Security.Cryptography.RNGCryptoServiceProvider rng =
         new System.Security.Cryptography.RNGCryptoServiceProvider();
     byte[] buffer = new byte[4];
     rng.GetBytes(buffer);
     int result = BitConverter.ToInt32(buffer, 0);
     return new System.Random(result).Next(min, max);
 }
开发者ID:xescrp,项目名称:breinstormin,代码行数:9,代码来源:RandomEngine.cs


注:本文中的System.Security.Cryptography.RNGCryptoServiceProvider.GetBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。