java代码中对含有&的String字符串进行转义的代码
时间: 2024-06-12 18:07:23 浏览: 10
敏感信息的字符串进行加密处理的示例:
1. 使用Java的加密库进行加密处理:
```
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class EncryptUtil {
private static final String ENCRYPT_ALGORITHM = "AES";
private static final String ENCRYPT_KEY = "mysecretkey";
public static String encrypt(String input) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPT_ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return new String(encryptedBytes);
}
public static String decrypt(String encryptedInput) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPT_ALGORITHM);
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedInput.getBytes());
return new String(decryptedBytes);
}
}
```
使用示例:
```
String input = "my sensitive information";
String encryptedInput = EncryptUtil.encrypt(input);
String decryptedInput = EncryptUtil.decrypt(encryptedInput);
System.out.println("Input: " + input);
System.out.println("Encrypted Input: " + encryptedInput);
System.out.println("Decrypted Input: " + decryptedInput);
```
2. 使用Base64编码进行加密处理:
```
import java.util.Base64;
public class EncryptUtil {
public static String encrypt(String input) {
byte[] encryptedBytes = Base64.getEncoder().encode(input.getBytes());
return new String(encryptedBytes);
}
public static String decrypt(String encryptedInput) {
byte[] decryptedBytes = Base64.getDecoder().decode(encryptedInput.getBytes());
return new String(decryptedBytes);
}
}
```
使用示例:
```
String input = "my sensitive information";
String encryptedInput = EncryptUtil.encrypt(input);
String decryptedInput = EncryptUtil.decrypt(encryptedInput);
System.out.println("Input: " + input);
System.out.println("Encrypted Input: " + encryptedInput);
System.out.println("Decrypted Input: " + decryptedInput);
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)