我有sun.misc.BASE64Encoder
的以下代码:
BASE64Decoder decoder = new BASE64Decoder();
byte[] saltArray = decoder.decodeBuffer(saltD);
byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);
并希望将其转换为org.apache.commons.codec.binary.Base64
。我已经浏览了API,文档等,但我找不到似乎匹配的东西并给出相同的结果值。
答案 0 :(得分:13)
实际上几乎完全相同:
Base64 decoder = new Base64();
byte[] saltArray = decoder.decode(saltD);
byte[] ciphertextArray = decoder.decode(ciphertext);
用于解码:
String saltString = encoder.encodeToString(salt);
String ciphertextString = encoder.encodeToString(ciphertext);
最后一个更难,因为你最后使用“toString”。
答案 1 :(得分:6)
您可以使用decodeBase64(byte[] base64Data)或decodeBase64(String base64String)方法。例如:
byte[] result = Base64.decodeBase64(base64);
这是一个简短的例子:
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
public class TestCodec {
public static void main(String[] args) throws IOException {
String test = "Test BASE64Encoder vs Base64";
// String encoded = new BASE64Encoder().encode(test.getBytes("UTF-8"));
// byte[] result = new BASE64Decoder().decodeBuffer(encoded);
byte[] encoded = Base64.encodeBase64(test.getBytes("UTF-8"));
byte[] result = Base64.decodeBase64(encoded);
System.out.println(new String(result, "UTF-8"));
}
}
答案 2 :(得分:3)
而不是这两个类(import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder),你可以使用 java.util.Base64 class.Now改变编码和解码方法如下。 对于编码:
String ciphertextString = Base64.getEncoder().encodeToString(ciphertext);
用于解码:
final byte[] encryptedByteArray = Base64.getDecoder().decode(ciphertext);
此处密文是编码方法中的编码密码。
现在一切都完成了,你可以保存程序并运行。它会在没有显示任何错误的情况下运行。