2010-03-29 87 views
0

我目前面临的一个错误称为Bad Base64Coder input character at ...
这是我在Java代码中。错误:错误Base64Coder输入字符

String nonce2 = strNONCE; 
byte[] nonceBytes1 = Base64Coder.decode(nonce2);          
System.out.println("nonceByte1 value : " + nonceBytes1); 

现在的问题是,我得到Bad Base64Coder input character错误和nonceBytes1值打印为空。我试图从Base64Coder解码nonce2。我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; }

+0

为什么你的Base64编码译码如果你自己刚刚创建它作为一个随机字节数组的现时价值? – Thilo 2010-03-29 03:14:02

回答

1

它只是看起来像你混淆了一些独立的概念,是相当新到Java为好。 Base64是一种将“人类不可读”字节数组转换为“人类可读”字符串(编码)和其他方式(解码)的编码类型。它通常用于将二进制数据作为字符传输或存储在严格要求的地方(由于协议或存储类型)。

SecureRandom事情不是编码器或解码器。它返回的值为random,与某个cipherencoder没有任何关系。以下是来自前给予链接一部分摘录:

ran·dom
adj.
1. Having no specific pattern, purpose, or objective


Cipher
In cryptography , a cipher (or cypher) is an algorithm for performing encryption or 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 .

我强烈建议您对齐这些概念为自己(点击链接了解更多关于他们),而不是把他们扔在一个大的同一个洞里。这里的至少一个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)); 
    } 

} 

(使用Commons Codec Base64由路)

下面是输出的一个例子:

 
[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] 
+0

非常感谢你的帮助。我会阅读,虽然东西了解更多。 – 2010-03-29 03:27:52

+0

非常感谢您的善意帮助。坦率地说,我对Java和这些网络安全产品相当陌生。我目前正在一家研究公司实习。基本上,在我的项目中,我完成了从客户端到服务器发送数据的部分。现在是做服务器端。生成一个随机数值并使用该值,我将计算密钥。 服务器端也将响应那个nonce值给客户端。然后,客户端也将计算键值,并尝试匹配之后的值。 – 2010-03-29 03:46:16

+3

有点居高临下的语气。 – RonanOD 2015-08-14 14:43:59

2
//convert byte array to string. 
strNONCE = new String(nonce); 

这是行不通的。你需要base64编码它。

strNONCE = Base64Coder.encode(nonce); 
+0

感谢您的回复。 第二代码正在工作,它将生成一个随机数值。 我现在要做的是计算密钥,完整性密钥和使用该nonce值的响应。我在代码的第一部分遇到问题。谢谢。 – 2010-03-29 02:57:19

+0

第二个代码不工作,因为它从随机字节创建一个字符串。这不是一个有效的字符串,而不是Base64编码(好吧,有时它是...) – Thilo 2010-03-29 03:07:25

+1

嗨蒂罗,再次感谢您的帮助。下面是生成随机数的类。当我打电话给班级时它正在工作。它确实生成一个随机数值。嗯.. – 2010-03-29 03:14:04

1

A base64编码的字符串只能有可打印字符。您正在从随机字节直接生成strNONCE,因此它将包含不可打印的字符。

究竟什么是你正在试图做的?

+0

非常感谢答复,戴夫。 seconde代码正在工作,它将生成一个随机数值。我现在想要做的是使用nonce值,我想要获得另一个字节数组并从Base64Coder解码它。从那我想要计算密钥,完整性密钥等 它是我的项目的一部分。 =) – 2010-03-29 03:01:36

+0

第二个代码不工作,因为它从随机字节创建一个字符串。这不是一个有效的字符串,而不是Base64编码(好吧,有时它是...) – Thilo 2010-03-29 03:07:09