我正在尝试生成BigInteger类型的随机素数,即我提供的最小值和最大值之间.
我知道BigInteger.probablePrime(int bitlength,random),但我不确定比特长度是如何转换为输出素数的最大值/最小值.
谢谢,Steven1350
我正试图弄清楚如何从头开始实施RSA加密(仅用于智力练习),我坚持这一点:
对于加密,c = m e mod n
现在,e通常是65537.m和n是1024位整数(例如128字节数组).对于标准方法来说,这显然太大了.你会如何实现这个?
我在这里读了一些关于取幂的内容,但它并没有点击我:
本章(见第14.85节)
谢谢.
编辑:也发现了这个 - 这是我应该看的更多吗?维基百科 - 模块化指数
我想安全地生成[0,N]范围内的随机数,其中N是一个参数.但是,System.Security.Cryptography.RandomNumberGenerator仅提供GetBytes()方法以使用随机值填充数组.
(我需要在稍微修改过的SRP版本中使用的随机整数."略微修改"的部分是我无法控制的,这是我甚至接触加密内容的唯一原因.)
我已经写了一个方法来做到这一点,但我正在寻找一种更好的方法,或者至少确认我做得对.
using System.Numerics
///<summary>Generates a uniformly random integer in the range [0, bound).</summary>
public static BigInteger RandomIntegerBelow(this System.Security.Cryptography.RandomNumberGenerator source, BigInteger bound) {
Contract.Requires<ArgumentException>(source != null);
Contract.Requires<ArgumentException>(bound > 0);
Contract.Ensures(Contract.Result<BigInteger>() >= 0);
Contract.Ensures(Contract.Result<BigInteger>() < bound);
//Get a byte buffer capable of holding any value below the bound
var buffer = (bound << 16).ToByteArray(); // << 16 adds two bytes, which decrease the chance of a retry later on
//Compute where the last partial fragment starts, in order to retry …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用整数数组在JavaScript中实现BigInt类型.现在每个都有一个256的上限.我已经完成了所有整数操作,但我无法弄清楚如何将BigInt转换为它的字符串表示.当然,简单的方法是:
BigInt.prototype.toString = function(base) {
var s = '', total = 0, i, conv = [
,,
'01',
'012',
'0123',
'01234',
'012345',
'0123456',
'01234567',
'012345678',
'0123456789',
,
,
,
,
,
'0123456789abcdef'
];
base = base || 10;
for(i = this.bytes.length - 1; i >= 0; i--) {
total += this.bytes[i] * Math.pow(BigInt.ByteMax, this.bytes.length - 1 - i);
}
while(total) {
s = conv[base].charAt(total % base) + s;
total = Math.floor(total / base);
}
return s || '0';
};
Run Code Online (Sandbox Code Playgroud)
但是当BigInts变 …
我们的产品中有一组位置需要BigInteger,因为数字可能相当长.然而,在超过90%的情况下,它们实际上并不那么长,并且很容易被包含在一起.
看看BigInteger的实现,在Long足够的情况下使用BigInteger会非常浪费.
是否有意义创建一个具有BigInteger(除法,乘法等)函数的接口,并且由BigInteger的子类和包装Long的类实现?就像是:
Interface: EfficientBigInteger
Class 1: MyBigInteger extends BigInteger imlpements EfficientBigInteger
Class 2: MyLong implements EfficientBigInteger (this will contain a Long, as we cannot extend the Long class)
Run Code Online (Sandbox Code Playgroud)
也许我们在这方向错了?
谢谢,Yon
更新:这些对象(Long或BigInteger)存储在内存中已有一段时间了,因为它们可以帮助我们识别与之交互的系统的有问题的行为.因此,内存占用可能是个问题.这是我们试图避免的问题.BigInteger类有几个字段(signum,mag数组,bitcount等等),它们大致是封装Long的类的两倍(考虑到首先拥有Object的内存成本).这意味着我们使用了很多东西的占地面积增加了一倍.
如何在c#中实现这个python代码?
Python代码:
print(str(int(str("e60f553e42aa44aebf1d6723b0be7541"), 16)))
Run Code Online (Sandbox Code Playgroud)
结果:
305802052421002911840647389720929531201
Run Code Online (Sandbox Code Playgroud)
但是在c#中我遇到了大数字问题.
你能帮助我吗?
我在python和c#中得到了不同的结果.哪里可以搞错?
在Java中,我能做到
//Parsing Octal String
BigInteger b = new BigInteger("16304103460644701340432043410021040424210140423204",8);
Run Code Online (Sandbox Code Playgroud)
然后按照我的喜好格式化它
b.toString(2); //2 for binary
b.toString(10); //10 for decimal
b.toString(16); //16 for hexadecimal
Run Code Online (Sandbox Code Playgroud)
C#BigInteger提供了上面显示的格式化功能,但我似乎无法找到解析BIIIG(大于64位,无符号)八进制值的方法.
在Java中,要将String转换为BigInteger,您可以使用构造函数,new BigInteger(String)但要转换为int/long,您使用的是工厂函数BigInteger.valueof(long),为什么会这样?
我正在尝试使用java.math.BigInteger进行一些精确的整数矩阵计算,其中标量值达到数百万位.我注意到一些内置的BigInteger操作出乎意料地非常慢 - 特别是gcd的一些情况,以及更多modInverse的情况.看来我可以更快地实现我自己的这些函数版本.
我写了一个程序打印计算gcd(10 ^ n-3,10 ^ n)的时间,用于增加n的值达到一百万左右,使用内置gcd或我自己的简单替代实现:
private static java.math.BigInteger myGcd(java.math.BigInteger a, java.math.BigInteger b)
{
a = a.abs();
b = b.abs();
while (true)
{
if (b.signum() == 0) return a;
a = a.mod(b);
if (a.signum() == 0) return b;
b = b.mod(a);
}
} // myGcd
Run Code Online (Sandbox Code Playgroud)
我在ubuntu linux下使用java 8运行它,运行时版本为1.8.0_111-8u111-b14-2ubuntu0.16.04.2-b14.在Java运行时1.8.0_92的macbook上,时间相对大致相似.
内置gcd大致是二次方的:
# numDigits seconds
1 0.000005626
2 0.000008172
4 0.000002852
8 0.000003097
16 0.000019158
32 0.000026365
64 0.000058330
128 0.000488692
256 0.000148674
512 0.007579581
1024 0.001199623
2048 0.001296036
4096 0.021341193 …Run Code Online (Sandbox Code Playgroud) 通过API通过R从BigQuery查询数据时,是否有比将大数转换为字符串更好的处理大整数的方法?
这是一个MVE,显示整数显示为“ NA”的问题:
> library(bigrquery)
>
> bq_str <- "
+ SELECT
+ 206932402797274623 AS big_pk
+ ,SAFE_CAST(206932402797274623 AS string) AS string_pk
+ "
>
> my_df <- bigrquery::query_exec(query = bq_str,
+ project = 'XXXXXXXXXXX',
+ use_legacy_sql = FALSE,
+ bigint = "integer64")
0 bytes processed
Warning message:
In converter[[type]](data_m[i, ]) :
NAs introduced by coercion to integer range
> head(my_df)
big_pk string_pk
1 NA 206932402797274623
Run Code Online (Sandbox Code Playgroud)
这是代码:
library(bigrquery)
bq_str <- "
SELECT
206932402797274623 AS big_pk
,SAFE_CAST(206932402797274623 AS string) AS string_pk …Run Code Online (Sandbox Code Playgroud) biginteger ×10
java ×4
.net ×2
c# ×2
cryptography ×2
bignum ×1
bigrquery ×1
encryption ×1
integer ×1
javascript ×1
math ×1
octal ×1
primes ×1
python ×1
r ×1
random ×1
security ×1