本文整理汇总了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();
}
示例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;
}
示例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();
}
}
}
示例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();
}
}
示例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();
}
示例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;
}
示例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;
}
示例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));
}
示例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();
}
示例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();
}
示例11: GetSalt
public string GetSalt(int saltLength)
{
var rng = new RNGCryptoServiceProvider();
var saltBytes = new byte[saltLength];
rng.GetNonZeroBytes(saltBytes);
return BitConverter.ToString(saltBytes).Replace("-", "");
}
示例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();
}
示例13: GenerateSalt
public byte[] GenerateSalt()
{
var rngCrypto = new RNGCryptoServiceProvider();
var saltBytes = new byte[64];
rngCrypto.GetNonZeroBytes(saltBytes);
return saltBytes;
}
示例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;
}
示例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();
}