3 Replies Latest reply on Oct 16, 2006 3:48 PM by JosAH

# BigInteger to Byte conversion

Hi to all,
I have written a code for rsa encryption. My intention is to encrypt/decrypt a file. So it accepts a byte array and encrypts each byte. Now the problem i face is that, when i convert it to a byte it sometimes returns negative values. Even if it is a positive value, on decrypting the byte i don't get the exact value. My code works only when i give the values for p and q as 7 and 17 respectively. And even in this case it doesn't work for characters having their values above 119(n=119 in this case). I need help urgently. Please some one help me. Thanking you in advance.. My code is as follows

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

public class rsa{

static DataInputStream dis;
static BigInteger n,p,q,d,e,n1,byte_value;
static BigInteger public_key,secret_key;

public rsa()
{
dis=new DataInputStream(System.in);
}

public static void assign_key()
{
try
{

System.out.println("Enter two prime no.s p and q ");

}
catch(Exception e) {}
}

public static void key_generator()
{
n=p.multiply(q);
n1=(p.subtract(BigInteger.ONE))
.multiply(q.subtract(BigInteger.ONE));

BigInteger i=new BigInteger("2");

while(n1.gcd(i).intValue() > 1)
public_key=i;
secret_key = i.modInverse(n1);

System.out.println("\n n = "+n);
System.out.println("\n Secret key = "+secret_key);
System.out.println("\n Public key = "+public_key);

}

public static void get_key()
{
try
{
System.out.println("\n Enter the value of n ");
System.out.println("Enter the secret key ");
}
catch(Exception e) {}
}

public static byte [] encrypt(byte plain_data[])
{

assign_key();
key_generator();
int size=plain_data.length;

byte cipher_data[]=new byte[size];
BigInteger a,b;

for(int i=0;i<size;i++)
{

a=BigInteger.valueOf(plain_data);
b=a.modPow(public_key,n);
cipher_data[i]=b.byteValue();

}

return(cipher_data);

}

public static byte [] decrypt(byte cipher_data[])
{
get_key();
int size=cipher_data.length;

byte plain_data[]=new byte[size];
BigInteger a,b;

for(int i=0;i<size;i++)
{
a=BigInteger.valueOf(cipher_data[i]);
b=a.modPow(secret_key,n);

plain_data[i]=b.byteValue();
}

return(plain_data);

}

}
• ###### 1. Re: BigInteger to Byte conversion
Please post code wrapped in [co[b]de]  tags, otherwise it is extremely hard to read.

Your code won't even compile, because you're trying to assign a byte to an array of bytes in a couple of places. You probably wanted to use BigInteger.toByteArray instead of BigInteger.byteValue.

Also, you are trying to create a new BigInteger from a byte array using BigInteger.valueOf, but that method takes a long. One of the BigInteger constructors takes a byte array, so use that instead.
• ###### 2. Re: BigInteger to Byte conversion
Sir thanks for your reply..I am sorry i've pasted the wrong code...Below is my correct code... i am not trying to create a BigInteger from a byte array. i need a BigInteger for each of those byte values.My intension is not converting a byte array into a BigInteger and vice versa. I want a single byte to be converted into BigInteger, perform operations on it and then reconvert the BigInteger into a single byte value. Here is where i face the problem. Please help me out...My code is as follows

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

public class rsa{

static DataInputStream dis;
static BigInteger n,p,q,d,e,n1,byte_value;
static BigInteger public_key,secret_key;

public rsa()
{
dis=new DataInputStream(System.in);
}

public static void assign_key()
{
try
{

System.out.println("Enter two prime no.s p and q ");

}
catch(Exception e) {}
}

public static void key_generator()
{
n=p.multiply(q);
n1=(p.subtract(BigInteger.ONE))
.multiply(q.subtract(BigInteger.ONE));

BigInteger i=new BigInteger("2");

while(n1.gcd(i).intValue() > 1)
public_key=i;
secret_key = i.modInverse(n1);

System.out.println("\n n = "+n);
System.out.println("\n Secret key = "+secret_key);
System.out.println("\n Public key = "+public_key);

}

public static void get_key()
{
try
{
System.out.println("\n Enter the value of n ");
System.out.println("Enter the secret key ");
}
catch(Exception e) {}
}

public static byte [] encrypt(byte plain_data[])
{

assign_key();
key_generator();
int size=plain_data.length;

byte cipher_data[]=new byte[size];
BigInteger a,b;

for(int i=0;i<size;i++)
{

a=BigInteger.valueOf(plain_data);
b=a.modPow(public_key,n);
cipher_data=b.byteValue();

}

return(cipher_data);

}

public static byte [] decrypt(byte cipher_data[])
{
get_key();
int size=cipher_data.length;

byte plain_data[]=new byte[size];
BigInteger a,b;

for(int i=0;i<size;i++)
{
a=BigInteger.valueOf(cipher_data);
b=a.modPow(secret_key,n);

plain_data[i]=b.byteValue();
}

return(plain_data);

}

}

Message was edited by:
sssmani

Message was edited by:
sssmani
• ###### 3. Re: BigInteger to Byte conversion
No cigar for you because your post ended in italics gibberish again.