它只是看起来像你混淆了一些独立的概念,是相当新到Java为好。 Base64是一种将“人类不可读”字节数组转换为“人类可读”字符串(编码)和其他方式(解码)的编码类型。它通常用于将二进制数据作为字符传输或存储在严格要求的地方(由于协议或存储类型)。
的SecureRandom
事情不是编码器或解码器。它返回的值为random,与某个cipher或encoder没有任何关系。以下是来自前给予链接一部分摘录:
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]
为什么你的Base64编码译码如果你自己刚刚创建它作为一个随机字节数组的现时价值? – Thilo 2010-03-29 03:14:02