在Java中将Base64字节数组转换为可读字符串

4

我希望将Base62字节数组转换为易读字符串。

在这段代码中,

我需要将"[B@913fe2"(结果)转换成"Hello World!"。

我查看了几个之前的问题,但我不知道如何做。

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");
    }
}

再次,结果显示

验证成功

[B@913fe2

我需要将“[B@913fe2”(结果)转换为“Hello World!”。

谢谢。

3个回答

12

对一个字节数组调用 toString() 方法仅仅会打印数组的类型 ([B) 及其哈希码。你应该使用 new String(byteArray)

此外,你还应该考虑使用明确的字符集而不是默认字符集:

byte[] array = string.getBytes("UTF8");
String s = new String(array, "UTF8");

很难说为什么,因为我们不知道您的CMSXxx对象是做什么的,S.getContent()的类型是什么,以及Base64用于编码的位置。尝试使用字符串、字节数组和base64编码/解码进行实验。然后再添加加密功能。 - JB Nizet
谢谢,JB Nizet。您不需要了解CMSXxx对象以及S.getContent()。我所需要做的就是将[B@eb7859转换为Hello World! - user1349407
1
你做不到。因为如果你再次阅读我的回答并试着理解它,你最终会明白[B@eb7859并没有代表任何有意义的东西。你需要转换成字符串的是一个字节数组。[B@eb7859只是字节数组的类型和哈希码。停止调用toString()或打印字节数组。 - JB Nizet

3

你应该使用

byte[] k = Base64.decodeBase64((S.getContent()).toString());
String asString = new String(k);

无法转换为字符串。 - user1349407
第一行还是第二行? - onon15
S.getContent().toString() 是什么?我使用有效的 base64 字符串("SGVsbG8sIHdvcmxkIQ==")测试了我的代码,它可以正常工作,所以问题可能出在你的 base64 源上。 - onon15
感谢onon15。byte[] K = Base64.decodeBase64((S.getContent()).toString()); 并且 System.out.println(K); 显示 [B@eb7859。那么有没有办法将 [B@eb7859 转换为“你好,世界”? - user1349407
你正在打印一个字节数组。请再次阅读我的答案。这将打印出数组的类型和其哈希码。要将包含编码字符的字节数组转换为字符串,必须使用 new String(byteArray); - JB Nizet

-1

你必须使用这段代码

CMSSignedData signeddata = new CMSSignedData(signedBytes);
CMSProcessable cmsdata = signeddata.getSignedContent();
System.out.println(new String((byte[]) s.getContent()));

这对我很有用。 我正在使用BouncyCastle,尝试从PKCS7签名数据中获取原始数据。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接