I have the following code. The function is to generate a random ID of a specific size. However i keep getting an error that says
‘System.Security.Cryptography.RNGCryptoServiceProvider’ does not contain a definition for ‘GetNonZeroBytes’ and no extension method ‘GetNonZeroBytes’ accepting a first argument of type ‘System.Security.Cryptography.RNGCryptoServiceProvider’ could be found (are you missing a using directive or an assembly reference)
I am trying to use the cryptography library in .NET and according to msdn this is one of the functions that can be used in this scenario but for some reason i keep getting this error.
Can anyone help?
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Security.Cryptography;
using System.Text;
namespace ONeRESPONSEAPP
{
public class IdGenerator
{
public static string GetUniqueKey(int maxSize)
{
char[] chars = new char[45];
chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
byte[] data = new byte[1];
RNGCryptoServiceProvider identity= new RNGCryptoServiceProvider();
identity.GetNonZeroBytes(data);
data = new byte[maxSize];
identity.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(maxSize);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length)]);
}
return result.ToString();
}
}
}
System.Security.Cryptography.RNGCryptoServiceProvider.GetNonZeroBytes()is not supported by Windows Phone 7.You might want to use
RNGCryptoServiceProvider.GetBytes()instead, it is cryptographic strong as well.