Arjun
Arjun

Reputation: 11

Issue while converting byte[] to string

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." );
    }
}

Upvotes: 0

Views: 768

Answers (2)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

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.

Upvotes: 2

Nartallax
Nartallax

Reputation: 123

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.

Upvotes: 2

Related Questions