[英]Convert Base64 byte[] to readable String in java
I want to convert Base62 Byte array to human readable Stiring 我想将Base62 Byte数组转换为人类可读的Stiring
In this code, 在这段代码中,
I need to convert "[B@913fe2"(the result) to "Hello Wold!". 我需要将“[B @ 913fe2”(结果)转换为“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");
}
}
} }
verification succeeded 验证成功
I need to convert "[B@913fe2"(the result) to "Hello Wold!". 我需要将“[B @ 913fe2”(结果)转换为“Hello Wold!”。
regards. 问候。
Calling toString()
on a byte array just prints the type of the array ( [B
) followed by its hashCode. 在字节数组上调用
toString()
只会打印数组的类型( [B
]后跟其hashCode)。 You want to use new String(byteArray)
. 您想使用
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");
You should use 你应该用
byte[] k = Base64.decodeBase64((S.getContent()).toString());
String asString = new String(k);
You must use this code 您必须使用此代码
CMSSignedData signeddata = new CMSSignedData(signedBytes);
CMSProcessable cmsdata = signeddata.getSignedContent();
System.out.println(new String((byte[]) s.getContent()));
It work for me. 它对我有用。 I'm using BouncyCastle and trying to get original data from PKCS7 signed data.
我正在使用BouncyCastle并尝试从PKCS7签名数据中获取原始数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.