-1

I'm implementing the following RSA algorithm.

http://www.sanfoundry.com/java-program-implement-rsa-algorithm/ The program picks up a string from an excel file, performs the RSA algorithm and stores that string (string of bytes) into the database.

While decrypting, I need to extract that string of bytes, convert it into a byte array to perform decryption.

 protected String encryptit(String teststring) {
            RSA rsa = new RSA();
            DataInputStream in = new DataInputStream(System.in);
            BigInteger e = rsa.getE();
            //String es=e.toString();
            BigInteger N = rsa.getN();
            BigInteger d=rsa.getD();

            try {
                byte[] encrypted = encrypt(teststring.getBytes(),e,N);
                String encryptString = encrypted.toString();
                return  encryptString;
                }
catch(Exception ex) {
                    System.out.println(ex);
                    return "0";
                }
            }

The above function performs encryption. One of the values that gets stored is [B@37ad7b17

protected String decrypt(String val)
    {
        System.out.println("Value " +val);
        RSA rsa = new RSA();
        BigInteger e = rsa.getE();
        BigInteger N = rsa.getN();
        BigInteger d=rsa.getD();
        try {
            String[] bytesString = val.split(" ");
            byte[] bytes = new byte[bytesString.length];
            for(int i=0;i<bytes.length;i++)
            {
                System.out.println("for loop");
                bytes[i]=Byte.parseByte(bytesString[i]);
            }
            byte[] decrypted= decrypt(bytes,d,N);
            String decryptString = new String(decrypted);
            System.out.println(decryptString);
            return  decryptString;
        }catch(Exception ex) {
            System.out.println(ex);
            return "0";
        }
    }

The above function performs the decryption. And it gives java.lang.NumberFormatException: For input string: "[B@37ad7b17"

What should I do to convert this string of bytes to a byte array.

user3508140
  • 285
  • 2
  • 18
  • Try using method getBytes() Refer http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#getBytes%28%29 – Akshay Aug 30 '17 at 06:08
  • 1
    Possible duplicate of [How to convert Java String into byte\[\]?](https://stackoverflow.com/questions/18571223/how-to-convert-java-string-into-byte) – Flown Aug 30 '17 at 06:09
  • @Akshay using getBytes() gives a junk value – user3508140 Aug 30 '17 at 06:10
  • @Flown I've tried that. Doesn't help. – user3508140 Aug 30 '17 at 06:11
  • In that tutorial they have written a method BytestoString use that – Akshay Aug 30 '17 at 06:11
  • @user3508140 that method is working I just tested – Akshay Aug 30 '17 at 06:14
  • @Akshay That serves the same purpose as .toString(). It converts a byte array to a string of bytes. I need a function to convert that string of bytes back to a byte array. – user3508140 Aug 30 '17 at 06:15
  • 3
    There is no such thing as a string of bytes. A string is made of characters. A These characters can be transformed into bytes using an encoding (CharSet). Random bytes, such as those returned by a cryptographic cipher, can't be transformed to a printable String without using Hex or Base64 encoding. Calling toString() on a byte array makes absolutely no sense: all you'll get is the type of the array ([B) followed by @ followed by the hashCode of the array object. – JB Nizet Aug 30 '17 at 06:24
  • Read https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ – JB Nizet Aug 30 '17 at 06:34

1 Answers1

0

To encode:

byte[] result = ...;

String str = Base64.encodeBase64String(result);

To decode:

byte[] bytes = Base64.decodeBase64(str);

In JDK8: import java.util.Base64;

String str = Base64.getEncoder().encode(result);
byte[] bytes = Base64.getDecoder().decode(str);
Rafael Coelho
  • 166
  • 3
  • 10