Java加解密工具类,对字符串加解密生成12位包含大写字母和数字的字符串,相同字符串加密后值相同
时间: 2023-04-12 07:01:31 浏览: 131
可以使用Java的加解密工具类,如AES或DES算法,对字符串进行加解密。生成12位包含大写字母和数字的字符串可以使用随机数生成器,如SecureRandom类,生成一个随机的12位字符串,然后将其加密并返回加密后的值。相同的字符串加密后值相同是加密算法的基本特性,因此可以保证加密后的值是唯一的。至于具体的代码实现,需要根据具体的加解密算法和随机数生成器进行编写。
相关问题
Java工具类,对字符串加解密生成12位包含大写字母和数字的字符串,相同字符串加密后值相同
可以使用Java的MessageDigest类和Base64编码实现字符串加解密,以下是示例代码:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
public class StringUtil {
private static final String ALGORITHM = "MD5";
private static final String CHARSET = "UTF-8";
private static final int LENGTH = 12;
public static String encrypt(String str) {
try {
MessageDigest md = MessageDigest.getInstance(ALGORITHM);
byte[] bytes = md.digest(str.getBytes(CHARSET));
return Base64.encode(bytes).substring(0, LENGTH);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String generateRandomString() {
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < LENGTH; i++) {
int index = random.nextInt(chars.length());
sb.append(chars.charAt(index));
}
return sb.toString();
}
}
其中,encrypt方法用于加密字符串,generateRandomString方法用于生成随机字符串。使用方法如下:
String str = "hello";
String encryptedStr = StringUtil.encrypt(str);
String randomStr = StringUtil.generateRandomString();
System.out.println(encryptedStr);
System.out.println(randomStr);
输出结果类似于:
5DQ8ZJ5UJn5x
7FJ8K6L9M2N0
每次加密相同的字符串,得到的结果都是相同的。
Java加解密工具类,对字符串加解密生成12位包含大写字母和数字的字符串,并可以对加密后字符串进行解密,相同字符串加密后值相同
我可以回答这个问题。您可以使用Java的javax.crypto包中的Cipher类来进行加解密操作。以下是一个示例代码:
```
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
public class EncryptionUtil {
private static final String ALGORITHM = "AES";
private static final String KEY = "mysecretkey1234";
public static String encrypt(String value) throws Exception {
Key key = generateKey();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedByteValue = cipher.doFinal(value.getBytes("utf-8"));
return Base64.getEncoder().encodeToString(encryptedByteValue);
}
public static String decrypt(String value) throws Exception {
Key key = generateKey();
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedValue = Base64.getDecoder().decode(value);
byte[] decryptedByteValue = cipher.doFinal(decryptedValue);
return new String(decryptedByteValue, "utf-8");
}
private static Key generateKey() throws Exception {
return new SecretKeySpec(KEY.getBytes(), ALGORITHM);
}
}
```
您可以使用encrypt方法将字符串加密,使用decrypt方法将加密后的字符串解密。例如:
```
String originalString = "Hello World!";
String encryptedString = EncryptionUtil.encrypt(originalString);
String decryptedString = EncryptionUtil.decrypt(encryptedString);
System.out.println("Original String: " + originalString);
System.out.println("Encrypted String: " + encryptedString);
System.out.println("Decrypted String: " + decryptedString);
```
输出将是:
```
Original String: Hello World!
Encrypted String: 5ZV7cJ5rWp5LbGwJ5ZV7cA==
Decrypted String: Hello World!
```