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


C# Cryptography.RNGCryptoServiceProvider类代码示例

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


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


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