# How to store and use large numbers containing more than 100 digits in java??

• February 18th, 2010, 07:59 PM
lovelysri16
How to store and use large numbers containing more than 100 digits in java??
Hi one and all.
Im doing a project on secret key protection n recovery. Im trying to implement SHAMIR's Secret scheme in java. The pblm im facing is that the secret key which can contain all types of characters(ascii values 0-127) when converted to number(integer) is having a length of more than 100 digits for 33+ characters. I cant limit my secret key to 5 or 4 characters but that kills the purpose. Im converting each character of the string into its ascii number, at the same time im making sure that each character corresponds to 3 digit integer by appending two 0's for the ascii values 0-9, a single 0 for the acsii values between 10-99. So i can club 3 digits to reconstruct the each character n finally the secret key.
But the pblm is that the number is becoming huge n i dunno how to handle it. I have to perform some arithmetic operations on that number, mainly addition. Im posting the code:

import java.util.*;
import java.io.*;
public class Project
{
public static long LIP(int q[],long p[],int degree,long despos)
{
long val=0;
for(int i=0;i<degree;++i)
{
long w=1;
for(int j=0;j<degree;++j)
if(j!=i)
w*=(despos-q[j])/(q[i]-q[j]);
val+=w*p[i];
}
return val;
}
public static void main(String[] args)
{
String S="6059"; // i used a small number in my pgm. Wat if the number
exceeds 100 digits?? How to perform the
following operations??
int k=4;
int n=8;
long x=0;
long r[]=new long[4]; //random numbers
int q[]=new int[4]; //k known shares' numbers
long p[]=new long[8]; //shares
Random R=new Random();
for(int i=0;i<k-1;i++)
{
r[i]=R.nextInt(1000);
System.out.println(r[i]); //Coefficients of the polynomial
generated randomnly
}
int b =Integer.parseInt(S);
System.out.println(b); //The secret key converted into an integer.
p[0]=(long)(b);
for(int i=0;i<n;i++)
for(int j=1;j<k-1;j++)
p[i]=p[i]+(long)(r[j]*Math.pow(i,j));
for(int i=0;i<n;i++)
System.out.println("("+i+","+p[i]+")"); // n secret shares
for(int i=0;i<k;i++)
q[i]=i;
System.out.println("The shares known are");
for(int i=0;i<k;i++)
System.out.println("("+q[i]+","+p[q[i]]+")");
x=LIP(q,p,k-1,0);
System.out.println(x);
}
}

Please help me out. One solution can be to divide the number into different parts n to use the last parts to perform the arithmetic functions as the random numbers generated are less than 1000 n the values of n are also less than 10. So their multiplication in the polynomial will be a max of 4-5 digits.
• February 19th, 2010, 03:30 AM
Json
Re: How to store and use large numbers containing more than 100 digits in java??
I'm not entirely sure I understand your question but here goes.

I assume you have a large string which is the key. You then loop through the key and generate a 3 digit number for each character.

You then want to store this number somewhere but its ending up being long as well.

For instance you have a 4 character key which ends up being a number of 4 x 3 = 12 numerical digits?

So a key of 50 characters would be about 150 digits.

Can you not store the key as a byte array then?

// Json
• February 19th, 2010, 07:10 AM
lovelysri16
Re: How to store and use large numbers containing more than 100 digits in java??
@json
Thnx for ur quick response mate. U understood my pblm. I dunno how to use bye array, n frankly speaking i dunno whether it will solve my pblm or not. That large number which i get my converting my secret key is used in the secret shares generation.
f(x)=a0+a1X+a2X.X+a3X.X.X+.....+ak-1X.X.X...X
(k-1) times
is the polynomial function.
a0=Secret key in number
(a1,...ak-1) are randomly generated numbers below 1000.
We need to find the shares by substituting x=1,2...,n in that f(x).
f(1),f(2),....,f(n) will be the secret shares. For their calculation, i have to substitute x=1, x=2,...,x=n in that f(x) equation. So this big number will be added to numbers which contain a max of 8 digits.
Plzzzzzzz tell me how to make it possible? I mean how to use that byte array??