3

I have to store a string (non-numeric, can contain any UTF8 Chars) in a BigInteger to perform some Mathematical operations with it. I need the conversion to be deterministic which

BigInteger mybigint = new BigInteger(mystring.getBytes());

does not seem to be…

Also I need to be able to convert it back from BigInteger to String. If I convert a String to BigInteger and back it needs to be identical afterwards.

Does anyone have an Idea how to do that? Thank you in advance!

4
  • 3
    How can a non-numeric string be called an integer? Commented Jun 14, 2012 at 13:41
  • 3
    What "mathematical operations" are you talking about? What's "HAPPY" * 2? Commented Jun 14, 2012 at 13:42
  • If you're trying to do some kind of encryption, do the first part (conversion to bytes) but don't use big integers, there are many standard ways of transforming byte arrays. Commented Jun 14, 2012 at 14:08
  • I need the String as in BigInteger because I have to use a given class that calculates a (mathematical) commitment to the string. This is needed for a proof that it was this string that was given… Only the "original" BigInteger will be converted back to a string so that should not be a problem. Commented Jun 14, 2012 at 16:36

2 Answers 2

6

Don't do this. Fundamentally you don't have numeric information. Don't pretend it's numeric information.

Define what you mean by "mathematical operations" and write a class to perform them. BigInteger is almost certainly the wrong representation here.

Sign up to request clarification or add additional context in comments.

Comments

3

This IS deterministic :

BigInteger mybigint = new BigInteger(mystring.getBytes("UTF-8"));

And you can revert it using

new String(mybigint.toByteArray(), "UTF-8");

I can't exclude there is some kind of usefulness to this process but if you're not sure don't hesitate to mention why you do this.

6 Comments

Keep in mind, that not all byte sequences are valid UTF-8 strings. You will not be able to convert every BigInteger to String.
Yes, that's an important precision. Most aren't valid. But I can't imagine for now why you would do math operations on the big integers and then try to make a new string with the result. I always find it hard to say "Don't do it" before I see the rationale but I'm as doubtful as everybody seems to be.
Good. Don't forget to accept the answer. And we're curious about the need : could you explain it ? :)
I need the String as a BigInteger because I have to use a given class that calculates a (mathematical) commitment to the string. This is needed to be able to publish a commitment to the string without revealing the string itself. Converting back to String is not actually needed but makes it far easier because I can't store a string where I would like to, but I can reconvert the BigInteger…
Ok. That makes sense but maybe you should use a hash function. That's not revertible but that makes it more secure. Read for example that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.