Android 3Des加密二进制输出

时间:2013-11-07 14:29:09

标签: java android base64 3des

用3des解密。我可以正确获得Base64输出,但我想得到输出二进制。我能怎么做?

        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedText = cipher.doFinal(unencryptedString);
        byte[]  sdd = Base64.encode(encryptedText, Base64.DEFAULT);

1 个答案:

答案 0 :(得分:2)

将字节数组转换为包含二进制值的String的简单方法。

String bytesToBinaryString(byte[] bytes){

    StringBuilder binaryString = new StringBuilder();

    /* Iterate each byte in the byte array */
    for(byte b : bytes){

        /* Initialize mask to 1000000 */           
        int mask = 0x80;

        /* Iterate over current byte, bit by bit.*/
        for(int i = 0; i < 8; i++){

            /* Apply mask to current byte with AND, 
             *  if result is 0 then current bit is 0. Otherwise 1.*/
            binaryString.append((mask & b) == 0 ? "0" : "1");

            /* bit-wise right shift the mask 1 position. */
            mask >>>= 1;
        }
    }

    /* Return the resulting 'binary' String.*/
    return binaryString.toString();
}