• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# Cryptography.RNGCryptoServiceProvider类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: CreateReference

        public string CreateReference(int size)
        {
            const int byteSize = 0x100;
            var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            var allowedCharSet = new HashSet<char>(chars).ToArray();

            using (var cryptoProvider = new RNGCryptoServiceProvider())
            {
                var result = new StringBuilder();
                var buffer = new byte[128];

                while (result.Length < size)
                {
                    cryptoProvider.GetBytes(buffer);

                    for (var i = 0; i < buffer.Length && result.Length < size; ++i)
                    {
                        var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);

                        if (outOfRangeStart <= buffer[i])
                        {
                            continue;
                        }

                        result.Append(allowedCharSet[buffer[i] % allowedCharSet.Length]);
                    }
                }
                return result.ToString();
            }
        }
开发者ID:letmeproperty,项目名称:TransactionalSMS,代码行数:30,代码来源:CryptographicReferenceGenerator.cs


示例2: CreateRandomSalt

 public static string CreateRandomSalt()
 {
     var saltBytes = new Byte[4];
     var rng = new RNGCryptoServiceProvider();
     rng.GetBytes(saltBytes);
     return Convert.ToBase64String(saltBytes);
 }
开发者ID:jpatte,项目名称:RavenDBMembership,代码行数:7,代码来源:PasswordUtil.cs


示例3: NextInt32

 public static int NextInt32(int max)
 {
     byte[] bytes = new byte[sizeof(int)];
     RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
     Gen.GetBytes(bytes);
     return Math.Abs(BitConverter.ToInt32(bytes, 0) % max);
 }
开发者ID:hanistory,项目名称:hasuite,代码行数:7,代码来源:Mouse.cs


示例4: GenerateSalt

        /// <summary>
        /// Generate Password Salt Int to add to password
        /// </summary>
        /// <returns>Salt Int</returns>

        internal string GenerateSalt()
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[32];
            rng.GetBytes(buff);
            return Convert.ToBase64String(buff);
        }
开发者ID:ChrisNelsonPE,项目名称:surveyproject_main_public,代码行数:12,代码来源:User.cs


示例5: NextInt64

 public static Int64 NextInt64()
 {
     var bytes = new byte[sizeof(Int64)];
        RNGCryptoServiceProvider Gen = new RNGCryptoServiceProvider();
        Gen.GetBytes(bytes);
        return BitConverter.ToInt64(bytes , 0);
 }
开发者ID:stefchri,项目名称:ArticulatieOnderzoek,代码行数:7,代码来源:Methods.cs


示例6: CreateHashedText

        /// <summary>
        /// CreateHashedText
        /// </summary>
        /// <param name="plainText"></param>
        /// <param name="useSalt"></param>
        /// <returns></returns>
        public static KeyValuePair<string, string> CreateHashedText(string plainText,bool useSalt)
        {
            var plainTextBytes = Encoding.Unicode.GetBytes(plainText);

            string salt = string.Empty;
            string hashedText = string.Empty;
            if (useSalt)
            {
                var saltBytes = new byte[0x10];
                using (var random = new RNGCryptoServiceProvider())
                {
                    random.GetBytes(saltBytes);
                }

                salt = Convert.ToBase64String(saltBytes);
                plainTextBytes=saltBytes.Concat(plainTextBytes).ToArray();
            }

            byte[] hashBytes;
            using (var hashAlgorithm = HashAlgorithm.Create())
            {
                hashBytes = hashAlgorithm.ComputeHash(plainTextBytes);
            }

            hashedText = Convert.ToBase64String(hashBytes);

            return new KeyValuePair<string, string>(hashedText, salt);
        }
开发者ID:eCollobro,项目名称:eCollabro,代码行数:34,代码来源:DataEncryption.cs


示例7: CreateKey

 /// <summary>
 /// Creates a key based on the indicated size.
 /// </summary>
 /// <param name="numBytes">size of the key.</param>
 /// <returns>Generated key of the specified length.</returns>
 public static string CreateKey(int numBytes)
 {
     RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
     byte[] buff = new byte[numBytes];
     rng.GetBytes(buff);
     return BytesToHexString(buff);
 }
开发者ID:kyallbarrows,项目名称:LifeguardServer,代码行数:12,代码来源:KeyCreator.cs


示例8: GenerateRandomString

        /// <summary>
        /// Generate a random string of characters
        /// </summary>
        /// <param name="length">length of string</param>
        /// <param name="type">type of string to be generated</param>
        /// <returns></returns>
        public static string GenerateRandomString(int length, StringType type)
        {
            switch (type)
            {
                case StringType.AlphaNumeric:
                    string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
                    char[] chars = new char[length];
                    Random rd = new Random();

                    for (int i = 0; i < length; i++)
                    {
                        chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
                    }

                    return new string(chars);
                    break;

                case StringType.AlphaNumericSymbol:
                    //Generate a cryptographic random number.
                    var rng = new RNGCryptoServiceProvider();
                    var buff = new byte[length];
                    rng.GetBytes(buff);

                    rng.Dispose();

                    // Return a Base64 string representation of the random number.
                    return Convert.ToBase64String(buff);
                    break;
                default:
                    throw new ArgumentException("Type not supported");
            }
        }
开发者ID:shawnmclean,项目名称:ShawnMclean-.Net-Utility-Library,代码行数:38,代码来源:StringHelpers.cs


示例9: Generate

        public static string Generate(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
        {
            if (length < 0) throw new ArgumentOutOfRangeException("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(String.Format("allowedChars may contain no more than {0} characters.", byteSize));

            // 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 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:jclement,项目名称:mattermost-dynamics-bot,代码行数:33,代码来源:SecureRandomString.cs


示例10: GetSalt

 public string GetSalt(int saltLength)
 {
     var rng = new RNGCryptoServiceProvider();
     var saltBytes = new byte[saltLength];
     rng.GetNonZeroBytes(saltBytes);
     return BitConverter.ToString(saltBytes).Replace("-", "");
 }
开发者ID:rnofenko,项目名称:Calendar,代码行数:7,代码来源:RandomSaltProvider.cs


示例11: GetRandomSalt

 /// <summary>
 /// Returns a random 64 character hex string (256 bits)
 /// </summary>
 /// <returns>The random string</returns>
 private static string GetRandomSalt()
 {
     RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
     byte[] salt = new byte[32]; // 256 bits
     random.GetBytes(salt);
     return BytesToHex(salt);
 }
开发者ID:lstern,项目名称:practices,代码行数:11,代码来源:SaltedHash.cs


示例12: TripleDESKeyWrapEncrypt

        //
        // internal static methods
        //

        // CMS TripleDES KeyWrap as described in "http://www.w3.org/2001/04/xmlenc#kw-tripledes"
        internal static byte[] TripleDESKeyWrapEncrypt (byte[] rgbKey, byte[] rgbWrappedKeyData) {
            // checksum the key
            SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
            byte[] rgbCKS = sha.ComputeHash(rgbWrappedKeyData);

            // generate a random IV
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] rgbIV = new byte[8];
            rng.GetBytes(rgbIV);

            // rgbWKCS = rgbWrappedKeyData | (first 8 bytes of the hash)
            byte[] rgbWKCKS = new byte[rgbWrappedKeyData.Length + 8];
            TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
            // Don't add padding, use CBC mode: for example, a 192 bits key will yield 40 bytes of encrypted data
            tripleDES.Padding = PaddingMode.None;
            ICryptoTransform enc1 = tripleDES.CreateEncryptor(rgbKey, rgbIV);
            Buffer.BlockCopy(rgbWrappedKeyData, 0, rgbWKCKS, 0, rgbWrappedKeyData.Length);
            Buffer.BlockCopy(rgbCKS, 0, rgbWKCKS, rgbWrappedKeyData.Length, 8);
            byte[] temp1 = enc1.TransformFinalBlock(rgbWKCKS, 0, rgbWKCKS.Length);
            byte[] temp2 = new byte[rgbIV.Length + temp1.Length];
            Buffer.BlockCopy(rgbIV, 0, temp2, 0, rgbIV.Length);
            Buffer.BlockCopy(temp1, 0, temp2, rgbIV.Length, temp1.Length);
            // temp2 = REV (rgbIV | E_k(rgbWrappedKeyData | rgbCKS))
            Array.Reverse(temp2);

            ICryptoTransform enc2 = tripleDES.CreateEncryptor(rgbKey, s_rgbTripleDES_KW_IV);
            return enc2.TransformFinalBlock(temp2, 0, temp2.Length);
        }
开发者ID:JianwenSun,项目名称:cc,代码行数:33,代码来源:SymmetricKeyWrap.cs


示例13: GenerateRandomSalt

 public static string GenerateRandomSalt()
 {
     RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
     byte[] bytes = new byte[10];
     provider.GetBytes(bytes);
     return Convert.ToBase64String(bytes);
 }
开发者ID:yudamaan,项目名称:ImageSharingWithUsers,代码行数:7,代码来源:PasswordHelper.cs


示例14: GetRandomSaltValue

 private byte[] GetRandomSaltValue()
 {
     var rcsp = new RNGCryptoServiceProvider();
     var bSalt = new byte[16];
     rcsp.GetBytes(bSalt);
     return bSalt;
 }
开发者ID:rut5949,项目名称:Dnn.Platform,代码行数:7,代码来源:MembershipPasswordController.cs


示例15: GenerateToken

 public static string GenerateToken()
 {
     RNGCryptoServiceProvider r = new RNGCryptoServiceProvider();
     byte[] buffer = new byte[39];
     r.GetNonZeroBytes(buffer);
     return Convert.ToBase64String(buffer);
 }
开发者ID:BenCmd,项目名称:BenCmd-Main,代码行数:7,代码来源:TokenGenerator.cs


示例16: GenerateSalt

 public string GenerateSalt() {
 var data = new byte[0x10];
 using (var cryptoServiceProvider = new RNGCryptoServiceProvider()) {
 cryptoServiceProvider.GetBytes(data);
 return Convert.ToBase64String(data);
 }
 }
开发者ID:saif-adil,项目名称:First_repo,代码行数:7,代码来源:CryptoService.cs


示例17: GenerateSalt

 public string GenerateSalt()
 {
     var random = new RNGCryptoServiceProvider();
     var salt = new Byte[8];
     random.GetBytes(salt);
     return Convert.ToBase64String(salt);
 }
开发者ID:OleksandrKL,项目名称:c_sharp_examples,代码行数:7,代码来源:User.cs


示例18: GetUniqueNumber

		/// <summary>
		/// http://www.codeproject.com/Articles/14403/Generating-Unique-Keys-in-Net
		/// </summary>
		/// <returns></returns>
		public static string GetUniqueNumber()
		{
			//string result = DateTime.Now.Year.ToString() + Guid.NewGuid().ToString().GetHashCode().ToString("x");
			//return result;

			int maxSize = 8;
			char[] chars = new char[62];
			string a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
			chars = a.ToCharArray();
			int size = maxSize;
			byte[] data = new byte[1];

			var crypto = new RNGCryptoServiceProvider();
			crypto.GetNonZeroBytes(data);
			data = new byte[size];
			crypto.GetNonZeroBytes(data);
			var result = new StringBuilder(size);

			foreach (byte b in data)
			{
				result.Append(chars[b % (chars.Length - 1)]);
			}

			return "CS" + result.ToString();

		}
开发者ID:gitter-badger,项目名称:vc-community-1.x,代码行数:30,代码来源:UniqueNumberGenerator.cs


示例19: CreateCardsValues

        public static Stack<int> CreateCardsValues([NotNull] GameStage stage)
        {
            Contract.Requires(stage != null);

            var totalNumberOfCards = stage.CardsRows.Sum();
            var numberOfDifferentCards = totalNumberOfCards / stage.CardsInGroup;

            var cardsValues = new List<int>();

            for (int i = 0; i < numberOfDifferentCards; i++)
            {
                cardsValues.AddRange(Enumerable.Repeat(i, stage.CardsInGroup));
            }

            using (var rndGen = new RNGCryptoServiceProvider())
            {
                byte[] bytes = new byte[cardsValues.Count];
                rndGen.GetBytes(bytes);

                int[] ints = bytes.Select(b => b - byte.MaxValue / 2).ToArray();

                var rnd = new Random((int)DateTime.UtcNow.Ticks);

                cardsValues.Sort((c1, c2) => ints[rnd.Next(0, ints.Length)]);
            }

            return new Stack<int>(cardsValues);
        }
开发者ID:yakimovim,项目名称:MemoryCards,代码行数:28,代码来源:CardValuesProvider.cs


示例20: GetBuffer

 public static byte[] GetBuffer(int length)
 {
     RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
     byte[] output = new byte[length];
     rng.GetBytes(output);
     return output;
 }
开发者ID:Clodo76,项目名称:airvpn-client,代码行数:7,代码来源:RandomGenerator.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Cryptography.RSA类代码示例发布时间:2022-05-26
下一篇:
C# Cryptography.PasswordDeriveBytes类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap