0

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

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

1 回答 1

2

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

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();
}
于 2013-11-07T15:04:48.160 回答