java Base64 解密
时间: 2024-05-19 08:06:15 浏览: 7
以下是 Java 中 Base64 解密的示例代码:
```java
import java.util.Base64;
public class Base64DecoderExample {
public static void main(String[] args) {
String encodedString = "SGVsbG8gV29ybGQh";
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println(decodedString);
}
}
```
在上面的例子中,我们使用 `java.util.Base64` 类中的 `getDecoder()` 方法获取解码器对象,然后使用 `decode()` 方法将 Base64 编码的字符串解码为字节数组。最后,我们将字节数组转换为字符串并打印输出。
相关问题
java base64解密
Java中的Base64解密可以使用JDK工具包中提供的Base64特性或者apache.commons-codex包中提供的Base64方法。以下是两种方法的示例代码:
方法一:使用JDK工具包中提供的Base64特性
```java
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Base64Util {
final static Base64.Decoder decoder = Base64.getDecoder();
public static String decode(String encodedText) {
return new String(decoder.decode(encodedText), StandardCharsets.UTF_8);
}
public static void main(String[] args) {
String encodedStr = "dGVzdDEyMzQ=";
System.out.println("解码后的字符串为:");
System.out.println(Base64Util.decode(encodedStr));
}
}
```
方法二:使用apache.commons-codex包中提供的Base64方法
```java
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
//base64 解码
public static String decode(String encodedText) {
return new String(Base64.decodeBase64(encodedText.getBytes()));
}
public static void main(String[] args) {
String encodedStr = "dGVzdDEyMzQ=";
System.out.println("解码后的字符串为:");
System.out.println(Base64Util.decode(encodedStr));
}
}
```
java base64加解密
Java中自带了Base64加解密的类库,可以直接使用。
加密示例代码:
```java
import java.util.Base64;
public class Base64Demo {
public static void main(String[] args) {
String str = "Hello, world!";
String encodedStr = Base64.getEncoder().encodeToString(str.getBytes());
System.out.println(encodedStr);
}
}
```
解密示例代码:
```java
import java.util.Base64;
public class Base64Demo {
public static void main(String[] args) {
String encodedStr = "SGVsbG8sIHdvcmxkIQ==";
byte[] decodedBytes = Base64.getDecoder().decode(encodedStr);
String decodedStr = new String(decodedBytes);
System.out.println(decodedStr);
}
}
```
以上代码中,我们使用了`Base64.getEncoder()`和`Base64.getDecoder()`方法获取加密和解密实例,然后分别调用`encodeToString()`和`decode()`方法进行加解密。注意,加密方法返回的是字符串类型,而解密方法返回的是字节数组类型,需要将其转换为字符串类型才能正常输出。
相关推荐
![](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)