-1

Please don't mark it as duplicate.

I am using Blowfish Algorithm to Encrypt and Decrypt a Contents in a File. The problem is the output characters of encrypted file having too much of special characters

SPECIAL CHARACTERS (Encrypted text) :- Ž81‡Ç¬ú²±DN¢HÜ4ºÜºT÷.Ê+ŠÀ

after that i had converted the special characters to hex it gives the value as

8E383187C7ACFAB214B113444EA248DC34BADCBA54F7032ECA2B8AC03C6B95C67D2F46F586E4CD86

if i tried to convert this to string i cant get the same encrypted text

Following java code for converting hex to string

package bytearray;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;



public class ByteArray {


    public static byte[] toByteArray(String s) {
        return DatatypeConverter.parseHexBinary(s);
    }

    public static void main(String[] args) throws IOException {
        String balaji  ="8E383187C7ACFAB214B113444EA248DC34BADCBA54F7032ECA2B8AC03C6B95C67D2F46F586E4CD86";

        byte[] pri = toByteArray(balaji);
        String value = new String(pri);
        System.out.println(value);


    }

}

Please help us how to convert the hex to string.

Expected Output :- Ž81‡Ç¬ú²±DN¢HÜ4ºÜºT÷.Ê+ŠÀ

Obtained output:- �81�Ǭ���DN�H�4�ܺT�.�+��

Balaji
  • 73
  • 1
  • 6
  • 1
    Don't convert random bytes to String with new String(). That makes no sense. Transform the clear-text to bytes using getBytes("UTF8"), then encrypt those bytes. The result is a byte array. If you want a printable version of that byte array, encode it using Hex or Base64. – JB Nizet Mar 15 '17 at 17:12
  • You already see it will likely be closed as a duplicate and, at least to me, you didn't make it clear why it *isn't* a duplicate. – Maarten Bodewes Mar 15 '17 at 19:01

1 Answers1

1

you need to make sure that converting to and from HEX uses same encoding. in this case I believe it should be UTF-8

public static String toHexString(String arg,String encoding) throws Exception {

    return String.format("%040x", new BigInteger(1, arg.getBytes(encoding)));
}



public static byte[] toByteArray(String s) {

    return DatatypeConverter.parseHexBinary(s);
}



public static void main(String[] args)throws Exception{

    String s1 = "Ž81‡Ç¬ú²±DN¢HÜ4ºÜºT÷.Ê+ŠÀ";
    System.out.println(s1);

    String s2 = toHexString(s1,"UTF-8");
    System.out.println(s2);

    String s3 = new String(toByteArray(s2));
    System.out.println(s3);


    String s4 = toHexString(s1,"ISO-8859-15");
    System.out.println(s4);

    String s5 = new String(toByteArray(s4));
    System.out.println(s5);

}

OUTPUT:

Ž81‡Ç¬ú²±DN¢HÜ4ºÜºT÷.Ê+ŠÀ

c5bd3831e280a1c387c2acc3bac2b2c2b1444ec2a248c39c34c2bac39cc2ba54c3b72ec38a2bc5a0c380

Ž81‡Ç¬ú²±DN¢HÜ4ºÜºT÷.Ê+ŠÀ

b438313fc7acfab2b1444ea248dc34badcba54f72eca2ba6c0

�81?Ǭ���DN�H�4�ܺT�.�+��

As you see in the output when I use UTF-8 for both encode and decode I get right result but when I encode with ISO and decode with UTF-8 I get what you get.

nafas
  • 5,283
  • 3
  • 29
  • 57