cipherText = new BASE64Encoder().encode(encrypted);和这句有什么区别
时间: 2024-03-31 15:32:18 浏览: 27
这两句代码的作用是相同的,都是将字节数组 `encrypted` 使用 Base64 编码转换成字符串形式的密文,并将其赋值给 `cipherText` 变量。但是,它们使用了不同的类来进行 Base64 编码。
`cipherText = Base64.encodeBase64String(encrypted)` 使用的是 Apache Commons Codec 库中的 `Base64` 类。
而 `cipherText = new BASE64Encoder().encode(encrypted)` 则使用的是 Java 标准库中的 `BASE64Encoder` 类。需要注意的是,`BASE64Encoder` 类是 Java 8 之前版本提供的,从 Java 8 开始,它已经被标记为过时,推荐使用 `java.util.Base64` 类进行 Base64 编码。
因此,建议使用 `Base64.encodeBase64String(encrypted)` 或 `Base64.getEncoder().encodeToString(encrypted)` 进行 Base64 编码,不建议使用 `BASE64Encoder` 类。
相关问题
byte[] encrypted = Util.hexToByte(cipherText); cipherText=Base64.encodeBase64String(encrypted);; //cipherText = new BASE64Encoder().encode(encrypted); if (cipherText != null && cipherText.trim().length() > 0) { Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(cipherText); cipherText = m.replaceAll(""); }
这段代码是用来将加密后的密文转换成 Base64 编码格式,并且将密文中的空格、制表符、回车符、换行符等无关字符去掉。具体来说,它首先将密文从十六进制格式转换成字节数组,然后使用 Base64 编码将字节数组转换成字符串形式的密文,并将其赋值给 cipherText 变量。接着,它使用正则表达式将密文中的无关字符去掉,最终得到处理后的密文。这段代码的作用可能是为了将密文进行格式化,以便于后续的处理。
cipherText=Base64.encodeBase64String(encrypted);该句代码有错误吗
这句代码看起来没有语法错误,它是将字节数组 `encrypted` 使用 Base64 编码转换成字符串形式的密文,并将其赋值给 `cipherText` 变量。但是,需要注意的是,这里使用的 `Base64.encodeBase64String()` 方法来自于 Apache Commons Codec 库,如果没有导入该库,则会编译错误。如果你没有导入该库,可以通过以下方式导入:
```
import org.apache.commons.codec.binary.Base64;
```
另外,如果你使用的是 Java 8 或更高版本,也可以使用 Java 内置的 Base64 类来进行编码,不需要导入额外的库:
```
import java.util.Base64;
```
使用方法类似:
```
cipherText = Base64.getEncoder().encodeToString(encrypted);
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![jar](https://img-home.csdnimg.cn/images/20210720083455.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)