本文整理汇总了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);
}
示例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();
}
示例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);
}
示例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);
}
示例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);
}
示例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();
}
}
示例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();
}
}
示例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");
}
示例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));
}
示例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('/', '~');
}
示例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);
}
示例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);
}
}
}
示例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);
}
示例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);
}
示例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);
}