How do I convert a char into a BigInteger and vice versa?

• November 12th, 2013, 06:00 PM
ineedahero
How do I convert a char into a BigInteger and vice versa?
I'm trying to do an encryption. I have this message: "hello world"

I am supposed to encrypt and decrypt that character by character.

So, the letter 'h' as a char may equal 97, and so on.

I want to make 97 a BigInteger, and then perform arithmetic on that to encrypt it.

I can turn the String into a char array.
I can turn the char array into an int array.
I cannot turn the int array into a BigInteger array.

Mind you, I need to use the BigInteger.modPow(e,n) function in order to perform the encryption, so it must ultimately become a BigInteger.

And, even if I did make the conversion, how would I convert it back?

Looked everywhere online for over an hour, couldn't find anything that makes sense.

• November 12th, 2013, 06:16 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
Quote:

I am supposed to encrypt and decrypt that character by character.
I don't understand why you are using BigInteger if the task does it one character at a time.
What is done with the encrypted character?
How are the encrypted characters saved? In an array?
• November 12th, 2013, 06:29 PM
ineedahero
Re: How do I convert a char into a BigInteger and vice versa?
Okay, for encryption, we use this formula:

c = m^e mod n
m is the "message".
We need a BigInteger.modPow(e,n) to calculate this.

So, the 'm' must be a BigInteger.

I intend to convert each character of the String into a BigInteger and perform the arithmetic, creating an array of BigIntegers the size of the length of the original message, where each entry in the array represents a character of the message.

And then convert back from that. Make sense?

--- Update ---

Though I'm sure there's a simpler way to do it character by character!
• November 12th, 2013, 06:37 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
What does the BigInteger constructor take to create an instance of BigInteger?
What technique can you use to convert a character to what the constructor takes?
• November 12th, 2013, 06:45 PM
ineedahero
Re: How do I convert a char into a BigInteger and vice versa?
Thanks for the response!

I found this:

BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger.

But I don't really know what the "decimal String representation is", and that doesn't seem to convert character by character.

I also found this:

BigInteger(byte[] val)
Translates a byte array containing the two's-complement binary representation of a BigInteger into a BigInteger.

But the two's complement binary representation kind of scares me. Plus I don't even know what a Byte array is....though it seems like that's the one I should use.

But...I'm creating a single BigInteger, not an array of BigIntegers, right? How exactly does that represent the String?
• November 12th, 2013, 06:54 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
Quote:

BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger.
That looks promising. Go read the details for that one.

Try some experiments using Strings of decimal digits: "56" or "65" and see what it does when you use some of the methods to add etc two BIs
• November 12th, 2013, 07:05 PM
ineedahero
Re: How do I convert a char into a BigInteger and vice versa?
So I can pass the BigInteger function a String....but the String has to contain numbers within it?

But I want to pass a String that is a message, not a String of numbers.

Do I need to take the message, make it an array of chars, make the chars into ints, and then make the char array into a String? That seems pretty convoluted, and it doesn't convert character by character.
• November 12th, 2013, 07:18 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
Quote:

pass the BigInteger function a String....but the String has to contain numbers within it
Is that what the doc said? Did you write a small program to create two BIs , add them together and print out the results to see how they worked?

The next step will be to convert a character to its int value and then that to a String of digits for the constructor.
• November 12th, 2013, 07:31 PM
ineedahero
Re: How do I convert a char into a BigInteger and vice versa?
Yeah, I made that test program and it worked only when I passed a String like "45" or "439", not when I passed "hello" or "world".

I also have this code, which is converting character to int value and then that to a String of digits for the constructor, but it won't compile:

Code :

```public void Encrypt() { int[] numArray = new int[message.length()]; char[] charArray = new char[message.length()];   charArray = message.toCharArray();   System.out.println("Encrypted message: ");   for (int i = 0; i < message.length(); i++) { numArray[i] = charArray[i]; }   String str = Arrays.toString(numArray); BigInteger BI = new BigInteger(str);     }```
• November 12th, 2013, 07:46 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
Quote:

it won't compile:
Please copy the full text of the error message and paste it here. It has important info about the error.

Have you tried a simple test program creating some BIs and using some methods and printing out the results?
• November 12th, 2013, 08:05 PM
ineedahero
Re: How do I convert a char into a BigInteger and vice versa?
I got it compiling, but now it has this error during runtime:
HTML Code:

```Encrypted message: Exception in thread "main" java.lang.NumberFormatException: For input string: "[104, 1"         at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)         at java.lang.Integer.parseInt(Integer.java:492)         at java.math.BigInteger.<init>(BigInteger.java:338)         at java.math.BigInteger.<init>(BigInteger.java:476)         at RSACipher1.Encrypt(RSACipher1.java:134)         at RSACipher1.main(RSACipher1.java:44)```
And yeah, I'll do some testing right now.

--- Update ---

And this is line 44:

BigInteger BI = new BigInteger(str);

--- Update ---

Okay, and I tried outputting the String "str" to the screen...and I got this:
str = [108, 111, 107]

So obviously that's no good lol.

But then how am I supposed to be making these conversions???
• November 12th, 2013, 08:16 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
Quote:

how am I supposed to be making these conversions???
I suppose, one char at a time.

For testing I'd use a very short String, 1 or two characters, so the results can be easily verified manually.
• November 12th, 2013, 08:42 PM
ineedahero
Re: How do I convert a char into a BigInteger and vice versa?
Okay, I finally got the little bastard to work.

Thanks for all the help.
• November 12th, 2013, 08:53 PM
Norm
Re: How do I convert a char into a BigInteger and vice versa?
Glad you got it going.