B
B
Billy Milligan2014-05-19 21:33:20
Java
Billy Milligan, 2014-05-19 21:33:20

How to convert byte[] to String and then back to byte[]?

I decided to implement a backup from the androyd database to the server (all data is stored in encrypted form) and so that in which case it would be possible to unload everything from the server back.
I encrypt the string like this:

public static byte[] encrypt(String text) throws Exception {
        byte[] clear = text.getBytes("UTF-8");
        IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        sr.setSeed(password);
        kgen.init(256, sr);
        SecretKey skey = kgen.generateKey();
        byte[] key = skey.getEncoded();

        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

After that, I need to send it to the server by POST, but the JSON library function for sending the request only accepts a String. So I first tried to do this:
byte[] passBytes = new byte[0];

        try {
            passBytes = mAES.encrypt("test");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            a = mAES.decrypt(passBytes.toString().getBytes(Charset.forName("UTF-8"))); //без параметра charset тоже пробовал
        } catch (Exception e) {
            e.printStackTrace();
        }

Unable to decrypt, throws an exception.
When encrypting / decrypting byte[] everything is fine.
I've been struggling with this for 3 days now.
Also tried to translate like this.
byte[] passBytes = new byte[0];
        StringBuffer sb = new StringBuffer();

        try {
            passBytes = mAES.encrypt("text");
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i = 0; i < passBytes.length; i++) {
            sb.append(Integer.toString((passBytes[i] & 0xff) + 0x100, 16).substring(1));
        }

        try {
            a = mAES.decrypt(sb.toString().getBytes(Charset.forName("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Billy Milligan, 2014-05-19
@Billy_Milligan

I found a solution, maybe it will be useful for someone.

public Test () {
        String a = "";
        try {
            a = bytesToHex(mAES.encrypt("test"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            a = mAES.decrypt(hexToBytes(a));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public static String bytesToHex(byte[] data) {
        if (data==null)
        {
            return null;
        }

        int len = data.length;
        String str = "";
        for (int i=0; i<len; i++) {
            if ((data[i]&0xFF)<16)
                str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
            else
                str = str + java.lang.Integer.toHexString(data[i]&0xFF);
        }
        return str;
    }


    public static byte[] hexToBytes(String str) {
        if (str==null) {
            return null;
        } else if (str.length() < 2) {
            return null;
        } else {
            int len = str.length() / 2;
            byte[] buffer = new byte[len];
            for (int i=0; i<len; i++) {
                buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
            }
            return buffer;
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question