java 错误:Base64Coder 输入字符错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2535542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Bad Base64Coder input character
提问by sebby_zml
I am currently facing an error called Bad Base64Coder input character at ...
Here is my code in java.
我目前面临一个名为Bad Base64Coder input character at ...
Here is my code in java的错误。
String nonce2 = strNONCE;
byte[] nonceBytes1 = Base64Coder.decode(nonce2);
System.out.println("nonceByte1 value : " + nonceBytes1);
The problem now is I get Bad Base64Coder input charactererror and the nonceBytes1value is printed as null. I am trying to decode the nonce2from Base64Coder. My strNONCEvalue is 16
现在的问题是我收到Bad Base64Coder input character错误并且该nonceBytes1值被打印为空值。我正在尝试解码nonce2来自Base64Coder. 我的strNONCE值是 16
/** Generating nonce value */
public static String generateNonce() {
try {
byte[] nonce = new byte[16];
Random rand;
rand = SecureRandom.getInstance ("SHA1PRNG");
rand.nextBytes(nonce);
//convert byte array to string.
strNONCE = new String(nonce);
}catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strNONCE;
}
/** Generating nonce value */
public static String generateNonce() {
try {
byte[] nonce = new byte[16];
Random rand;
rand = SecureRandom.getInstance ("SHA1PRNG");
rand.nextBytes(nonce);
//convert byte array to string.
strNONCE = new String(nonce);
}catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strNONCE;
}
回答by Thilo
//convert byte array to string.
strNONCE = new String(nonce);
That is not going to work. You need to base64 encode it.
那是行不通的。您需要对它进行 base64 编码。
strNONCE = Base64Coder.encode(nonce);
回答by leedm777
回答by BalusC
It simply look like you're confusing some independent concepts and are pretty new to Java as well. Base64is a type of encoding which converts "human unreadable" byte arrays into "human readable" strings (encoding) and the other way round (decoding). It is usually used to transfer or store binary data as characters there where it is strictly been required (due to the protocol or the storage type).
看起来您似乎混淆了一些独立的概念,而且对 Java 来说也很新。Base64是一种编码,它将“人类不可读”的字节数组转换为“人类可读”的字符串(编码),反之亦然(解码)。它通常用于在严格要求(由于协议或存储类型)的地方将二进制数据作为字符传输或存储。
The SecureRandomthing is not an encoder or decoder. It returns a randomvalue which is in no way to be corelated with a certain cipheror encoder. Here are some extracts from the before given links:
的SecureRandom东西不是编码器或解码器。它返回一个随机值,该值与某个cipher或encoder无关。以下是之前给出的链接的一些摘录:
ran·dom
adj.
1.Having no specific pattern, purpose, or objective
随机的
adj.
1.没有特定的模式、目的或目标
Cipher
In cryptography, a cipher (or cypher) is an algorithmfor performing encryptionor decryption— a series of well-defined steps that can be followed as a procedure.
Encoding
Encoding is the process of transforming information from one format into another. The opposite operation is called decoding.
编码
编码是将信息从一种格式转换为另一种格式的过程。相反的操作称为解码。
I'd strongly recommend you to align those concepts out for yourself (click the links to learn more about them) and not to throw them in one big and same hole. Here's at least an SSCCEwhich shows how you can properly encode/decode a (random) byte array using base64 (and how to show arrays as string (a human readable format)):
我强烈建议您自己整理这些概念(单击链接以了解有关它们的更多信息),而不是将它们扔进一个大坑。这里至少有一个SSCCE,它展示了如何使用 base64 正确编码/解码(随机)字节数组(以及如何将数组显示为字符串(一种人类可读的格式)):
package com.stackoverflow.q2535542;
import java.security.SecureRandom;
import java.util.Arrays;
import org.apache.commons.codec.binary.Base64;
public class Test {
public static void main(String[] args) throws Exception {
// Generate random bytes and show them.
byte[] bytes = new byte[16];
SecureRandom.getInstance("SHA1PRNG").nextBytes(bytes);
System.out.println(Arrays.toString(bytes));
// Base64-encode bytes and show them.
String base64String = Base64.encodeBase64String(bytes);
System.out.println(base64String);
// Base64-decode string and show bytes.
byte[] decoded = Base64.decodeBase64(base64String);
System.out.println(Arrays.toString(decoded));
}
}
(using Commons Codec Base64by the way)
(顺便使用Commons Codec Base64)
Here's an example of the output:
下面是一个输出示例:
[14, 52, -34, -74, -6, 72, -127, 62, -37, 45, 55, -38, -72, -3, 123, 23] DjTetvpIgT7bLTfauP17Fw== [14, 52, -34, -74, -6, 72, -127, 62, -37, 45, 55, -38, -72, -3, 123, 23]

