如何将以下 Java 行转换为 C#。它生成一个 130 位大小的随机 BigInteger,将其转换为以 32 为底的字符串(即不是十进制),然后操作该字符串:
new BigInteger(130, new SecureRandom()).toString(32).replace("/", "w").toUpperCase(Locale.US);
我怎样才能在 C# 中实现这一点?
- 生成一个随机的 130 位 BigInteger
- 将其转换为 base 32 中的字符串
至于随机 BigInteger 我有这个功能:
static BigInteger RandomInteger(int bits)
{
RNGCryptoServiceProvider secureRandom = new RNGCryptoServiceProvider();
// make sure there is extra room for a 0-byte so our number isn't negative
// in the case that the msb is set
var bytes = new byte[bits / 8 + 1];
secureRandom.GetBytes(bytes);
// mask off excess bits
bytes[bytes.Length - 1] &= (byte)((1 << (bits % 8)) - 1);
return new BigInteger(bytes);
}
取自未解决基数 32 转换的问题:Equivalent of Java's BigInteger in C#
但是我不确定该功能是否也正确。
到目前为止,我拥有的 C# 代码 RandomInteger 是上述函数:
RandomInteger(130).ToString().Replace("/","w").ToUpper(CultureInfo.GetCultureInfo("en-US"));