0

I have a problem with converting byte[] to string. I have encrypted using encryptText(), it returns byte[]. And then I'm passing byte[] to convert to string using byteToString().

Converting byte[] to String:

    s = bytesToString(cipherText);      //junk value getting here, i'm expecting 

same encrypted value here even after converting byte[] to string

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.prefs.Preferences;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;

public class Test {
    private Test() {  } 
    /**
     * gets the AES encryption key. 
     * @return
     * @throws Exception
     */
    public static SecretKey getSecretEncryptionKey() throws Exception 
    {
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128); 
        SecretKey secKey = generator.generateKey();
        return secKey;
    } 
    /**
     * Encrypts password in AES using the secret key.
     * @param passWord
     * @param secKey
     * @return
     * @throws Exception
     */
    public static byte[] encryptText(String passWord,SecretKey secKey) throws Exception 
    {        
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
        byte[] byteCipherText = aesCipher.doFinal(passWord.getBytes());
        return byteCipherText;
    }
    /**
     * Decrypts encrypted byte array using the key used for encryption.
     * @param byteCipherText
     * @param secKey
     * @return
     * @throws Exception
     */
    public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws Exception 
    {
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secKey);
        byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
        return new String(bytePlainText);
    }
    //converting byte[] to string
    private static String bytesToString(byte[] bytesArray)
    {         
        StringBuffer stringBuffer = new StringBuffer();         
        for (int i = 0; i < bytesArray.length; i++) {             
            stringBuffer.append((char) bytesArray[i]);         
        }         
        return stringBuffer.toString();     
    }

    public static void main(String args[]) throws Exception 
    {
        SecretKey secKey = getSecretEncryptionKey();        
        String s = null;        
        String Username = null;
        String Password = null;     
        String value = null;    
        try 
        {
            if(args[0] != null)
                Username = args[0];
            if(args[1] != null)
                Password = args[1];     
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("ArrayIndexOutOfBoundsException caught");
        }
        finally {           
        } 
        byte[] cipherText = encryptText(Password, secKey);
        s = bytesToString(cipherText);      //junk value getting here, i'm expecting same encrypted value here even after converting byte[] to string
        System.out.println("Encrypted cipherText = " + cipherText);
        System.out.println("Encrypted Password = " + s);        
        System.out.println("Done." );
    }
}
Markus Mitterauer
  • 1,560
  • 1
  • 14
  • 28
Arjun
  • 11
  • 2
  • 4
    Possible dupe http://stackoverflow.com/q/6684665/4285122 – Brian Gerhards Sep 15 '16 at 11:14
  • 2
    Wait, why you dont just `new String(bytesArray)`? And don't use `StringBuffer` unless you need synchronized functionality, if not then use `StringBuilder`. – Shadov Sep 15 '16 at 11:17
  • Encrypted data in a byte array is not human-readable, while Strings represent human-readable Unicode text. Why do you want to convert. – Joni Sep 15 '16 at 11:21
  • I wonder how you determine that a piece of encrypted data is "junk value". Do you perhaps have encryption which transforms a piece of legible text into another piece of legible text? Never heard. – Marko Topolnik Sep 15 '16 at 11:24
  • What exactly is your issue? – Martin Nyolt Sep 15 '16 at 13:05
  • i have added my full code and written comment where exact issue getting. Plz do the needful. – Arjun Sep 15 '16 at 13:58
  • Well, when I look at `System.out.println("Encrypted cipherText = " + cipherText);`, then I hope that you don't think that *"Encrypted cipherText = [B@78308db1"* shows something encrypted. – Tom Sep 15 '16 at 14:47

2 Answers2

2

Short answer: use new String(bytes, "utf8") (or any other charset bytes of which you have).

But in your case bytes returned by encryption function may be impossible to convert to utf8-string. You can't just take arbitrary bytes and convert it to string because some byte sequences could not be interpreted as valid utf8.

You may want to use some single-byte charset to solve the problem. But I generally recommend not to convert to string bytes that are not meant to be string.

Nartallax
  • 123
  • 6
2

An encrypted content is not meant to be read by a human being it is a pure binary content, but if you really need to make it readable you should encode it to an hexadecimal value using Apache Commons Codec

Your method would then be:

private static String bytesToString(byte[] bytesArray){ 
   return Hex.encodeHexString(bytesArray); 
}

Another approach could be to append each byte directly to stringBuffer instead of casting them as a char first as next:

private static String bytesToString(byte[] bytesArray) {
    StringBuilder stringBuffer = new StringBuilder();
    for (int i = 0; i < bytesArray.length; i++) {
        stringBuffer.append(bytesArray[i]);
    }
    return stringBuffer.toString();
}

s = new String(cipherText, "utf8"); is not working!

It cannot work because cipherText is not the UTF8 encoded version of a given String but an array of bytes corresponding to an encrypted content. In other words it is somehow equivalent to encode your String with a given algorithm and try to decode it using the decode function of a totally different algorithm, it simply cannot work.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122