我正在玩El Gamal密码系统,我的目标是能够加密和破译长文本序列.
El Gamal要求明文为整数.我已经使用字符串的.getBytes()方法将我的字符串转换为byte [],然后在byte []中创建了一个BigInteger.在加密/解密之后,我使用BigIntegers的.toByteArray()方法将BigInteger转换为byte [],然后从byte []创建一个新的String对象.
我正在使用1035位密钥,当我使用最多129个字符的字符串加密/解密时,这非常有效.对于130个或更多字符,我的解密方法产生的输出是乱码.
有人可以建议如何解决这个问题吗?
您可以在C/C++/Java中推荐一些好的大整数计算库,并且最好支持对数运算.
谢谢.
BigInteger类有一个返回字节数组的方法.这是否表明该类在内部使用字节数组以存储数字?
知道这一点非常重要,以便选择正确的数据类型来操作二进制数据.例如,如果类使用Int64数组,那么类似的数组可以更有效地通过任何调用函数来操作原始数据.
作为一个例子,我调用ToByteArray方法来遍历寻找特定二进制模式的字节.
我试图将Double值数组转换为值数组BigInteger,但我无法弄清楚如何告诉java创建一个BigInteger与原始数组长度相同的新空数组.
出于某种原因,BigInteger没有像我想的那样工作.我正在做BigVariable.add(BigVariable),但它不会添加.它的结果始终是它初始化的值.谁知道我错过了什么?提前致谢
代码用于项目euler 48
import java.math.BigInteger;
public class tuna {
public static void main(String[] args) {
BigInteger result = BigInteger.ZERO;
for(int i= 1; i <= 1000; i++)
result.add( bigPow(BigInteger.valueOf(i), i) );
System.out.println(result);
}
public static BigInteger bigPow(BigInteger number, int pow){
if(pow < 1)
throw new RuntimeException("bigPow can't handle exponents lower than 1");
if (pow == 1)
return number;
return number.multiply( bigPow(number, pow-1) );
}
}
Run Code Online (Sandbox Code Playgroud) 您可以使用BigInteger.isProbablePrime()生成加密安全素数吗?他们"安全"需要什么确定性?
我正在创建一个大的整数类。我读了两个long作为函数参数,我想做的是让long读入的每个数字都占用一个数组的索引。
通过将long转换为字符串,然后将其转换为char数组并将它们临时存储在某个地方,可以做到这一点。这可以正常工作,并且在打印出来时可以像读入的数字一样打印出来。现在,我要做的就是将它们现在添加到新数组中,并将其数据类型设置为long。我已经做了两个数组来处理这个问题。
问题是当我尝试将char转换为long时,它给了我完全不同的价值。似乎是将char转换为自己的数字?字符类似乎没有任何转换为long的方法。
最好的方法是什么?
编辑:看来,如果我将长数组更改为int数组,然后使用Character.getNumericValue(char ch),则可以正常将其添加到数组中。
由于我计划在此函数的结尾处返回一个long值,因此我应该确保这些数组对安全性很久吗?还是将它们存储为int数组?谢谢
public static long hugeMultiplication(long value1, long value2){
System.out.println("originalvalue: "+value1);
int lengthOfWordOne = String.valueOf(value1).length();
int lengthOfWordTwo = String.valueOf(value1).length();
System.out.println("length1: "+lengthOfWordOne);
System.out.println("length2: "+lengthOfWordTwo);
long[] numberOne = new long[lengthOfWordOne+1];
long[] numberTwo = new long[lengthOfWordTwo+1];
//make those longs into string to convert char array
char[] tempValueOne = String.valueOf(value1).toCharArray();
char[] tempValueTwo = String.valueOf(value2).toCharArray();
//copy each value of a char array to long array and change to long again
//this will set up the array having each …Run Code Online (Sandbox Code Playgroud) 我从教科书中得到以下代码来计算阶乘:
import java.math.*;
public class LargeFactorial {
public static void main(String[] args) {
System.out.println("50! is \n" + factorial(50));
} public static BigInteger factorial(long n) {
BigInteger result = BigInteger.ONE;
for (int i = 1; i <= n; i++)
result = result.multiply(new BigInteger(i +""));
return result;
}
Run Code Online (Sandbox Code Playgroud)
但是,我真的不明白new BigInteger(i +"").为什么他们放入+""构造函数?我的意思是我们没有乘以一个空字符串,它也没有任何意义.请解释.
这是我的代码:
public class sample {
public static void main(String []args) throws Exception {
System.out.println("Enter from file:");
BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\KK\\A Key.txt"));
String currentline;
while((currentline=br.readLine())!=null){
System.out.println(currentline);
}
BigInteger a = new BigInteger(currentline);
System.out.println(a);
}
Run Code Online (Sandbox Code Playgroud)
我想从文本文档中读取,从字符串转换为大整数,我试过这个但是我得到一个运行时错误,如何将String转换为相应的大整数Ascii值.
从这篇文章中,我理解的位长bitlength()将给您
此BigInteger的最小2补码表示形式中的位数
但是,当我运行以下程序输出为 0时,我希望此输出为1,因为数字0用一位表示,我很困惑,请帮助
public class Test {
public static void main(String[] args) {
int l = BigInteger.valueOf(0L).bitLength();
System.out.println(l);
}
}
Run Code Online (Sandbox Code Playgroud) biginteger ×10
java ×9
arrays ×2
cryptography ×2
.net ×1
algorithm ×1
c ×1
c# ×1
c++ ×1
double ×1
elgamal ×1
java-8 ×1
long-integer ×1
primes ×1
string ×1