Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Base64 byte[] to readable String in java

Tags:

java

base64

I want to convert Base62 Byte array to human readable Stiring

In this code,

I need to convert "[B@913fe2"(the result) to "Hello Wold!".

I viewd several former questions but I don't know how.

package chapter9;

import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.*;
import java.util.Arrays;

import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.cms.CMSProcessable;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;

/**
 * Example of generating a detached signature.
 */
public class SignedDataExample
    extends SignedDataProcessor
{

    public static void main(String[] args)
        throws Exception
    {
        KeyStore        credentials = Utils.createCredentials();
        PrivateKey      key = (PrivateKey)credentials.getKey(Utils.END_ENTITY_ALIAS, Utils.KEY_PASSWD);
    Certificate[]   chain = credentials.getCertificateChain(Utils.END_ENTITY_ALIAS);
    CertStore       certsAndCRLs = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(Arrays.asList(chain)), "BC");
    X509Certificate cert = (X509Certificate)chain[0];

    // set up the generator
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

    gen.addSigner(key, cert, CMSSignedDataGenerator.DIGEST_SHA256);
    gen.addCertificatesAndCRLs(certsAndCRLs);

    // create the signed-data object

    CMSProcessable  data = new CMSProcessableByteArray("Hello World!".getBytes());
    //CMSProcessable  data = new CMSProcessableByteArray(data1.getBytes());

    CMSSignedData signed = gen.generate(data, "BC");

    // recreate
    signed = new CMSSignedData(data, signed.getEncoded());

    //signed.signedContent
    //signed.g
    CMSProcessable S = signed.getSignedContent();
    byte[] K = Base64.decodeBase64((S.getContent()).toString());
    //String K = Base64.decodeBase64(S.getContent());
    //BASE64Decoder.decoder.decodeBuffer()

    // verification step
    X509Certificate rootCert = (X509Certificate)credentials.getCertificate(Utils.ROOT_ALIAS);

    if (isValid(signed, rootCert))
    {
        System.out.println("verification succeeded");
        System.out.println(K);
    }
    else
    {
        System.out.println("verification failed");
    }
}

}

again, the result shows

verification succeeded

[B@913fe2

I need to convert "[B@913fe2"(the result) to "Hello Wold!".

regards.

like image 589
user1349407 Avatar asked Dec 08 '22 19:12

user1349407


1 Answers

Calling toString() on a byte array just prints the type of the array ([B) followed by its hashCode. You want to use new String(byteArray).

You should also consider using an explicit charset instead of the default one:

byte[] array = string.getBytes("UTF8");
String s = new String(array, "UTF8");
like image 145
JB Nizet Avatar answered Dec 11 '22 11:12

JB Nizet