reading and writing data as BigInteger datatype numbers from a file in java

1.9k views Asked by At

I was coding the RSA algorithm with 2048 bit keys in JAVA. The keys generated are stored as BigIntegers datatype.

    int x=in.read();
    while(x!=-1)
    {
        data[a++] = BigInteger.valueOf(x);
        x=in.read();   
    }

This piece of code reads the input from a file using FileInputStream class. The BigInteger array data stores the input then exponential modulo operation is performed on it, then it is written to another file in form of byte array.

    for(int t=0;t<enc.length;t++)
    {
        out.write(enc[t].toByteArray());
    }

The output is the encrypted data. To decrypt it, I need to read the encrypted data from the output file, but on reading the byte stream from the file in form of byte array, I will not get the same BigInteger number to again exponent modulo it, as the code will not know how many bytes are to be clubbed to make the same number which was written. Is there any other method which could write the BigInteger numbers then read it form the file producing the same BigInteger numbers or any other technique to store and retrieve the RSA encrypted data from the file ?

0

There are 0 answers