可能生成BigInteger值碰撞?

时间:2014-02-15 19:16:01

标签: java biginteger collision

我正在对由BigInteger值表示的文件实施数字签名系统。 不幸的是,由于异常“零长度BigInteger”,空文件无法通过byte []转换为BigInteger。

所以我用以下代码表示空文件:

bigmesssage = new BigInteger ("0");

我现在关注的是:当我在下面的程序BigIntegerTest.java中读取文件“test1”时,是否存在可能表示为BigInteger(“0”)的文件的值冲突!

包含单个“0”的文件“test1”的程序输出结果:

0: 0
test1: 12298

非常感谢 格里特

import  java.io.*;
import  java.math.BigInteger;
import  java.security.NoSuchAlgorithmException;


public class BigIntegerTest {



/**
 * @param args
 * @throws NoSuchAlgorithmException 
 */
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO Auto-generated method stub

    byte[] message1;
    BigInteger bigmessage1 = null;
    BigInteger bigmessage2=new BigInteger ("0");        

    System.out.println("0: "+bigmessage2);

    String filename="./bin/test1";

    message1 = To_Byte_Array(filename);

    bigmessage1= new BigInteger (message1);

    System.out.println("test1: "+ bigmessage1);

}

public static byte[] To_Byte_Array (String filename) throws java.security.NoSuchAlgorithmException {
      //Liest die Nachricht, die in der Datei filename gespeichert ist, ein und
      //speichert sie als byte-Array in message.

      //lokale Variablen:
      byte[] data = null;
     // MessageDigest hash = MessageDigest.getInstance("SHA-512");//SHA2 //removed

      //Streams, in:
      File textFile;//Textdatei
      FileInputStream in;//Dateieingabe-Stream

      try {

          textFile = new File(filename);
          in = new FileInputStream(textFile);
          int size = (int)textFile.length(); // Dateilaenge
          int read = 0;    // Anzahl der gelesenen Zeichen
          data = new byte[size];      // Lesepuffer
          // Auslesen der Datei
          while (read < size)
            read =+ in.read(data, read, size-read);
          in.close();
          // Schreiben des Lesepuffers in Instanz von MessageDigest und speichern des Hash-Werts in message
          //hash.update (data);//removed
          //message=hash.digest ();//removed


      }//try
      catch (IOException ex) {
        ex.printStackTrace();
      }
      return data;//added
    }//To_Byte_Array


}

3 个答案:

答案 0 :(得分:0)

应该可以创建一个与任何其他BigInteger不等的BigInteger

BigInteger内部存储int signum(表示数字的符号)和int[] mag,其中包含表示数字二进制值的字节。

但是,BigInteger.ZERO包含int[] mag = new int[0]signum = 0(在所有情况下确保0 == 0)。如果您制作包含相同zeromag为'1'的特殊signum,则您将拥有一个不等于BigInteger的唯一BigInteger.ZERO但将在数学上与BigInteger.ZERO完全相同。

这似乎创建了一个不等于ZERO的BigInteger,但是对它进行数学运算似乎打乱了BigInteger数学。

public void test() throws Exception {
    BigInteger NAN = bigNAN();
    System.out.println("NAN==ZERO " + NAN.equals(BigInteger.ZERO));
    System.out.println("NAN+0 " + NAN.add(BigInteger.ZERO));
}

public static BigInteger bigNAN() throws Exception {
    BigInteger NAN = new BigInteger(new byte[]{0});
    Field hack = BigInteger.class.getDeclaredField("signum");
    hack.setAccessible(true);
    hack.set(NAN, 1);
    return NAN;
}

打印

NAN==ZERO false
java.lang.ArrayIndexOutOfBoundsException: 0
...

答案 1 :(得分:0)

使用常量BigInteger.ZERO,它不会==到任何其他“零”实例。

然后,您可以使用==来测试文件是否为空:

if (bigmesssage == BigInteger.ZERO)
    // the file is empty

答案 2 :(得分:0)

我发现无法生成与所有其他BigInteger值一样的唯一BigInteger值。我正在考虑由散列函数sha256sum计算的唯一值:

leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/gmr-digital-signature-2$ sha256sum test1
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  test1

test1是空文件以及:

leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/gmr-digital-signature-2$ echo -ne '' | sha256sum 
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855  -

与ASCII值^ @或\ x00:

相反
leder@leder-HP-Pavilion-dv7-Notebook-PC:~/workspace1/BigIntegerTest/bin$ echo -ne '\x00' | sha256sum 
6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d  -

因此,我将空文件排除在我的系统之外!

碰撞应该使用值'0'两次:对于空文件和echo -ne '\x00' > test1a。 从以下演示中可以看出,对BigInteger计算有副作用:

import  java.io.*;
import  java.math.BigInteger;
import java.security.NoSuchAlgorithmException;


public class BigIntegerTest {



/**
 * @param args
 * @throws NoSuchAlgorithmException 
 */
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO Auto-generated method stub

    byte[] message3;
    BigInteger bigmessage1 = null;
    BigInteger bigmessage2 = null;      
    BigInteger bigmessage3 = null;



    bigmessage1 = BigInteger.ZERO; 

    System.out.println("BigInteger.ZERO: "+bigmessage1);

    System.out.println("null: "+bigmessage2);

    String filename="./bin/test1a";

    message3 = To_Byte_Array(filename);

    bigmessage3= new BigInteger (message3);

    System.out.println("test1a: "+ bigmessage3);

    String comp = null;

    if (bigmessage1==bigmessage3) {comp="true";} else {comp="false";};

    System.out.println("bigmessage1==bigmessage3: " + comp);

    if (BigInteger.ZERO==BigInteger.ZERO) {comp="true";} else {comp="false";};

    System.out.println("BigInteger.ZERO==BigInteger.ZERO: " + comp);

}

public static byte[] To_Byte_Array (String filename) throws java.security.NoSuchAlgorithmException {
      //Liest die Nachricht, die in der Datei filename gespeichert ist, ein und
      //speichert sie als byte-Array in message.

      //lokale Variablen:
      byte[] data = null;
     // MessageDigest hash = MessageDigest.getInstance("SHA-512");//SHA2 //removed

      //Streams, in:
      File textFile;//Textdatei
      FileInputStream in;//Dateieingabe-Stream

      try {

          textFile = new File(filename);
          in = new FileInputStream(textFile);
          int size = (int)textFile.length(); // Dateilaenge
          int read = 0;    // Anzahl der gelesenen Zeichen
          data = new byte[size];      // Lesepuffer
          // Auslesen der Datei
          while (read < size)
            read =+ in.read(data, read, size-read);
          in.close();
          // Schreiben des Lesepuffers in Instanz von MessageDigest und speichern des Hash-Werts in message
          //hash.update (data);//removed
          //message=hash.digest ();//removed


      }//try
      catch (IOException ex) {
        ex.printStackTrace();
      }
      return data;//added
    }//To_Byte_Array


}

----------生成的输出:

BigInteger.ZERO: 0
null: null
test1a: 0
bigmessage1==bigmessage3: false
BigInteger.ZERO==BigInteger.ZERO: true