java 字符串加密
时间: 2023-11-12 22:55:29 浏览: 67
在Java中,字符串加密可以通过使用密码对源字符串进行加密算法来实现。在给定的引用中,使用了一个名为`EncryptAndDecrypt`的类来实现字符串的加密和解密操作。具体步骤如下:
1. 创建一个`EncryptAndDecrypt`的对象。
2. 通过调用`encrypt`方法,传入源字符串和密码作为参数,将源字符串加密为密文。
3. 输出密文结果。
4. 通过调用`decrypt`方法,传入密文和密码作为参数,将密文解密为明文。
5. 输出明文结果。
请注意,这里的加密和解密算法是通过遍历字符数组,并进行相应的加密和解密运算实现的。
相关问题
写一个java字符串加密
以下是一个简单的Java字符串加密示例,使用了AES对称加密算法:
```java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class StringEncryptor {
private static final String SECRET_KEY = "my-secret-key-123"; // 密钥,长度必须是16、24或32个字符(128、192或256位)
public static String encrypt(String strToEncrypt) {
try {
SecretKeySpec secretKey = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(strToEncrypt.getBytes("UTF-8"));
return Base64.getEncoder().encodeToString(encryptedBytes);
} catch (Exception e) {
System.err.println("Error while encrypting: " + e.toString());
}
return null;
}
public static String decrypt(String strToDecrypt) {
try {
SecretKeySpec secretKey = new SecretKeySpec(SECRET_KEY.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedBytes = Base64.getDecoder().decode(strToDecrypt);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
} catch (Exception e) {
System.err.println("Error while decrypting: " + e.toString());
}
return null;
}
public static void main(String[] args) {
String originalString = "Hello World!";
String encryptedString = encrypt(originalString);
String decryptedString = decrypt(encryptedString);
System.out.println("Original String: " + originalString);
System.out.println("Encrypted String: " + encryptedString);
System.out.println("Decrypted String: " + decryptedString);
}
}
```
这段代码使用了AES算法进行加密和解密,密钥为`my-secret-key-123`。你可以根据需要修改密钥和算法。
java字符串加密_Java简单加密字符串
Java 中可以使用 MessageDigest 类来进行字符串加密。以下是一个简单的示例代码:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class StringEncryptor {
public static String encrypt(String input) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
```
使用 SHA-256 算法对字符串进行加密,返回一个 32 位的十六进制字符串。可以通过调用 `encrypt` 方法来加密任意字符串:
```java
String input = "Hello, world!";
String encrypted = StringEncryptor.encrypt(input);
System.out.println(encrypted); // 输出:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)