Encode a string in BigInteger, then decode back to String

I found an answer that almost solves my problem: stack overflow

This answer demonstrates how to encode BigInteger to String and then back to BigInteger using Base64 encodings, which uses the Apache codec.

Is there any coding method / method method for String for BigInteger and then back to String? if so is anyone please explain how to use it?

String s = "hello world"; System.out.println(s); BigInteger encoded = new BigInteger( SOME ENCODING.(s)); System.out.println(encoded); String decoded = new String(SOME DECODING.(encoded)); System.out.println(decoded); 

Print

  hello world 830750578058989483904581244 hello world 

(The result is just an example, and the hi world does not need to be decrypted for this BigInteger)

EDIT

More specific:

I am writing an RSA algorithm, and I need to convert the message to BigInteger so that I can then encrypt the message with the public key (send the message), and then decrypt the message with the private key, and then convert the number back to string.

I need a conversion method that could create the smallest BigInteger, since I planned to use the binary until I realized how ridiculous the number would be.

+4
source share
1 answer

I don’t understand why you want to go through complex methods, BigInteger already compatible with String :

 // test string String text = "Hello world!"; System.out.println("Test string = " + text); // convert to big integer BigInteger bigInt = new BigInteger(text.getBytes()); System.out.println(bigInt.toString()); // convert back String textBack = new String(bigInt.toByteArray()); System.out.println("And back = " + textBack); 

** Change **

But why do you need BigInteger when you can work directly with bytes, such as DNA ?

+8
source

Source: https://habr.com/ru/post/1399032/


All Articles