RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string zz = rsa.ToXmlString(true);
从zz中获取p和q的base64字串,并
byte[] p = Convert.FromBase64String(base64str).ToArray(); //(试过.Reverse(),也不行)
BigInteger tmpa =new BigInteger(p);
bool z = IsProbablePrime(tmpa);//判断是否为质数
测试结果是P和Q都不是质数,感觉不科学。是RSACryptoServiceProvider在偷懒或者抱有目的性,还是我判断质素的方法有误?
public static bool IsProbablePrime(BigInteger source)
{
int certainty = 2;
if (source == 2 || source == 3)
return true;
if (source < 2 || source % 2 == 0)
return false;
BigInteger d = source - 1;
int s = 0;
while (d % 2 == 0)
{
d /= 2;
s += 1;
}
RandomNumberGenerator rng = RandomNumberGenerator.Create();
byte[] bytes = new byte[source.ToByteArray().LongLength];
BigInteger a;
for (int i = 0; i < certainty; i++)
{
do
{
rng.GetBytes(bytes);
a = new BigInteger(bytes);
}
while (a < 2 || a >= source - 2);
BigInteger x = BigInteger.ModPow(a, d, source);
if (x == 1 || x == source - 1)
continue;
for (int r = 1; r < s; r++)
{
x = BigInteger.ModPow(x, 2, source);
if (x == 1)
return false;
if (x == source - 1)
break;
}
if (x != source - 1)
return false;
}
return true;
}
string zz = rsa.ToXmlString(true);
从zz中获取p和q的base64字串,并
byte[] p = Convert.FromBase64String(base64str).ToArray(); //(试过.Reverse(),也不行)
BigInteger tmpa =new BigInteger(p);
bool z = IsProbablePrime(tmpa);//判断是否为质数
测试结果是P和Q都不是质数,感觉不科学。是RSACryptoServiceProvider在偷懒或者抱有目的性,还是我判断质素的方法有误?
public static bool IsProbablePrime(BigInteger source)
{
int certainty = 2;
if (source == 2 || source == 3)
return true;
if (source < 2 || source % 2 == 0)
return false;
BigInteger d = source - 1;
int s = 0;
while (d % 2 == 0)
{
d /= 2;
s += 1;
}
RandomNumberGenerator rng = RandomNumberGenerator.Create();
byte[] bytes = new byte[source.ToByteArray().LongLength];
BigInteger a;
for (int i = 0; i < certainty; i++)
{
do
{
rng.GetBytes(bytes);
a = new BigInteger(bytes);
}
while (a < 2 || a >= source - 2);
BigInteger x = BigInteger.ModPow(a, d, source);
if (x == 1 || x == source - 1)
continue;
for (int r = 1; r < s; r++)
{
x = BigInteger.ModPow(x, 2, source);
if (x == 1)
return false;
if (x == source - 1)
break;
}
if (x != source - 1)
return false;
}
return true;
}