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


C# RNGCryptoServiceProvider.GetNonZeroBytes方法代码示例

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


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

示例1: 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

示例2: CreateDatabase

 public static UserWallet CreateDatabase(string path, string password)
 {
     using (WalletDataContext ctx = new WalletDataContext(path))
     using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
     {
         byte[] passwordKey = password.ToAesKey();
         byte[] masterKey = new byte[32];
         byte[] iv = new byte[16];
         rng.GetNonZeroBytes(masterKey);
         rng.GetNonZeroBytes(iv);
         masterKey.AesEncrypt(passwordKey, iv);
         Array.Clear(passwordKey, 0, passwordKey.Length);
         ctx.Database.EnsureDeleted();
         ctx.Database.EnsureCreated();
         ctx.Keys.Add(new Key
         {
             Name = Key.MasterKey,
             Value = masterKey
         });
         ctx.Keys.Add(new Key
         {
             Name = Key.IV,
             Value = iv
         });
         ctx.SaveChanges();
     }
     UserWallet wallet = OpenDatabase(path, password);
     wallet.CreateAccount();
     return wallet;
 }
开发者ID:EppoFq,项目名称:AntShares,代码行数:30,代码来源:UserWallet.cs

示例3: Run

        public static void Run()
        {
            using (RNGCryptoServiceProvider key = new RNGCryptoServiceProvider())
            {
                for (int i = 0; i < 12; i++)
                {
                    byte[] b = new byte[1];
                    key.GetNonZeroBytes(b);
                    Random rng = new Random(b[0]);
                    b = new byte[rng.Next(8, 16)];
                    key.GetNonZeroBytes(b);

                    double seed = b.Aggregate((int)1, (x, y) => x * y);
                    Console.WriteLine(seed);
                    int seed2 = 1;
                    for (int j = 0; j < b.Length; j++)
                    {
                        seed2 *= (int)b[j];
                    }
                    Console.WriteLine(seed2);
                    Console.ReadLine();
                }

            }
        }
开发者ID:anuade,项目名称:MScProject,代码行数:25,代码来源:RNG2.cs

示例4: GetUniqueIdentifier

        /// <summary>
        /// 生成指定长度的唯一标识<see cref="System.String"/>值
        /// </summary>
        /// <param name="length">要生成的标识长度</param>
        /// <returns>唯一标识的<see cref="System.String"/>值</returns>
        public static string GetUniqueIdentifier(int length)
        {
            while (true)
            {
                var maxSize = length;
                var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                var chars = a.ToCharArray();
                var size = maxSize;
                var data = new byte[1];
                using (var crypto = new RNGCryptoServiceProvider())
                {
                    crypto.GetNonZeroBytes(data);
                    data = new byte[size];
                    crypto.GetNonZeroBytes(data);
                }

                var result = new StringBuilder(size);
                foreach (var b in data)
                {
                    result.Append(chars[b%(chars.Length - 1)]);
                }

                // Unique identifiers cannot begin with 0-9
                if (result[0] >= '0' && result[0] <= '9')
                    continue;

                return result.ToString();
            }
        }
开发者ID:yangganggood,项目名称:ReportMS,代码行数:34,代码来源:Utils.cs

示例5: GetCryptographicRandomCharacterGenerator

        /// <summary>
        /// Gets the Cryptographic Random Character generated string.
        /// Developed by http://www.codeproject.com/KB/aspnet/UniqueKeys.aspx.
        /// </summary>
        /// <param name="characterLength">Length of the character.</param>
        /// <returns>A string with generated key.</returns>
        public static string GetCryptographicRandomCharacterGenerator(int characterLength)
        {
            StringBuilder returnValue;

            try
            {
                int maxSize = characterLength;
                const string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                char[] chars = characters.ToCharArray();
                var data = new byte[1];

                var crypto = new RNGCryptoServiceProvider();
                crypto.GetNonZeroBytes(data);

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

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

                returnValue = result;
            }
            catch (Exception ex)
            {
                Logger.Error("Error at GetCryptographicRandomCharacterGenerator", ex);
                throw;
            }

            return returnValue.ToString();
        }
开发者ID:allinonekk,项目名称:OurProject,代码行数:40,代码来源:RepositoryUtil.cs

示例6: CreateDatabase

 public static UserWallet CreateDatabase(string path, string password)
 {
     SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder();
     sb.AttachDBFilename = path;
     sb.DataSource = @"(LocalDB)\v11.0";
     sb.IntegratedSecurity = true;
     using (WalletDataContext ctx = new WalletDataContext(sb.ToString()))
     using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
     {
         byte[] passwordKey = password.ToAesKey();
         byte[] masterKey = new byte[32];
         byte[] iv = new byte[16];
         rng.GetNonZeroBytes(masterKey);
         rng.GetNonZeroBytes(iv);
         masterKey.AesEncrypt(passwordKey, iv);
         Array.Clear(passwordKey, 0, passwordKey.Length);
         ctx.Database.Delete();
         ctx.Database.Create();
         ctx.Keys.Add(new Key
         {
             Name = Key.MasterKey,
             Value = masterKey
         });
         ctx.Keys.Add(new Key
         {
             Name = Key.IV,
             Value = iv
         });
         ctx.SaveChanges();
     }
     UserWallet wallet = OpenDatabase(path, password);
     wallet.CreateAccount();
     return wallet;
 }
开发者ID:SuperHanZi,项目名称:guichou,代码行数:34,代码来源:UserWallet.cs

示例7: GetBytes

 public static byte[] GetBytes(int size)
 {
     byte[] data = new byte[1];
     RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
     crypto.GetNonZeroBytes(data);
     data = new byte[size];
     crypto.GetNonZeroBytes(data);
     return data;
 }
开发者ID:Gab-km,项目名称:MPack,代码行数:9,代码来源:Helpers.cs

示例8: randomNumber

 /// <summary>
 /// Generates a cryptographically secure random integer
 /// </summary>
 /// <returns>https://xkcd.com/221/</returns>
 public static int randomNumber()
 {
     byte[] data = new byte[1];
     using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
     {
         crypto.GetNonZeroBytes(data);
         data = new byte[31];
         crypto.GetNonZeroBytes(data);
     }
     return Math.Abs(BitConverter.ToInt32(data, 0));
 }
开发者ID:ext0,项目名称:SaneWeb,代码行数:15,代码来源:Utility.cs

示例9: GetUniqueKey

 public static string GetUniqueKey(int maxSize)
 {
     var data = new byte[1];
     var crypto = new RNGCryptoServiceProvider();
     crypto.GetNonZeroBytes(data);
     data = new byte[maxSize];
     crypto.GetNonZeroBytes(data);
     var result = new StringBuilder(maxSize);
     foreach (var b in data)
     {
         result.Append(Chars[b % (Chars.Length)]);
     }
     return result.ToString();
 }
开发者ID:NKumarPraveen,项目名称:ClearScriptV8Debugger,代码行数:14,代码来源:TokenGenerator.cs

示例10: GetUniqueKey

 public static string GetUniqueKey(int maxSize)
 {
     char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
     var data = new byte[1];
     var crypto = new RNGCryptoServiceProvider();
     crypto.GetNonZeroBytes(data);
     data = new byte[maxSize];
     crypto.GetNonZeroBytes(data);
     var result = new StringBuilder(maxSize);
     foreach (byte b in data)
     {
         result.Append(chars[b % (chars.Length)]);
     }
     return result.ToString();
 }
开发者ID:azmataliakhtar,项目名称:Infinity_Solution-,代码行数:15,代码来源:KeyGenerationUtil.cs

示例11: 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

示例12: Main

        static void Main(string[] args)
        {
            var rng = new RNGCryptoServiceProvider();

            var a = new byte[16];

            for (int i = 0; i < 10; i++)
            {
                //rng.GetBytes(a);
                rng.GetNonZeroBytes(a);

                foreach (var item in a)
                {
                    Console.Write("{0,4}", item);
                }

                Console.WriteLine();
            }

            //algoritmo de criptografia: trocar letras de lugar de acordo com uma chave pré-definida

            //bcfjkmpqrsuvwxyzagnldoetih -> chave de criptografia
            //abcdefghijklmnopqrstuvwxyz

            //atacar o inimigo -> mensagem
            //blbfbgyrxrwrpy -> mensagem cifrada (encriptada, criptografada)
            //atacaroinimigo -> mensagem decifrada (decriptada)

            Console.ReadKey();
        }
开发者ID:50minutos,项目名称:MOC-10265,代码行数:30,代码来源:Program.cs

示例13: GenerateSalt

 public byte[] GenerateSalt()
 {
     var rngCrypto = new RNGCryptoServiceProvider();
     var saltBytes = new byte[64];
     rngCrypto.GetNonZeroBytes(saltBytes);
     return saltBytes;
 }
开发者ID:shuiping150,项目名称:drinks-server,代码行数:7,代码来源:PasswordHelper.cs

示例14: GenerateLegacySaltedHash

        private static string GenerateLegacySaltedHash(string input, string hashAlgorithmId)
        {
            var saltBytes = new byte[SaltLengthInBytes];

            using (var cryptoProvider = new RNGCryptoServiceProvider())
            {
                cryptoProvider.GetNonZeroBytes(saltBytes);
            }

            var textBytes = Encoding.Unicode.GetBytes(input);

            var saltedTextBytes = new byte[saltBytes.Length + textBytes.Length];
            Array.Copy(saltBytes, saltedTextBytes, saltBytes.Length);
            Array.Copy(textBytes, 0, saltedTextBytes, saltBytes.Length, textBytes.Length);

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

            var saltPlusHashBytes = new byte[saltBytes.Length + hashBytes.Length];
            Array.Copy(saltBytes, saltPlusHashBytes, saltBytes.Length);
            Array.Copy(hashBytes, 0, saltPlusHashBytes, saltBytes.Length, hashBytes.Length);

            var saltedHash = Convert.ToBase64String(saltPlusHashBytes);
            return saltedHash;
        }
开发者ID:ashuthinks,项目名称:webnuget,代码行数:28,代码来源:CryptographyService.cs

示例15: GetUniqueKey

 public static string GetUniqueKey(int maxSize)
 {
     char[] chars = new char[62];
     chars = "123456789".ToCharArray();
     byte[] data = new byte[1];
     RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
     crypto.GetNonZeroBytes(data);
     data = new byte[maxSize];
     crypto.GetNonZeroBytes(data);
     StringBuilder result = new StringBuilder(maxSize);
     foreach (byte b in data)
     {
         result.Append(chars[b % (chars.Length)]);
     }
     return result.ToString();
 }
开发者ID:rajibrezwan,项目名称:--Restaurant-Management-System--maniacs-,代码行数:16,代码来源:frmSales.cs


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