0

decrypt with 3des. I can get Base64 output correctly but i want to get output binary. How can i do?

        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encryptedText = cipher.doFinal(unencryptedString);
        byte[]  sdd = Base64.encode(encryptedText, Base64.DEFAULT);
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Zapateus
  • 516
  • 3
  • 8
  • 21
  • Clarify your output requirements. Do you want a String representation of a binary number? – Dev Nov 07 '13 at 14:59

1 Answers1

2

A simple method that turns a byte array in to a String containing the binary value.

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();
}
Dev
  • 11,919
  • 3
  • 40
  • 53