gpt4 book ai didi

java - 将 Base64 byte[] 转换为 java 中的可读字符串

转载 作者:搜寻专家 更新时间:2023-11-01 01:17:41 26 4
gpt4 key购买 nike

我想将 Base62 字节数组转换为人类可读的 Stiring

在这段代码中,

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

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

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 Wold!”。

问候。

最佳答案

在字节数组上调用 toString() 只是打印数组的类型 ([B),然后是它的 hashCode。你想使用 new String(byteArray)

您还应该考虑使用显式字符集而不是默认字符集:

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

关于java - 将 Base64 byte[] 转换为 java 中的可读字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13549783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com